Skip to content

Commit 9e1bb3d

Browse files
committed
Fix typing errors for python 3.7
1 parent 0242f98 commit 9e1bb3d

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,5 @@ cython_debug/
142142
.benchmarks/
143143

144144
.python-version
145+
146+
env3.7/

pydantic_redis/model.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,27 @@ def initialize(cls):
5050
is_generic = hasattr(field_type, "__origin__")
5151
if (
5252
is_generic
53-
and field_type.__origin__ == Union
54-
and field_type.__args__[-1] == None.__class__
53+
and typing_get_origin(field_type) == Union
54+
and typing_get_args(field_type)[-1] == None.__class__
5555
):
56-
field_type = field_type.__args__[0]
56+
field_type = typing_get_args(field_type)[0]
5757
is_generic = hasattr(field_type, "__origin__")
5858

5959
if (
6060
is_generic
61-
and field_type.__origin__ == List
62-
and issubclass(field_type.__args__[0], Model)
61+
and typing_get_origin(field_type) in (List, list)
62+
and issubclass(typing_get_args(field_type)[0], Model)
6363
):
64-
cls._nested_model_list_fields[field] = field_type.__args__[0]
64+
cls._nested_model_list_fields[field] = typing_get_args(field_type)[
65+
0
66+
]
6567

6668
elif (
6769
is_generic
68-
and field_type.__origin__ == Tuple
69-
and any([issubclass(v, Model) for v in field_type.__args__])
70+
and typing_get_origin(field_type) in (Tuple, tuple)
71+
and any([issubclass(v, Model) for v in typing_get_args(field_type)])
7072
):
71-
cls._nested_model_tuple_fields[field] = field_type.__args__
73+
cls._nested_model_tuple_fields[field] = typing_get_args(field_type)
7274

7375
elif issubclass(field_type, Model):
7476
cls._nested_model_fields[field] = field_type
@@ -401,7 +403,7 @@ def __get_select_fields(cls, columns: Optional[List[str]]) -> Optional[List[str]
401403
if isinstance(field_type, type(Model)):
402404
fields.append(f"{NESTED_MODEL_PREFIX}{col}")
403405
elif issubclass(field_type, List) and isinstance(
404-
field_type.__args__[0], type(Model)
406+
typing_get_args(field_type)[0], type(Model)
405407
):
406408
fields.append(f"{NESTED_MODEL_LIST_FIELD_PREFIX}{col}")
407409
else:
@@ -487,3 +489,19 @@ def strip_leading(word: str, substring: str) -> str:
487489
if word.startswith(substring):
488490
return word[len(substring) :]
489491
return word
492+
493+
494+
def typing_get_args(v: Any) -> Tuple[Any, ...]:
495+
"""Gets the __args__ of the annotations of a given typing"""
496+
try:
497+
return typing.get_args(v)
498+
except AttributeError:
499+
return getattr(v, "__args__", ()) if v is not typing.Generic else typing.Generic
500+
501+
502+
def typing_get_origin(v: Any) -> Optional[Any]:
503+
"""Gets the __origin__ of the annotations of a given typing"""
504+
try:
505+
return typing.get_origin(v)
506+
except AttributeError:
507+
return getattr(v, "__origin__", None)

0 commit comments

Comments
 (0)