def test_types_of_iterables(): assert _types_of_iterable([1]) == Type(int) assert _types_of_iterable(['1']) == Type(str) assert _types_of_iterable([1, '1']) == Union(int, str) assert _types_of_iterable((1, )) == Type(int) assert _types_of_iterable(('1', )) == Type(str) assert _types_of_iterable((1, '1')) == Union(int, str)
def test_types_of_iterables(): assert _types_of_iterable([1]) == Type(int) assert _types_of_iterable(["1"]) == Type(str) assert _types_of_iterable([1, "1"]) == Union[int, str] assert _types_of_iterable((1, )) == Type(int) assert _types_of_iterable(("1", )) == Type(str) assert _types_of_iterable((1, "1")) == Union[int, str]
def test_type_of(): assert type_of(1) == Type(int) assert type_of('1') == Type(str) assert type_of([1]) == List(int) assert type_of([1, '1']) == List({int, str}) assert type_of([1, '1', (1, )]) == List({int, str, Tuple(int)}) assert type_of((1, )) == Tuple(int) assert type_of(('1', )) == Tuple(str) assert type_of((1, '1')) == Tuple({int, str}) assert type_of((1, '1', [1])) == Tuple({int, str, List(int)})
def test_type_of(): assert type_of(1) == Type(int) assert type_of("1") == Type(str) assert type_of([1]) == List[int] assert type_of([1, "1"]) == List[Union[int, str]] assert type_of([1, "1", (1, )]) == List[Union[int, str, Tuple[int]]] assert type_of((1, )) == Tuple[int] assert type_of(("1", )) == Tuple[str] assert type_of((1, "1")) == Tuple[int, str] assert type_of((1, "1", [1])) == Tuple[int, str, List[int]]
def test_astype(): # Need `ok` here: printing will resolve `Self`. assert isinstance(as_type(Self), Self) assert isinstance(as_type([]), VarArgs) assert isinstance(as_type([int]), VarArgs) with pytest.raises(TypeError): as_type([int, str]) assert as_type({int, str}) == Union(int, str) assert as_type(Type(int)) == Type(int) assert as_type(int) == Type(int) with pytest.raises(RuntimeError): as_type(1)
def test_promisedtype(): t = PromisedType() with pytest.raises(ResolutionError): hash(t) with pytest.raises(ResolutionError): repr(t) with pytest.raises(ResolutionError): t.get_types() t.deliver(Type(int)) assert hash(t) == hash(Type(int)) assert repr(t) == repr(Type(int)) assert t.get_types() == Type(int).get_types() assert not t.parametric
def test_union(): assert hash(Union(int, str)) == hash(Union(str, int)) assert repr(Union(int, str)) == repr(Union(int, str)) assert set(Union(int, str).get_types()) == {str, int} assert not Union(int).parametric # Test equivalence between `Union` and `Type`. assert hash(Union(int)) == hash(Type(int)) assert hash(Union(int, str)) != hash(Type(int)) assert repr(Union(int)) == repr(Type(int)) assert repr(Union(int, str)) != repr(Type(int)) # Test lazy conversion to set. t = Union(int, int, str) assert isinstance(t._types, tuple) t.get_types() assert isinstance(t._types, set) # Test aliases. assert repr(Union(int, alias='MyUnion')) == 'tests.test_type.MyUnion' assert repr(Union(int, str, alias='MyUnion')) == 'tests.test_type.MyUnion'
def test_varargs_properties(): assert Sig(Num, Num, [Num]).expand_varargs_to(Sig(Num)) == (Type(Num), Type(Num)) assert Sig(Num, [Num]).expand_varargs_to(Sig(Num, Num, Num)) == (Type(Num), Type(Num), Type(Num)) assert Sig(Num).expand_varargs_to(Sig(Num, Num)) == (Type(Num), ) assert Sig(Num, [FP]).base == (Type(Num), ) assert Sig([Num]).has_varargs() assert not Sig(Num).has_varargs() assert Sig([Num]).varargs_type == Type(Num) with pytest.raises(RuntimeError): Sig(Num).varargs_type
def test_tupletype(): # Standard type tests. assert hash(Tuple(int)) == hash(Tuple(int)) assert hash(Tuple(int)) != hash(Tuple(str)) assert hash(Tuple(Tuple(int))) == hash(Tuple(Tuple(int))) assert hash(Tuple(Tuple(int))) != hash(Tuple(Tuple(str))) assert repr(Tuple(int)) == 'TupleType({})'.format(repr(Type(int))) assert issubclass(Tuple(int).get_types()[0], tuple) assert not issubclass(Tuple(int).get_types()[0], int) assert not issubclass(Tuple(int).get_types()[0], list) # Test instance check. assert isinstance((), Tuple(Union())) assert isinstance((1, 2), Tuple(Union(int))) # Check tracking of parametric. assert Tuple(int).parametric assert as_type([Tuple(int)]).parametric assert as_type({Tuple(int)}).parametric promise = PromisedType() promise.deliver(Tuple(int)) assert promise.resolve().parametric # Test correctness. dispatch = Dispatcher() @dispatch(object) def f(x): return 'fallback' @dispatch(tuple) def f(x): return 'tup' @dispatch(Tuple(int)) def f(x): return 'tup of int' @dispatch(Tuple(Tuple(int))) def f(x): return 'tup of tup of int' assert f((1, )) == 'tup of int' assert f(1) == 'fallback' assert f((1, 2)) == 'tup of int' assert f((1, 2, '3')) == 'tup' assert f(((1, ), )) == 'tup of tup of int' assert f(((1, ), (1, ))) == 'tup of tup of int' assert f(((1, ), (1, 2))) == 'tup of tup of int' assert f(((1, ), (1, 2, '3'))) == 'tup'
def test_listtype(): # Standard type tests. assert hash(List(int)) == hash(List(int)) assert hash(List(int)) != hash(List(str)) assert hash(List(List(int))) == hash(List(List(int))) assert hash(List(List(int))) != hash(List(List(str))) assert repr(List(int)) == 'ListType({})'.format(repr(Type(int))) assert issubclass(List(int).get_types()[0], list) assert not issubclass(List(int).get_types()[0], int) assert not issubclass(List(int).get_types()[0], tuple) # Test instance check. assert isinstance([], List(Union())) assert isinstance([1, 2], List(Union(int))) # Check tracking of parametric. assert List(int).parametric assert as_type([List(int)]).parametric assert as_type({List(int)}).parametric promise = PromisedType() promise.deliver(List(int)) assert promise.resolve().parametric # Test correctness. dispatch = Dispatcher() @dispatch(object) def f(x): return 'fallback' @dispatch(list) def f(x): return 'list' @dispatch(List(int)) def f(x): return 'list of int' @dispatch(List(List(int))) def f(x): return 'list of list of int' assert f([1]) == 'list of int' assert f(1) == 'fallback' assert f([1, 2]) == 'list of int' assert f([1, 2, '3']) == 'list' assert f([[1]]) == 'list of list of int' assert f([[1], [1]]) == 'list of list of int' assert f([[1], [1, 2]]) == 'list of list of int' assert f([[1], [1, 2, '3']]) == 'list'
def test_varargs(): assert hash(VarArgs(int)) == hash(VarArgs(int)) assert repr(VarArgs(int)) == 'VarArgs({!r})'.format(Type(int)) assert VarArgs(int).expand(2) == (Type(int), Type(int)) assert not VarArgs(int).parametric
def test_type(): assert hash(Type(int)) == hash(Type(int)) assert hash(Type(int)) != hash(Type(str)) assert repr(Type(int)) == '{}.{}'.format(int.__module__, int.__name__) assert Type(int).get_types() == (int, ) assert not Type(int).parametric
def test_is_type(): assert is_type(int) assert is_type({int}) assert is_type([int]) assert is_type(Type(int)) assert not is_type(1)
def test_is_object(): assert is_object(Type(object)) assert not is_object(Type(int))
def test_properties(): assert repr(Sig(Num)) == '({!r})'.format(Type(Num)) assert hash(Sig(Num)) == hash(Sig(Num)) assert hash(Sig(Num)) != hash(Type(Num)) assert len(Sig(Num)) == 1 assert len(Sig(Num, [Num])) == 1