Skip to content

Commit

Permalink
rewrite based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Rusteam committed Jul 23, 2024
1 parent 3a23052 commit 060e110
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/hayhooks/server/utils/create_valid_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def _handle_generics(t_) -> GenericAlias:
else:
result = t
child_typing.append(result)
return GenericAlias(get_origin(t_), tuple(child_typing))

if len(child_typing) == 2 and child_typing[1] is type(None):
# because TypedDict can't handle union types with None
# rewrite them as Optional[type]
return Optional[child_typing[0]]
else:
return GenericAlias(get_origin(t_), tuple(child_typing))

if isclass(type_):
new_type = {}
Expand All @@ -35,14 +41,6 @@ def _handle_generics(t_) -> GenericAlias:
new_type[arg_name] = _handle_generics(arg_type)
else:
new_type[arg_name] = arg_type
if new_type:
# because TypedDict can't handle union types with None
# rewrite them as Optional[type]
for arg_name, arg_type in new_type.items():
type_args = get_args(arg_type)
if len(type_args) == 2 and type_args[1] is type(None):
new_type[arg_name] = Optional[type_args[0]]
return TypedDict(type_.__name__, new_type)

return type_

Expand Down

0 comments on commit 060e110

Please sign in to comment.