Skip to content

Commit

Permalink
Improved list type determination (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
theharshpat authored Mar 9, 2024
1 parent 76425a3 commit 3c792b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions dentopy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ class FieldMetadata(BaseModel):
is_optional: bool


def determine_field_type_from_values(all_values: List[Any]) -> str:
def determine_field_type_from_values(data: List[Any]) -> str:
field_type = "Any"
if all(isinstance(val, dict) for val in all_values if val is not None):
if all(isinstance(val, dict) for val in data if val is not None):
field_type = "Dict"
elif all(isinstance(val, list) for val in all_values if val is not None):
list_type = "Any"
if all_values and all_values[0]:
list_types = set(type(val).__name__ for val in all_values[0] if val is not None)
if len(list_types) == 1:
list_type = list(list_types)[0]
field_type = f"List[{list_type}]"
elif all(isinstance(val, str) for val in all_values if val is not None):
elif all(isinstance(val, list) for val in data if val is not None):
field_type = "List"
all_field_value_types = set()
for val in data:
if val:
for item in val:
all_field_value_types.add(type(item).__name__)
if len(all_field_value_types) == 1:
field_type = f"List[{list(all_field_value_types)[0]}]"
elif all(isinstance(val, str) for val in data if val is not None):
field_type = "str"
elif all(isinstance(val, int) for val in all_values if val is not None):
elif all(isinstance(val, int) for val in data if val is not None):
field_type = "int"
elif all(isinstance(val, float) for val in all_values if val is not None):
elif all(isinstance(val, float) for val in data if val is not None):
field_type = "float"
elif all(isinstance(val, bool) for val in all_values if val is not None):
elif all(isinstance(val, bool) for val in data if val is not None):
field_type = "bool"
return field_type

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dentopy"
version = "0.1.1"
version = "0.1.3"
description = ""
authors = ["harsh-leen <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 3c792b8

Please sign in to comment.