Skip to content

Commit

Permalink
Merge pull request #47 from erezsh/dec2023
Browse files Browse the repository at this point in the history
Fixes for typesystem (mostly around handling of Any)
  • Loading branch information
erezsh authored Dec 30, 2023
2 parents e70dbc4 + d89cf87 commit b4c0519
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ contextvars = {version = "*", python = "~3.6"}

[tool.poetry.dev-dependencies]
typing_extensions = "*"
# The following are used for benchmarking -
pytest-benchmark = "*"
beartype = "*"
plum-dispatch = "*"
multipledispatch = "*"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
22 changes: 21 additions & 1 deletion runtype/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def __ge__(self, other):

def __le__(self, other):
if isinstance(other, Type):
return other is self
if other is self: # Optimization
return True

return NotImplemented

Expand All @@ -72,6 +73,12 @@ def __le__(self, other):

return super().__le__(other)

def __ge__(self, other):
# XXX hack
if isinstance(other, AnyType):
return False
return NotImplemented


class SumType(Type):
"""Implements a sum type, i.e. a disjoint union of a set of types.
Expand Down Expand Up @@ -170,6 +177,11 @@ class ContainerType(DataType):
def __getitem__(self, other):
return GenericType(self, other)

def __le__(self, other):
# XXX hack
if isinstance(other, AnyType):
return True
return super().__le__(other)

class GenericType(ContainerType):
"""Implements a generic type. i.e. a container for items of a specific type.
Expand Down Expand Up @@ -214,6 +226,10 @@ def __le__(self, other):
elif isinstance(other, DataType):
return self.base <= other

elif isinstance(other, AnyType):
# HACK
return True

return NotImplemented

def __ge__(self, other):
Expand All @@ -223,6 +239,10 @@ def __ge__(self, other):
elif isinstance(other, DataType):
return self.base >= other

elif isinstance(other, AnyType):
# HACK
return False

return NotImplemented

def __hash__(self):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,17 @@ class Foo:
"d": {"a": {"baz": 2}}
}




def test_any(self):
assert is_subtype(int, Any)
assert not is_subtype(Any, int)
assert is_subtype(Any, Any)
assert is_subtype(Any, Union[Any, int])
assert is_subtype(Any, Union[Any, None])
assert is_subtype(Union[Any, int], Any)
assert is_subtype(Union[Any, None], Any)
assert is_subtype(Union[Any, None], Union[Any, None])
assert is_subtype(dict, Any, )
assert not is_subtype(Any, dict)

if __name__ == '__main__':
unittest.main()
14 changes: 9 additions & 5 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import collections.abc as cabc

from runtype.base_types import DataType, ContainerType, PhantomType
from runtype.pytypes import type_caster, List, Dict, Int, Any, Constraint, String, Tuple, Iter, Literal
from runtype.pytypes import type_caster, List, Dict, Int, Any, Constraint, String, Tuple, Iter, Literal, NoneType
from runtype.typesystem import TypeSystem


Expand Down Expand Up @@ -60,7 +60,7 @@ def test_phantom(self):



def test_pytypes(self):
def test_pytypes1(self):
assert List + Dict == Dict + List
assert Any + ((Any + Any) + Any) is Any

Expand Down Expand Up @@ -133,7 +133,7 @@ def get_type(self, a):
assert i.isinstance(3, 4)
assert not i.isinstance(4, 3)

def test_pytypes(self):
def test_pytypes2(self):
assert Tuple <= Tuple
assert Tuple >= Tuple
# assert Tuple[int] <= Tuple
Expand Down Expand Up @@ -231,8 +231,12 @@ def test_canonize_pytypes(self):
for b in bad:
assert not t.test_instance(b), (t, b)



def test_any(self):
assert Any <= Any
assert Any <= Any + Int
assert Any <= Any + NoneType
assert Any + Int <= Any
assert Any + NoneType <= Any



Expand Down

0 comments on commit b4c0519

Please sign in to comment.