コード例 #1
0
ファイル: test_type.py プロジェクト: GeneralCommission/plum
def test_typetype():
    Promised = PromisedType()
    Promised.deliver(int)

    assert ptype(type(int)) <= TypeType
    assert ptype(type(Promised)) <= TypeType

    assert not (ptype(int) <= TypeType)
    assert not (ptype(Promised) <= TypeType)
コード例 #2
0
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]) == f"List[{Type(int)!r}]"
    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[object]])
    assert isinstance([1, 2], List[Union[int]])

    # Check tracking of parametric.
    assert List[int].parametric
    assert ptype(List[List[int]]).parametric
    assert ptype(Union[List[int]]).parametric
    promise = PromisedType()
    promise.deliver(List[int])
    assert promise.resolve().parametric

    # Test correctness.
    dispatch = Dispatcher()

    @dispatch
    def f(x):
        return "fallback"

    @dispatch
    def f(x: list):
        return "list"

    @dispatch
    def f(x: List[int]):
        return "list of int"

    @dispatch
    def f(x: List[List[int]]):
        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"
コード例 #3
0
ファイル: test_type.py プロジェクト: GeneralCommission/plum
def test_astype():
    class A:
        pass

    t = Type(int)
    assert ptype(t) is t
    assert ptype(int) == t

    # Check conversion of strings.
    t = ptype("A")
    deliver_forward_reference(A)
    assert t == Type(A)

    with pytest.raises(RuntimeError):
        ptype(1)
コード例 #4
0
    def retrieve(self):
        """Retrieve the type, assuming that the module has been imported.

        Clears all cache after retrieval.
        """
        target = sys.modules[self.module]
        for part in self.name.split("."):
            target = getattr(target, part)
        self._type = ptype(target)
        clear_all_cache()
コード例 #5
0
def test_ptype():
    class A:
        pass

    t = Type(int)
    assert ptype(t) is t
    assert ptype(int) == t

    # Check `None` as valid type annotation.
    assert ptype(None) == Type(type(None))

    # Check conversion of strings.
    t = ptype("A")
    deliver_forward_reference(A)
    assert t == Type(A)

    with pytest.raises(RuntimeError):
        ptype(1)
コード例 #6
0
ファイル: test_type.py プロジェクト: GeneralCommission/plum
def test_astype_typing_mapping():
    class A:
        pass

    # `Union`:
    assert ptype(typing.Union[typing.Union[int], list]) == Union[Union[int],
                                                                 list]
    assert ptype(typing.Union) == Union[object]

    # `Optional`:
    assert ptype(typing.Optional[int]) == Union[int, type(None)]
    assert ptype(typing.Optional) == Union[object]

    # `List`:
    assert ptype(typing.List[typing.List[int]]) == List[List[int]]
    assert ptype(typing.List) == Type(list)

    # `Tuple`:
    assert ptype(typing.Tuple[typing.Tuple[int], list]) == Tuple[Tuple[int],
                                                                 list]
    assert ptype(typing.Tuple) == Type(tuple)

    # `ForwardRef`:
    if hasattr(typing, "ForwardRef"):
        t = ptype(typing.ForwardRef("A"))
    else:
        # The `typing` package is different for Python 3.6.
        t = ptype(typing._ForwardRef("A"))
    deliver_forward_reference(A)
    assert t == Type(A)

    # `Any`:
    assert ptype(typing.Any) == ptype(object)

    # `Callable`:
    assert ptype(typing.Callable) == Type(typing.Callable)

    # Check propagation of conversion of strings.
    t = ptype(typing.Union["A"])
    deliver_forward_reference(A)
    assert t == ptype(Union[A])

    t = ptype(typing.List["A"])
    deliver_forward_reference(A)
    assert t == ptype(List[A])

    t = ptype(typing.Tuple["A"])
    deliver_forward_reference(A)
    assert t == ptype(Tuple[A])
コード例 #7
0
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]) == f"Tuple[{Type(int)!r}]"
    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())
    assert isinstance((1, 2), Tuple[int, int])

    # Check tracking of parametric.
    assert Tuple[int].parametric
    assert ptype(List[Tuple[int]]).parametric
    assert ptype(Union[Tuple[int]]).parametric
    promise = PromisedType()
    promise.deliver(Tuple[int])
    assert promise.resolve().parametric

    # Test correctness.
    dispatch = Dispatcher()

    @dispatch
    def f(x):
        return "fallback"

    @dispatch
    def f(x: tuple):
        return "tup"

    @dispatch
    def f(x: Tuple[int]):
        return "tup of int"

    @dispatch
    def f(x: Tuple[int, int]):
        return "tup of double int"

    @dispatch
    def f(x: Tuple[Tuple[int]]):
        return "tup of tup of int"

    @dispatch
    def f(x: Tuple[Tuple[int], Tuple[int]]):
        return "tup of double tup of int"

    @dispatch
    def f(x: Tuple[int, Tuple[int, int]]):
        return "tup of int and tup of double int"

    assert f((1, )) == "tup of int"
    assert f(1) == "fallback"
    assert f((1, 2)) == "tup of double int"
    assert f((1, 2, "3")) == "tup"
    assert f(((1, ), )) == "tup of tup of int"
    assert f(((1, ), (1, ))) == "tup of double tup of int"
    assert f((1, (1, 2))) == "tup of int and tup of double int"
    assert f(((1, ), (1, 2))) == "tup"