Debian Codegraph @

test-data/unit/check-semanal-error.test

raw
1 -- Type checking after an error during semantic analysis
2 -- -----------------------------------------------------
3 --
4 -- This tests both the semantic analyzer (that it does not generate
5 -- corrupt state on error) and the type checker (that it can deal with
6 -- whatever state the semantic analyzer sets up).
7
8 -- TODO:
9 -- - invalid type in annotation
10 -- - invalid function comment type annotation
11 -- - invalid multiple assignment type annotation
12 -- - using a type variable as a value
13 -- - using special names defined in typing as values
14
15 [case testMissingModuleImport1]
16 import m # E
17 m.foo()
18 m.x = m.y
19 1() # E
20 [out]
21 main:1: error: Cannot find implementation or library stub for module named "m"
22 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
23 main:4: error: "int" not callable
24
25 [case testMissingModuleImport2]
26 from m import x # E
27 x.foo()
28 x.a = x.b
29 1() # E
30 [out]
31 main:1: error: Cannot find implementation or library stub for module named "m"
32 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
33 main:4: error: "int" not callable
34
35 [case testMissingModuleImport3]
36 from m import * # E
37 x # E
38 1() # E
39 [out]
40 main:1: error: Cannot find implementation or library stub for module named "m"
41 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
42 main:2: error: Name "x" is not defined
43 main:3: error: "int" not callable
44
45 [case testInvalidBaseClass1]
46 class A(X): # E: Name "X" is not defined
47 x = 1
48 A().foo(1)
49 A().x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
50
51 [case testInvalidBaseClass2]
52
53 X = 1
54 class A(X): # E
55 x = 1
56 A().foo(1)
57 A().x = '' # E
58 [out]
59 main:3: error: Variable "__main__.X" is not valid as a type
60 main:3: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
61 main:3: error: Invalid base class "X"
62 main:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")
63
64
65 [case testInvalidNumberOfTypeArgs]
66 from typing import TypeVar
67 T = TypeVar('T')
68 class C: # Forgot to add type params here
69 def __init__(self, t: T) -> None: pass
70 c = C(t=3) # type: C[int] # E: "C" expects no type arguments, but 1 given
71
72 [case testBreakOutsideLoop]
73 break # E: "break" outside loop
74
75 [case testContinueOutsideLoop]
76 continue # E: "continue" outside loop
77
78 [case testYieldOutsideFunction]
79 yield # E: "yield" outside function
80 x = 1
81 yield from x # E: "yield from" outside function
82 [(yield 1) for _ in x] # E: "yield" inside comprehension or generator expression
83 {(yield 1) for _ in x} # E: "yield" inside comprehension or generator expression
84 {i: (yield 1) for i in x} # E: "yield" inside comprehension or generator expression
85 ((yield 1) for _ in x) # E: "yield" inside comprehension or generator expression
86 y = 1
87 [(yield from x) for _ in y] # E: "yield from" inside comprehension or generator expression
88 {(yield from x) for _ in y} # E: "yield from" inside comprehension or generator expression
89 {i: (yield from x) for i in y} # E: "yield from" inside comprehension or generator expression
90 ((yield from x) for _ in y) # E: "yield from" inside comprehension or generator expression
91 def f(y):
92 [x for x in (yield y)]
93 {x for x in (yield y)}
94 {x: x for x in (yield y)}
95 (x for x in (yield y))
96 [x for x in (yield from y)]
97 {x for x in (yield from y)}
98 {x: x for x in (yield from y)}
99 (x for x in (yield from y))
100 def g(y):
101 [(yield 1) for _ in y] # E: "yield" inside comprehension or generator expression
102 {(yield 1) for _ in y} # E: "yield" inside comprehension or generator expression
103 {i: (yield 1) for i in y} # E: "yield" inside comprehension or generator expression
104 ((yield 1) for _ in y) # E: "yield" inside comprehension or generator expression
105 lst = 1
106 [(yield from lst) for _ in y] # E: "yield from" inside comprehension or generator expression
107 {(yield from lst) for _ in y} # E: "yield from" inside comprehension or generator expression
108 {i: (yield from lst) for i in y} # E: "yield from" inside comprehension or generator expression
109 ((yield from lst) for _ in y) # E: "yield from" inside comprehension or generator expression
110 def h(y):
111 lst = 1
112 [x for x in lst if (yield y)] # E: "yield" inside comprehension or generator expression
113 {x for x in lst if (yield y)} # E: "yield" inside comprehension or generator expression
114 {x: x for x in lst if (yield y)} # E: "yield" inside comprehension or generator expression
115 (x for x in lst if (yield y)) # E: "yield" inside comprehension or generator expression
116 lst = 1
117 [x for x in lst if (yield from y)] # E: "yield from" inside comprehension or generator expression
118 {x for x in lst if (yield from y)} # E: "yield from" inside comprehension or generator expression
119 {x: x for x in lst if (yield from y)} # E: "yield from" inside comprehension or generator expression
120 (x for x in lst if (yield from y)) # E: "yield from" inside comprehension or generator expression
121
122 [case testImportFuncDup]
123
124 import m
125 def m() -> None: ... # E: Name "m" already defined (by an import)
126
127 [file m.py]
128 [out]
129
130 [case testIgnoredImportDup]
131
132 import m # type: ignore
133 from m import f # type: ignore
134 def m() -> None: ... # E: Name "m" already defined (possibly by an import)
135 def f() -> None: ... # E: Name "f" already defined (possibly by an import)
136
137 [out]
138
139 [case testRuntimeProtoTwoBases]
140 from typing import TypeVar, Generic, Protocol, runtime_checkable
141
142 T = TypeVar('T')
143
144 @runtime_checkable
145 class P(Protocol, Generic[T]):
146 attr: T
147
148 class C:
149 attr: int
150
151 x: P[int] = C()
152 [builtins fixtures/tuple.pyi]
153 [typing fixtures/typing-full.pyi]
154
155 [case testSemanalDoesNotLeakSyntheticTypes]
156 # flags: --cache-fine-grained
157 from typing import Generic, NamedTuple, TypedDict, TypeVar
158 from dataclasses import dataclass
159
160 T = TypeVar('T')
161 class Wrap(Generic[T]): pass
162
163 invalid_1: 1 + 2 # E: Invalid type comment or annotation
164 invalid_2: Wrap[1 + 2] # E: Invalid type comment or annotation
165
166 class A:
167 invalid_1: 1 + 2 # E: Invalid type comment or annotation
168 invalid_2: Wrap[1 + 2] # E: Invalid type comment or annotation
169
170 class B(NamedTuple):
171 invalid_1: 1 + 2 # E: Invalid type comment or annotation
172 invalid_2: Wrap[1 + 2] # E: Invalid type comment or annotation
173
174 class C(TypedDict):
175 invalid_1: 1 + 2 # E: Invalid type comment or annotation
176 invalid_2: Wrap[1 + 2] # E: Invalid type comment or annotation
177
178 @dataclass
179 class D:
180 invalid_1: 1 + 2 # E: Invalid type comment or annotation
181 invalid_2: Wrap[1 + 2] # E: Invalid type comment or annotation
182 [builtins fixtures/dict.pyi]
183 [typing fixtures/typing-typeddict.pyi]