File: check-type-checks.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (117 lines) | stat: -rw-r--r-- 4,247 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- Conditional type checks.


[case testSimpleIsinstance]
x: object
n: int
s: str
if int():
    n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
if isinstance(x, int):
    n = x
    s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
    n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
[builtins fixtures/isinstance.pyi]

[case testSimpleIsinstance2]
import typing
def f(x: object, n: int, s: str) -> None:
    if int():
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
        if isinstance(x, int):
            n = x
            s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
[builtins fixtures/isinstance.pyi]
[out]

[case testSimpleIsinstance3]
class A:
    x = None  # type: object
    n = None  # type: int
    s = None  # type: str
    if int():
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
    if isinstance(x, int):
        n = x
        s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    else:
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
[builtins fixtures/isinstance.pyi]
[out]

[case testMultipleIsinstanceTests]
import typing
class A: pass
class B(A): pass
def f(x: object, a: A, b: B, c: int) -> None:
    if isinstance(x, A):
        if isinstance(x, B):
            b = x
            x = a
        a = x
        c = x # E: Incompatible types in assignment (expression has type "A", variable has type "int")
[builtins fixtures/isinstance.pyi]
[out]

[case testMultipleIsinstanceTests2]
import typing
class A: pass
class B(A): pass
def f(x: object, y: object, n: int, s: str) -> None:
    if isinstance(x, int):
        if isinstance(y, str):
            n = x
            s = y
            s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
            n = y # E: Incompatible types in assignment (expression has type "str", variable has type "int")
        s = y # E: Incompatible types in assignment (expression has type "object", variable has type "str")
        n = y # E: Incompatible types in assignment (expression has type "object", variable has type "int")
        n = x
[builtins fixtures/isinstance.pyi]
[out]

[case testIsinstanceAndElif]
import typing
def f(x: object, n: int, s: str) -> None:
    if int():
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
        if isinstance(x, int):
            n = x
            s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
        elif isinstance(x, str):
            s = x
            n = x # E: Incompatible types in assignment (expression has type "str", variable has type "int")
        else:
            n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
            s = x # E: Incompatible types in assignment (expression has type "object", variable has type "str")
        n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
[builtins fixtures/isinstance.pyi]
[out]

[case testIsinstanceAndAnyType]
from typing import Any
def f(x: Any, n: int, s: str) -> None:
    if int():
        s = x
        if isinstance(x, int):
            n = x
            s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
        s = x
[builtins fixtures/isinstance.pyi]
[out]

[case testIsinstanceAndGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
class C(Generic[T]):
    def f(self, x: T) -> None: pass
def f(x: object) -> None:
    if isinstance(x, C):
        x.f(1)
        x.f('')
        x.g() # E: "C[Any]" has no attribute "g"
    x.g() # E: "object" has no attribute "g"
[builtins fixtures/isinstance.pyi]
[out]