aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/typeguard/tests/mypy/negative.py
blob: 6db0eb2a35b8af4f6ca83048b40c2c05b6dde486 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typeguard import check_argument_types, check_return_type, typechecked, typeguard_ignore


@typechecked
def foo(x: int) -> int:
    return x + 1


@typechecked
def bar(x: int) -> int:
    return str(x)  # error: Incompatible return value type (got "str", expected "int")


@typeguard_ignore
def non_typeguard_checked_func(x: int) -> int:
    return str(x)  # error: Incompatible return value type (got "str", expected "int")


def returns_str() -> str:
    return bar(0)  # error: Incompatible return value type (got "int", expected "str")


def arg_type(x: int) -> str:
    return check_argument_types()  # noqa: E501 # error: Incompatible return value type (got "bool", expected "str")


def ret_type() -> str:
    return check_return_type(False)  # noqa: E501 # error: Incompatible return value type (got "bool", expected "str")


_ = arg_type(foo)  # noqa: E501 # error: Argument 1 to "arg_type" has incompatible type "Callable[[int], int]"; expected "int"
_ = foo("typeguard")  # error: Argument 1 to "foo" has incompatible type "str"; expected "int"


@typechecked
class MyClass:
    def __init__(self, x: int = 0) -> None:
        self.x = x

    def add(self, y: int) -> int:
        return self.x + y


def get_value(c: MyClass) -> int:
    return c.x


def create_myclass(x: int) -> MyClass:
    return MyClass(x)


_ = get_value("foo")  # noqa: E501 # error: Argument 1 to "get_value" has incompatible type "str"; expected "MyClass"
_ = MyClass(returns_str())  # noqa: E501 # error: Argument 1 to "MyClass" has incompatible type "str"; expected "int"