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

Validation: Added support for typing.Type #41

Merged
merged 2 commits into from
Sep 22, 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
18 changes: 15 additions & 3 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def validate_instance(self, obj, sampler=None):
if not isinstance(obj, tuple):
raise TypeMismatchError(obj, self)


# cv_type_checking allows the user to define different behaviors for their objects
# while they are being type-checked.
# This is especially useful if they overrode __hash__ or __eq__ in nonconventional ways.
Expand Down Expand Up @@ -234,7 +235,7 @@ def cast_from(self, obj):


class GenericType(base_types.GenericType, PythonType):
def __init__(self, base, item=Any):
def __init__(self, base: PythonType, item=Any):
return super().__init__(base, item)


Expand Down Expand Up @@ -263,7 +264,7 @@ def cast_from(self, obj):

class DictType(GenericType):

def __init__(self, base, item=Any*Any):
def __init__(self, base: PythonType, item=Any*Any):
super().__init__(base)
if isinstance(item, tuple):
assert len(item) == 2
Expand Down Expand Up @@ -305,6 +306,13 @@ class TupleEllipsisType(SequenceType):
def __repr__(self):
return '%s[%s, ...]' % (self.base, self.item)

class TypeType(GenericType):

def validate_instance(self, obj, sampler=None):
t = type_caster.to_canon(obj)
if not t <= self.item:
raise TypeMismatchError(obj, self)


Object = PythonDataType(object)
Iter = SequenceType(PythonDataType(collections.abc.Iterable))
Expand All @@ -323,6 +331,7 @@ def __repr__(self):
Bytes = PythonDataType(bytes)
Callable = PythonDataType(abc.Callable) # TODO: Generic
Literal = OneOf
Type = TypeType(PythonDataType(type))


class _Number(PythonDataType):
Expand Down Expand Up @@ -578,8 +587,11 @@ def _to_canon(self, t):
x ,= args
return AbstractSet[to_canon(x)]
elif origin is type or origin is typing.Type:
if args:
t ,= args
return Type[self.to_canon(t)]
# TODO test issubclass on t.__args__
return PythonDataType(type)
return Type
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that changing from returning 'PythonDataType(type)' to simply 'Type' doesn't unintentionally alter the program's behavior.


raise NotImplementedError("No support for type:", t)

Expand Down
28 changes: 28 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ def __eq__(self, other):
inst = BadEq(True)
assert isa(inst, typing.Literal[1]) == False

def test_type_generic(self):
assert isa(int, typing.Type)
assert isa(int, typing.Type[int])
assert not isa(int, typing.Type[str])
assert isa(typing.List[int], typing.Type[typing.List[int]])
assert not isa(typing.List[int], typing.Type[typing.List[str]])
assert isa(int, typing.Type[object])
assert isa(list, typing.Type[typing.Sequence])

assert issubclass(typing.Type[int], typing.Type[int])
assert not issubclass(typing.Type[int], typing.Type[str])
assert issubclass(typing.Type[list], typing.Type[typing.Sequence])
assert issubclass(typing.Type[typing.List[int]], typing.Type[typing.Sequence[int]])
assert not issubclass(typing.Type[typing.List[int]], typing.Type[typing.Sequence[str]])


class TestDispatch(TestCase):
def setUp(self):
Expand Down Expand Up @@ -581,7 +596,20 @@ def __init__(self, points: list):
p2 = Point([10, 20])
assert p1 == p2

@unittest.skip("not implemented yet")
def test_type_generic(self):
dp = Dispatch()

@dp
def f(t: typing.Type[int]):
return "int"

@dp
def f(t: typing.Type[str]):
return "str"

assert f(int) == "int"
assert f(str) == "str"

def test_sequence(self):
pass
Expand Down
Loading