Skip to content

Commit

Permalink
Add support for typing.Annotated (Issue #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Dec 17, 2022
1 parent ea57401 commit 75f7c4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ def _to_canon(self, t):
if isinstance(t, tuple):
return SumType([to_canon(x) for x in t])

if hasattr(typing, '_AnnotatedAlias') and isinstance(t, typing._AnnotatedAlias):
return to_canon(t.__origin__)

try:
t.__origin__
except AttributeError:
Expand Down Expand Up @@ -418,7 +421,6 @@ def _to_canon(self, t):
elif t.__origin__ is abc.Sequence or t.__origin__ is typing.Sequence:
x ,= t.__args__
return Sequence[to_canon(x)]

elif t.__origin__ is type or t.__origin__ is typing.Type:
# TODO test issubclass on t.__args__
return PythonDataType(type)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,17 @@ def test_basic(self):
assert not isa(frozenset({'a'}), FrozenSet[int])
assert not isa(frozenset({'a'}), Set[int])


def test_basic2(self):
assert issubclass(List[Tuple], list)

if hasattr(typing, 'Annotated'):
a = typing.Annotated[int, range(1, 10)]
assert is_subtype(a, int)
assert is_subtype(int, a)
assert isa(1, a)


def test_assert(self):
assert_isa(1, int)
assert_isa("a", str)
Expand Down

0 comments on commit 75f7c4f

Please sign in to comment.