Skip to content

Commit

Permalink
Remove rednundant if branches and adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sultan Iman committed Feb 6, 2024
1 parent 6037b04 commit a077442
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
14 changes: 3 additions & 11 deletions dlt/common/libs/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ def pydantic_to_table_schema_columns(
inner_type = list
elif is_dict_generic_type(inner_type):
inner_type = dict
elif issubclass(inner_type, BaseModel):
is_inner_type_pydantic_model = True

is_inner_type_pydantic_model = False
name = field.alias or field_name
Expand All @@ -132,9 +130,7 @@ def pydantic_to_table_schema_columns(
# try to coerce unknown type to text
data_type = "text"

if data_type == "complex" and skip_complex_types:
continue
elif is_inner_type_pydantic_model and not skip_complex_types:
if is_inner_type_pydantic_model:
# This case is for a single field schema/model
# we need to generate snake_case field names
# and return flattened field schemas
Expand All @@ -146,12 +142,8 @@ def pydantic_to_table_schema_columns(
**hints,
"name": snake_case_naming_convention.make_path(name, hints["name"]),
}
elif is_inner_type_pydantic_model and skip_complex_types:
result[name] = {
"name": name,
"data_type": "complex",
"nullable": nullable,
}
elif data_type == "complex" and skip_complex_types:
continue
else:
result[name] = {
"name": name,
Expand Down
11 changes: 4 additions & 7 deletions tests/load/pipeline/test_pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,23 @@ def src():
}

assert loaded_values == {
"child": '{"child_attribute":"any string","optional_child_attribute":null}',
"child__child_attribute": "any string",
"child__optional_child_attribute": None,
"optional_parent_attribute": None,
"data_dictionary": '{"child_attribute":"any string"}',
}

keys = p.default_schema.tables["items"]["columns"].keys()
assert keys == {
"child",
"child__child_attribute",
"child__optional_child_attribute",
"optional_parent_attribute",
"data_dictionary",
"_dlt_load_id",
"_dlt_id",
}

columns = p.default_schema.tables["items"]["columns"]
assert columns["child"] == {
"name": "child",
"data_type": "complex",
"nullable": False,
}

assert columns["optional_parent_attribute"] == {
"name": "optional_parent_attribute",
Expand Down

0 comments on commit a077442

Please sign in to comment.