Skip to content

Commit 42d4cce

Browse files
authored
Fix dataclass field types (#257)
There is a (Python?) bug where calling `dataclasses.fields(some_data_class)` sometimes returns fields where `type` is a type _name_ instead of a _type_ (for example `'str'` instead of `<class str>`). This prevents our ORM from working as expected. The issue can be reproduced in databrickslabs/ucx#2401, and is currently fixed using a local workaround see https://github.com/databrickslabs/ucx/pull/2401/files#diff-40a8f3f4a3a5e5b222ccbfb95204001a6b007c78cbe55b9f1a53af5a8309886eR349 This PR works around the issue globally. --------- Co-authored-by: Eric Vergnaud <eric.vergnaud@databricks.com>
1 parent e6006ae commit 42d4cce

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/databricks/labs/lsql/backends.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ def _schema_for(cls, klass: Dataclass):
7070
fields = []
7171
for f in dataclasses.fields(klass):
7272
field_type = f.type
73+
# workaround rare (Python?) issue where f.type is the type name instead of the type itself
74+
# this seems to happen when the dataclass is first used from a file importing it
75+
if isinstance(field_type, str):
76+
try:
77+
field_type = __builtins__[field_type]
78+
except TypeError as e:
79+
logger.warning(f"Could not load type {field_type}", exc_info=e)
7380
if isinstance(field_type, UnionType):
7481
field_type = field_type.__args__[0]
7582
if field_type not in cls._builtin_type_mapping:

0 commit comments

Comments
 (0)