Exemplo n.º 1
0
def test_default_vs_checked_kwargs11():
    @tc.typecheck
    def p1c_q2c(*, p: int = 1, q: int = 2):
        return p + q

    assert p1c_q2c(p=1, q=2) == 3
    with expected(
            tc.InputParameterError(
                "p1c_q2c() has got an incompatible value for q: 2.0")):
        p1c_q2c(p=1, q=2.0)
    with expected(
            tc.InputParameterError(
                "p1c_q2c() has got an incompatible value for p: 1.0")):
        p1c_q2c(p=1.0, q=2)
    with expected(
            tc.InputParameterError,
            "p1c_q2c\(\) has got an incompatible value for (p: 1.0|q: 2.0)"):
        p1c_q2c(p=1.0, q=2.0)
    with expected(
            tc.InputParameterError(
                "p1c_q2c() has got an incompatible value for q: <no value>")):
        p1c_q2c(p=1)
    with expected(
            tc.InputParameterError(
                "p1c_q2c() has got an incompatible value for p: <no value>")):
        p1c_q2c(q=2)
Exemplo n.º 2
0
def test_default_vs_checked_kwargs7():
    @tc.typecheck
    def pxn_q2c(*, p, q: int = 2):
        return p + q

    assert pxn_q2c(p=1, q=2) == 3
    with expected(
            tc.InputParameterError(
                "pxn_q2c() has got an incompatible value for q: 2.0")):
        pxn_q2c(p=1, q=2.0)
    assert pxn_q2c(p=1.0, q=2) == 3.0
    with expected(
            tc.InputParameterError(
                "pxn_q2c() has got an incompatible value for q: 2.0")):
        pxn_q2c(p=1.0, q=2.0)
    with expected(
            tc.InputParameterError(
                "pxn_q2c() has got an incompatible value for q: <no value>")):
        pxn_q2c(p=1)
    with expected(TypeError, "pxn_q2c"):
        pxn_q2c(q=2)
    with expected(
            tc.InputParameterError(
                "pxn_q2c() has got an incompatible value for q: <no value>")):
        pxn_q2c()
Exemplo n.º 3
0
def test_default_vs_checked_kwargs10():
    @tc.typecheck
    def pxc_q2c(*, p: int, q: int = 2):
        return p + q

    assert pxc_q2c(p=1, q=2) == 3
    with expected(
            tc.InputParameterError(
                "pxc_q2c() has got an incompatible value for q: 2.0")):
        pxc_q2c(p=1, q=2.0)
    with expected(
            tc.InputParameterError(
                "pxc_q2c() has got an incompatible value for p: 1.0")):
        pxc_q2c(p=1.0, q=2)
    with expected(
            tc.InputParameterError,
            "pxc_q2c\(\) has got an incompatible value for (p: 1.0|q: 2.0)"):
        pxc_q2c(p=1.0, q=2.0)
    # TODO: should tc.optional() be required when a 'None' default is given? No! (also elsewhere)
    with expected(
            tc.InputParameterError(
                "pxc_q2c() has got an incompatible value for q: <no value>")):
        pxc_q2c(p=1)
    with expected(
            tc.InputParameterError(
                "pxc_q2c() has got an incompatible value for p: <no value>")):
        pxc_q2c(q=2)
Exemplo n.º 4
0
def test_any1():
    @tc.typecheck
    def foo(x: tc.any()):
        pass

    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for x: 1")):
        foo(1)

    @tc.typecheck
    def bar(x: tc.any((int, float), tc.re("^foo$"), tc.enum(b"X", b"Y"))):
        pass

    bar((1, 1.0))
    bar("foo")
    bar(b"X")
    bar(b"Y")
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x: (1.0, 1)")):
        bar((1.0, 1))
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x: b'foo'")):
        bar(b"foo")
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x: X")):
        bar("X")
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x: Y")):
        bar("Y")
Exemplo n.º 5
0
def test_FixedSequenceChecker2():
    @tc.typecheck
    def foo(a: [] = [], *, k: tc.optional([]) = None) -> ([], tc.optional([])):
        return a, k

    assert foo() == ([], None)
    assert foo([]) == ([], None)
    assert foo(k=[]) == ([], [])
    assert foo([], k=[]) == ([], [])
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: ()")):
        foo(())
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: (1,)")):
        foo((1, ))
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: ()")):
        foo(k=())
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: (1,)")):
        foo(k=(1, ))
