Skip to content

Commit 271114a

Browse files
Python 3.6 support for TypeAlias and ClassVar
1 parent 11b45df commit 271114a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/typeapi/typehint.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ def _make_wrapper(cls, hint: object, source: "Any | None") -> "TypeHint":
9292
return TypeVarTypeHint(hint, source)
9393
elif origin == tuple:
9494
return TupleTypeHint(hint, source)
95-
elif origin is None and getattr(hint, "__name__", None) == "TypeAlias":
95+
96+
elif origin is None and type(hint).__name__ == "_TypeAliasBase": # Python <3.10
97+
return TypeAliasTypeHint(hint, source)
98+
elif origin is None and getattr(hint, "__name__", None) == "TypeAlias": # Python >=3.10
9699
return TypeAliasTypeHint(hint, source)
97-
elif origin is ClassVar or hint is ClassVar:
100+
101+
elif origin is None and type(hint).__name__ == "_ClassVar": # Python <3.10
102+
return ClassVarTypeHint(hint, source)
103+
elif origin is ClassVar or hint is ClassVar: # Python >=3.10
98104
return ClassVarTypeHint(hint, source)
99105

100106
return ClassTypeHint(hint, source)
@@ -523,4 +529,10 @@ class TypeAliasTypeHint(TypeHint):
523529

524530

525531
class ClassVarTypeHint(TypeHint):
526-
pass
532+
def __init__(self, hint: object, source: "Any | None" = None) -> None:
533+
super().__init__(hint, source)
534+
if hasattr(self.hint, "__type__"): # Python <3.10? (Maybe lower)
535+
if self.hint.__type__ is not None: # type: ignore[attr-defined]
536+
self._args = (self.hint.__type__,) # type: ignore[attr-defined]
537+
else:
538+
self._args = ()

0 commit comments

Comments
 (0)