Example #1
0
def test_complex_example_not_OK():
    with expected(tc.InputParameterError("1")):
        assert foo_wow_thats_nested(1) == 1
    with expected(IndexError("")):
        foo_wow_thats_nested([])
    with expected(tc.ReturnValueError("")):
        assert foo_wow_thats_nested(None) == None
    with expected(tc.ReturnValueError("")):
        assert foo_wow_thats_nested(dt.date.today()) == dt.date.today()
    with expected(tc.ReturnValueError("None")):
        assert foo_wow_thats_nested([dict(a=None, b=2.0)]) == None
Example #2
0
def test_enum2():
    @tc.typecheck
    def bar(*, x: tc.enum(None)) -> tc.enum():
        return x

    with expected(
            tc.ReturnValueError(
                "bar() has returned an incompatible value: None")):
        bar(x=None)
Example #3
0
def test_FixedSequenceChecker3():
    @tc.typecheck
    def foo(*args) -> (int, str):
        return args

    foo(1, "2") == 1, "2"
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: (1, 2)")):
        foo(1, 2)
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: (1, '2', None)")):
        foo(1, "2", None)
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: (1,)")):
        foo(1)
Example #4
0
def test_CallableChecker3():
    @tc.typecheck
    def foo(x=None) -> type(None):
        return x

    assert foo() is None
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: ''")):
        foo("")
Example #5
0
def test_OptionalChecker2():
    not_none = lambda x: x is not None
    with expected(
            tc.TypeCheckSpecificationError(
                "the default value for a is incompatible with its typecheck")):

        @tc.typecheck
        def foo(a: not_none = None):
            return a

    @tc.typecheck
    def foo(a: tc.optional(not_none) = None
            ):  # note how optional overrides the not_none
        return a

    assert foo() is None
    assert foo(None) is None
    with expected(
            tc.TypeCheckSpecificationError(
                "the default value for k is incompatible with its typecheck")):

        @tc.typecheck
        def foo(*, k: not_none = None):
            return k

    @tc.typecheck
    def foo(*,
            k: tc.optional(
                not_none) = None):  # note how optional overrides the not_none
        return k

    assert foo() is None
    assert foo(k=None) is None

    @tc.typecheck
    def foo(x=None) -> not_none:
        return x

    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: None")):
        foo()

    @tc.typecheck
    def foo(
        x=None
    ) -> tc.optional(not_none):  # note how optional overrides the not_none
        return x

    assert foo() is None
    assert foo(None) is None
Example #6
0
def test_any2():
    nothing_at_all = ((type(None), ) * 1000)
    either_nothing = tc.any(
        tc.any(tc.any(tc.any(*nothing_at_all), *nothing_at_all),
               *nothing_at_all), *nothing_at_all)

    @tc.typecheck
    def biz(x) -> either_nothing:
        return x

    with expected(
            tc.ReturnValueError(
                "biz() has returned an incompatible value: anything")):
        biz("anything")
Example #7
0
def test_range_float():
    @tc.typecheck
    def foo(x: tc.range(1.0, 11.0)) -> tc.range(1.0, 21.0):
        return 2 * x

    assert foo(1.0) == 2.0
    wrong_value = tc.InputParameterError(
        "foo() has got an incompatible value for x: ")
    with expected(wrong_value):
        foo(2)
    with expected(wrong_value):
        foo(0.0)
    with expected(wrong_value):
        foo(20.0)
    with expected(
            tc.ReturnValueError("foo() has returned an incompatible value: ")):
        foo(11.0)
Example #8
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")
Example #9
0
def test_TypeChecker():
    @tc.typecheck
    def foo(a: int) -> object:
        return a

    assert foo(10) == 10

    @tc.typecheck
    def foo(*args, a: str) -> float:
        return float(a)

    assert foo(a="10.0") == 10.0

    class Foo():
        pass

    class Bar(Foo):
        pass

    @tc.typecheck
    def foo(a: Bar) -> Foo:
        return a

    f = Bar()
    assert foo(f) is f
    f = Foo()
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for a: <")):
        foo(f)

    @tc.typecheck
    def foo(a: Foo) -> Bar:
        return a

    f = Bar()
    assert foo(f) is f
    f = Foo()
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: <")):
        foo(f)
Example #10
0
def test_OptionalChecker1():
    @tc.typecheck
    def foo(b: bool) -> bool:
        return not b

    assert foo(True) is False
    assert foo(False) is True
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for b: 0")):
        foo(0)

    @tc.typecheck
    def foo(*, b: tc.optional(bool) = None) -> bool:
        return b

    assert foo(b=False) is False
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: None")):
        foo()
Example #11
0
def test_empty_string_in_incompatible_values():
    @tc.typecheck
    def foo(s: lambda s: s != "" = None):
        return s

    assert foo() is None
    assert foo(None) is None
    assert foo(0) == 0
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for s: ''")):
        foo("")

    @tc.typecheck
    def foo(*, k: typecheck.framework.optional(lambda s: s != "") = None):
        return k

    assert foo() is None
    assert foo(k=None) is None
    assert foo(k=0) == 0
    with expected(
            tc.InputParameterError(
                "foo() has got an incompatible value for k: ''")):
        foo(k="")

    @tc.typecheck
    def foo(s=None) -> lambda s: s != "":
        return s

    assert foo() is None
    assert foo(None) is None
    assert foo(0) == 0
    with expected(
            tc.ReturnValueError(
                "foo() has returned an incompatible value: ''")):
        foo("")
Example #12
0
def test_Sequence_int_with_wrong_result():
    with expected(tc.ReturnValueError(
            "has returned an incompatible value: [1, '2']")):
        foo_Sequence_int_to_List_int([1], "2")