Exemplo n.º 6
0
def test_simple_default_checked_args5():
    @tc.typecheck
    def axc_b2c(a: int, b: int = 2):
        return a + b

    assert axc_b2c(10, 20) == 30
    with expected(
            tc.InputParameterError(
                "axc_b2c() has got an incompatible value for b: 20.0")):
        axc_b2c(10, 20.0)
    with expected(
            tc.InputParameterError(
                "axc_b2c() has got an incompatible value for a: 10.0")):
        axc_b2c(10.0, 20)
    with expected(
            tc.InputParameterError(
                "axc_b2c() has got an incompatible value for a: 10.0")):
        axc_b2c(10.0, 20.0)
    assert axc_b2c(10) == 12
    with expected(
            tc.InputParameterError(
                "axc_b2c() has got an incompatible value for a: 10.0")):
        axc_b2c(10.0)
    with expected(TypeError, "axc_b2c"):
        axc_b2c()
Exemplo n.º 7
0
def test_map_of_complex():
    @tc.typecheck
    def foo(*,
            k: tc.map_of(
                (int, int),
                [tc.re("^[0-9]+$"), tc.re("^[0-9]+$")])):
        return functools.reduce(
            lambda r, t: r and str(t[0][0]) == t[1][0] and str(t[0][1]) == t[1]
            [1], k.items(), True)

    assert foo(k={(1, 2): ["1", "2"], (3, 4): ["3", "4"]})
    assert not foo(k={(1, 3): ["1", "2"], (3, 4): ["3", "4"]})
    assert not foo(k={(1, 2): ["1", "2"], (3, 4): ["3", "5"]})
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: {(1, 2): ['1', '2'], (3, 4): ['3', 'x']}"
            )):
        foo(k={(1, 2): ["1", "2"], (3, 4): ["3", "x"]})
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: {(1, 2): ['1', '2'], (3,): ['3', '4']}"
            )):
        foo(k={(1, 2): ["1", "2"], (3, ): ["3", "4"]})
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: {(1, 2): ['1', '2'], (3, 4.0): ['3', '4']}"
            )):
        foo(k={(1, 2): ["1", "2"], (3, 4.0): ["3", "4"]})
Exemplo n.º 8
0
def test_none2():
    class TestCase:
        pass

    class MyCheckers(TestCase):
        pass

    class AddressTest:
        pass

    def classname_contains_Test(arg):
        return type(arg).__name__.find("Test") >= 0

    @tc.typecheck
    def no_tests_please(arg: tc.none(TestCase, classname_contains_Test)):
        pass

    no_tests_please("stuff")  # OK
    with expected(
            tc.InputParameterError(
                "no_tests_please() has got an incompatible value for arg: <")):
        no_tests_please(TestCase())  # Wrong: not wanted here
    with expected(
            tc.InputParameterError(
                "no_tests_please() has got an incompatible value for arg: <")):
        no_tests_please(MyCheckers())  # Wrong: superclass not wanted here
    with expected(
            tc.InputParameterError(
                "no_tests_please() has got an incompatible value for arg: <")):
        no_tests_please(AddressTest())  # Wrong: suspicious class name
Exemplo n.º 9
0
def test_Union_not_OK():
    with expected(tc.InputParameterError("u: wrong")):
        foo_Union_int_SequenceFloat("wrong")
    with expected(tc.InputParameterError("u: [4]")):
        foo_Union_int_SequenceFloat([4])
    with expected(tc.InputParameterError("u: None")):
        foo_Union_int_SequenceFloat(None)
Exemplo n.º 10
0
def test_NamedTuple_not_OK():
    with expected(tc.InputParameterError("name=8, id=9)")):
        foo_Employee(Employee(name=8, id=9))
    with expected(tc.InputParameterError("'aaa')")):
        foo_Employee(Employee(name='Jones', id='aaa'))
    with expected(tc.InputParameterError("Employee2(name='Jones', id=999)")):
        foo_Employee(Employee2(name='Jones', id=999))
Exemplo n.º 11
0
def test_Set_Tc_not_OK():
    with expected(tc.InputParameterError("")):
        assert foo_Set_Tc_Tc_to_bool(set(("yes",b"maybe")), "yes")
    with expected(tc.InputParameterError("")):
        assert foo_Set_Tc_Tc_to_bool(set((1, 2)), 2)
    with expected(tc.InputParameterError("")):
        assert foo_Set_Tc_Tc_to_bool(set((("yes",),("maybe",))), ("yes",))
