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

Implement callable generic with correct variance #55

Merged
merged 2 commits into from
Mar 9, 2024
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
29 changes: 26 additions & 3 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def cast_from(self, obj):


class PythonDataType(DataType, PythonType):
kernel: type

def __init__(self, kernel, supertypes={Any}):
self.kernel = kernel

Expand Down Expand Up @@ -328,6 +330,22 @@ def test_instance(self, obj, sampler=None):
t = type_caster.to_canon(obj)
return t <= self.item

class CallableType(PythonDataType):
args: PythonType = Any
ret: PythonType = Any

def __init__(self, args: PythonType = Any, ret: PythonType = Any):
self.args = args
self.ret = ret
self.kernel = typing.Callable

def __getitem__(self, signature):
args, ret = signature
return type(self)(args, ret)

def __repr__(self):
return f"Callable[{self.args}, {self.ret}]"


Object = PythonDataType(object)
Iter = SequenceType(PythonDataType(collections.abc.Iterable))
Expand All @@ -344,7 +362,7 @@ def test_instance(self, obj, sampler=None):
TupleEllipsis = TupleEllipsisType(PythonDataType(tuple))
# Float = PythonDataType(float)
Bytes = PythonDataType(bytes)
Callable = PythonDataType(abc.Callable) # TODO: Generic
Callable = CallableType()
Literal = OneOf
Type = TypeType(PythonDataType(type))

Expand Down Expand Up @@ -545,6 +563,8 @@ def _to_canon(self, t):
return Sequence
elif t is typing.MutableSequence:
return MutableSequence
elif t is typing.Callable:
return Callable

if origin is None:
if isinstance(t, typing.TypeVar):
Expand Down Expand Up @@ -579,8 +599,8 @@ def _to_canon(self, t):
elif origin is typing.Union:
res = [to_canon(x) for x in args]
return SumType(res)
elif origin is abc.Callable or t is typing.Callable:
# return Callable[ProductType(to_canon(x) for x in t.__args__)]
elif origin is abc.Callable or origin is typing.Callable:
return Callable[ProductType(to_canon(x) for x in args[:-1]), to_canon(args[-1])]
return Callable # TODO
elif py38 and origin is typing.Literal:
return OneOf(args)
Expand Down Expand Up @@ -630,6 +650,9 @@ def to_canon(self, t) -> PythonType:
def le(self: PythonDataType, other: PythonDataType):
return issubclass(self.kernel, other.kernel)
@dp
def le(self: CallableType, other: CallableType):
return self.args >= other.args and self.ret <= other.ret
@dp
def le(self: TupleEllipsisType, other: TupleType):
return True
@dp
Expand Down
13 changes: 13 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from runtype.pytypes import type_caster, List, Dict, Int, Any, Constraint, String, Tuple, Iter, Literal, NoneType, Sequence, Mapping
from runtype.typesystem import TypeSystem

make_type = type_caster.to_canon

class TestTypes(TestCase):
def test_basic_types(self):
Expand Down Expand Up @@ -283,6 +284,18 @@ def test_invariance(self):
assert not Mapping[Int, Sequence] <= Mapping[Int, List]
assert not Dict[Int, Sequence] <= Dict[Int, List]

def test_callable(self):
repeat = make_type(typing.Callable[[str, int], str])
class _Str(str):
pass
assert repeat <= repeat
assert not make_type(typing.Callable[[_Str, int], str]) <= repeat
assert not make_type(typing.Callable[[int, str], str]) <= repeat
assert not repeat <= make_type(typing.Callable[[str, int], _Str])
assert make_type(typing.Callable[[str, int], _Str]) <= repeat
assert repeat <= make_type(typing.Callable[[_Str, int], str])



if __name__ == '__main__':
unittest.main()
Loading