Skip to content

Commit 2e1005a

Browse files
param type parsing: correctly handling builtins types with args (e.g. list[int]) in Python 3.9+
1 parent 999759b commit 2e1005a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/dbally/views/exposed_functions.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ class TypeParsingError(ValueError):
1515
"""
1616

1717

18-
def _parse_complex_type(param_type: Union[type_ext.Type, _GenericAlias]) -> str:
18+
def _parse_standard_type(param_type: Union[type_ext.Type, _GenericAlias]) -> str:
1919
"""
20-
Generates string representation of a complex type from `typing` or `typing_extensions` module.
20+
Generates string representation of a data type (or alias) being neither custom class or string.
21+
This function is primarily intended to parse types from consecutive modules:
22+
`builtins` (>= Python 3.9), `typing` and `typing_extensions`.
2123
2224
Args:
2325
param_type: type or type alias.
@@ -78,9 +80,16 @@ def parse_param_type(param_type: Union[type_ext.Type, _GenericAlias, str]) -> st
7880
if isinstance(param_type, str):
7981
return f"'{param_type}'"
8082

81-
if param_type.__module__ in ["typing", "typing_extensions"]:
82-
return _parse_complex_type(param_type)
83+
if param_type.__module__ in ["builtins", "typing", "typing_extensions"]:
84+
return _parse_standard_type(param_type)
8385

86+
# TODO add explicit support Generic types,
87+
# although they should be already handled okay by _parse_standard_type()
88+
89+
# TODO test on various different Python versions > 3.8
90+
91+
# fallback, at the moment we expect this to be called only for type aliases
92+
# note that in Python 3.12+ there exists typing.TypeAliasType that can be checked with isinstance()
8493
return str(param_type)
8594

8695

0 commit comments

Comments
 (0)