Exemplo n.º 12
0
def test_Iterator_Container_content_not_OK_catchable():
    """
    Because there is no suitable way to access their contents,
    such generic types may still pass the typecheck if their content is
    of the wrong type.
    This is a fundamental problem, not an implementation gap.
    The only cases where improper contents will be caught is when the argument
    is _also_ tg.Iterable.
    """
    with expected(tc.InputParameterError("list_iterator")):
        foo_Iterator(["shouldn't be", "strings here"].__iter__())
    with expected(tc.InputParameterError("3, 4")):
        foo_Container([[3, 4], [5, 6]])
Exemplo n.º 13
0
def test_seq_of_simple():
    @tc.typecheck
    def foo_s(x: tc.seq_of(int)) -> tc.seq_of(float):
        return list(map(float, x))

    assert foo_s([]) == []
    assert foo_s(()) == []
    assert foo_s([1, 2, 3]) == [1.0, 2.0, 3.0]
    with expected(
            tc.InputParameterError(
                "foo_s() has got an incompatible value for x: ['1.0']")):
        foo_s(["1.0"])
    with expected(
            tc.InputParameterError(
                "foo_s() has got an incompatible value for x: 1")):
        foo_s(1)
Exemplo n.º 14
0
def test_simple_default_checked_args2():
    @tc.typecheck
    def a1n_b2c(a=1, b: int = 2):
        return a + b

    assert a1n_b2c(10, 20) == 30
    with expected(
            tc.InputParameterError(
                "a1n_b2c() has got an incompatible value for b: 20.0")):
        a1n_b2c(10, 20.0)
    assert a1n_b2c(10.0, 20) == 30.0
    with expected(
            tc.InputParameterError(
                "a1n_b2c() has got an incompatible value for b: 20.0")):
        a1n_b2c(10.0, 20.0)
    assert a1n_b2c(10) == 12
    assert a1n_b2c(10.0) == 12.0
    assert a1n_b2c() == 3
Exemplo n.º 15
0
def test_named_args():
    @tc.typecheck
    def foo(a: int):
        return a

    @tc.typecheck
    def foo_with_default(a: int = 4):
        return a

    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: 1")):
        foo(a='1')
    with expected(
            tc.InputParameterError(
                "foo_with_default() has got an incompatible value for a: [1]")
    ):
        foo_with_default(a=[1])
Exemplo n.º 16
0
def test_all2():
    def complete_blocks(arg):
        return len(arg) % 512 == 0

    @tc.typecheck
    def foo_all(arg: tc.all(tc.any(bytes, bytearray), complete_blocks)):
        pass

    foo_all(b"x" * 512)  # OK
    foo_all(bytearray(b"x" * 1024))  # OK
    with expected(
            tc.InputParameterError(
                "foo_all() has got an incompatible value for arg: xxx")):
        foo_all("x" * 512)  # Wrong: not a bytearray or bytes
    with expected(
            tc.InputParameterError(
                "foo_all() has got an incompatible value for arg: b'xxx")):
        foo_all(b"x" * 1012)  # Wrong: no complete blocks
Exemplo n.º 17
0
def test_has2():
    @tc.typecheck
    def foo(*, k: tc.re("^[0-9A-F]+$")) -> tc.re("^[0-9]+$"):
        return "".join(reversed(k))

    assert foo(k="1234") == "4321"
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: ''")):
        foo(k="")
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: 1")):
        foo(k=1)
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: DAB")):
        foo(k="BAD")
Exemplo n.º 18
0
def test_forward_reference_to_local_class_OK_or_not_OK():
    class B:
        @tc.typecheck
        def foo_something(self, another: 'B') -> 'B':
            return self
    b1 = B()
    b2 = B()
    b1.foo_something(b2)
    with expected(tc.InputParameterError("something different")):
        b1.foo_something("something different")
Exemplo n.º 19
0
def test_simple_checked_args1():
    @tc.typecheck
    def axc_bxn(a: int, b):
        return a + b

    assert axc_bxn(10, 20) == 30
    assert axc_bxn(10, 20.0) == 30.0
    with expected(
            tc.InputParameterError(
                "axc_bxn() has got an incompatible value for a: 10.0")):
        axc_bxn(10.0, 20)
    with expected(
            tc.InputParameterError(
                "axc_bxn() has got an incompatible value for a: 10.0")):
        axc_bxn(10.0, 20.0)
    with expected(TypeError, "axc_bxn"):
        axc_bxn(10)
    with expected(TypeError, "axc_bxn"):
        axc_bxn()
