Skip to content

Commit

Permalink
Merge pull request #63 from freakypie/master
Browse files Browse the repository at this point in the history
TypedDict introspection
  • Loading branch information
adenh93 authored Aug 3, 2024
2 parents d1cdaab + b33b651 commit bb1a113
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions django_typomatic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ def __get_nested_serializer_field(
ts_interface(context=context)(return_type)
# For duplicate interface, set not exported
setattr(return_type, "__exported__", False)
elif getattr(return_type, "__annotations__", None):
ts_type = __process_annotation(return_type.__annotations__)

if is_serializer_type:
ts_type = __get_trimmed_name(return_type.__name__, trim_serializer_output)
Expand All @@ -459,6 +461,41 @@ def __get_nested_serializer_field(
return is_many, ts_type


def __process_annotation(type, debug=False) -> str:
retval = None
type_name = getattr(type, "_name", None)
if isinstance(type, dict):
retval = "{ "
for k, v in type.items():
keytype = __process_annotation(v, debug)
retval += f"{k}: {keytype}, "
retval += " }"
elif type_name == "List":
retval = __process_annotation(type.__args__[0], debug) + "[]"
elif type_name == "Tuple":
retval = "[ "
for v in type.__args__:
retval += __process_annotation(v, debug) + ", "
retval += "]"
else:
retval = {
float: "number",
int: "number",
str: "string",
bool: "boolean",
}.get(type)
if not retval:
retval = "null"
if debug:
print(
"annotation",
type,
getattr(type, "__name__", None),
retval,
)
return retval


def __process_generic_type(return_type):
origin = get_origin(return_type)
args = get_args(return_type)
Expand Down

0 comments on commit bb1a113

Please sign in to comment.