Skip to content

Commit

Permalink
Fix handling of Any in sumtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Mar 1, 2024
1 parent 7de3405 commit edcb3d6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions runtype/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def create(cls, types):
if isinstance(t, SumType):
# Optimization: Flatten recursive SumTypes
x |= set(t.types)
elif isinstance(t, AnyType):
return t
else:
x.add(t)

Expand Down
4 changes: 2 additions & 2 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ def _to_canon(self, t):
t = _forwardref_evaluate(t, self.frame.f_globals, self.frame.f_locals, frozenset())

if isinstance(t, tuple):
return SumType([to_canon(x) for x in t])
return SumType.create([to_canon(x) for x in t])

if hasattr(types, 'UnionType') and isinstance(t, types.UnionType):
res = [to_canon(x) for x in t.__args__]
return SumType(res)
return SumType.create(res)

origin = getattr(t, '__origin__', None)
if hasattr(typing, '_AnnotatedAlias') and isinstance(t, typing._AnnotatedAlias):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def test_pytypes2(self):
self.assertRaises(ValueError, type_caster.to_canon, typing.Tuple[int, str, ...])


def test_pytypes3(self):
assert Any + Int == Any
assert Int + Any == Any


def test_canonize_pytypes(self):
pytypes = [
Expand Down

0 comments on commit edcb3d6

Please sign in to comment.