Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for typesystem (mostly around handling of Any) #47

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -
erezsh marked this conversation as resolved.
Show resolved Hide resolved
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):
erezsh marked this conversation as resolved.
Show resolved Hide resolved
assert Any <= Any
assert Any <= Any + Int
assert Any <= Any + NoneType
assert Any + Int <= Any
assert Any + NoneType <= Any



Expand Down
Loading