Skip to content

Commit 060e110

Browse files
committed
rewrite based on PR comments
1 parent 3a23052 commit 060e110

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/hayhooks/server/utils/create_valid_type.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def _handle_generics(t_) -> GenericAlias:
2626
else:
2727
result = t
2828
child_typing.append(result)
29-
return GenericAlias(get_origin(t_), tuple(child_typing))
29+
30+
if len(child_typing) == 2 and child_typing[1] is type(None):
31+
# because TypedDict can't handle union types with None
32+
# rewrite them as Optional[type]
33+
return Optional[child_typing[0]]
34+
else:
35+
return GenericAlias(get_origin(t_), tuple(child_typing))
3036

3137
if isclass(type_):
3238
new_type = {}
@@ -35,14 +41,6 @@ def _handle_generics(t_) -> GenericAlias:
3541
new_type[arg_name] = _handle_generics(arg_type)
3642
else:
3743
new_type[arg_name] = arg_type
38-
if new_type:
39-
# because TypedDict can't handle union types with None
40-
# rewrite them as Optional[type]
41-
for arg_name, arg_type in new_type.items():
42-
type_args = get_args(arg_type)
43-
if len(type_args) == 2 and type_args[1] is type(None):
44-
new_type[arg_name] = Optional[type_args[0]]
45-
return TypedDict(type_.__name__, new_type)
4644

4745
return type_
4846

0 commit comments

Comments
 (0)