Exemplo n.º 20
0
def test_simple_checked_args2():
    @tc.typecheck
    def axn_bxc(a, b: int):
        return a + b

    assert axn_bxc(10, 20) == 30
    with expected(
            tc.InputParameterError(
                "axn_bxc() has got an incompatible value for b: 20.0")):
        axn_bxc(10, 20.0)
    assert axn_bxc(10.0, 20) == 30.0
    with expected(
            tc.InputParameterError(
                "axn_bxc() has got an incompatible value for b: 20.0")):
        axn_bxc(10.0, 20.0)
    with expected(TypeError, "axn_bxc"):
        axn_bxc(10)
    with expected(TypeError, "axn_bxc"):
        axn_bxc()
Exemplo n.º 21
0
def test_map_of_simple():
    @tc.typecheck
    def foo(x: tc.map_of(int, str)) -> tc.map_of(str, int):
        return {v: k for k, v in x.items()}

    assert foo({}) == {}
    assert foo({1: "1", 2: "2"}) == {"1": 1, "2": 2}
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for x: []")):
        foo([])
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for x: {'1': '2'}")):
        foo({"1": "2"})
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for x: {1: 2}")):
        foo({1: 2})
Exemplo n.º 22
0
def test_default_vs_checked_kwargs1():
    @tc.typecheck
    def foo(*, a: str):
        return a

    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: <no value>")):
        foo()
    assert foo(a="b") == "b"
Exemplo n.º 23
0
def test_FixedSequenceChecker1():
    @tc.typecheck
    def foo(a: (int, str) = (1, "!"), *, k: tc.optional(()) = ()) -> (str, ()):
        return a[1], k

    assert foo() == ("!", ())
    assert foo((2, "x")) == ("x", ())
    assert foo(k=()) == ("!", ())
    assert foo((33, "44"), k=()) == ("44", ())
    assert foo([3, "4"]) == ("4", ())
    assert foo(k=[]) == ("!", [])
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: (1,)")):
        foo((1, ))
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: (1, 2)")):
        foo(k=(1, 2))
Exemplo n.º 24
0
def test_FixedSequenceChecker5():
    @tc.typecheck
    def foo(a: collections.UserList([str, collections.UserList])):
        return a[1][1]

    assert foo(collections.UserList(["aha!",
                                     collections.UserList([3, 4, 5])])) == 4
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: ")):
        assert foo(["aha!", collections.UserList([3, 4, 5])]) == 4
Exemplo n.º 25
0
def test_FixedSequenceChecker4():
    @tc.typecheck
    def foo(*, k: tc.optional([[[[lambda x: x % 3 == 1]]]]) = [[[[4]]]]):
        return k[0][0][0][0]

    assert foo() % 3 == 1
    assert foo(k=[[[[1]]]]) % 3 == 1
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: [[[[5]]]]")):
        foo(k=[[[[5]]]])
Exemplo n.º 26
0
def test_none1():
    @tc.typecheck
    def foo(x: tc.none()):
        pass

    foo(foo)  # an empty none() accepts anything

    @tc.typecheck
    def taboo(x: tc.none(tc.re("foo"), tc.re("bar"))):
        pass

    taboo("boofar")
    with expected(
            tc.InputParameterError(
                "taboo() has got an incompatible value for x: foofar")):
        taboo("foofar")
    with expected(
            tc.InputParameterError(
                "taboo() has got an incompatible value for x: boobar-ism")):
        taboo("boobar-ism")
Exemplo n.º 27
0
def test_all1():
    @tc.typecheck
    def foo(x: tc.all()):
        pass

    foo(foo)  # an empty all() accepts anything

    @tc.typecheck
    def bar(x: tc.all(tc.re("abcdef"), tc.re("defghi"), tc.re("^abc"))):
        pass

    bar("abcdefghijklm")
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x:  abcdefghi")):
        bar(" abcdefghi")
    with expected(
            tc.InputParameterError(
                "bar() has got an incompatible value for x: abc defghi")):
        bar("abc defghi")
Exemplo n.º 28
0
def test_enum1():
    @tc.typecheck
    def foo(x: tc.enum(int, 1)) -> tc.enum(1, int):
        return x

    assert foo(1) == 1
    assert foo(int) is int
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for x: 2")):
        foo(2)
Exemplo n.º 29
0
def test_has5():
    @tc.typecheck
    def numbers_only_please(s: tc.re("^[0-9]+$")):
        pass

    numbers_only_please("123")
    with expected(
            tc.InputParameterError(
                "numbers_only_please() has got an incompatible value for s: 123"
            )):
        numbers_only_please("123\x00HUH?")
Exemplo n.º 30
0
def test_default_vs_checked_kwargs2():
    @tc.typecheck
    def foo(*, a: typecheck.framework.optional(str) = "a"):
        return a

    assert foo() == "a"
    assert foo(a="b") == "b"
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: 10")):
        foo(a=10)