Debian Codegraph @

test-data/unit/check-warnings.test

raw
1 -- Test cases for warning generation.
2
3 -- Redundant casts
4 -- ---------------
5
6 [case testRedundantCast]
7 # flags: --warn-redundant-casts
8 from typing import cast
9 a = 1
10 b = cast(str, a)
11 c = cast(int, a)
12 [out]
13 main:5: error: Redundant cast to "int"
14
15 [case testRedundantCastWithIsinstance]
16 # flags: --warn-redundant-casts
17 from typing import cast, Union
18 x = 1 # type: Union[int, str]
19 if isinstance(x, str):
20 cast(str, x)
21 [builtins fixtures/isinstance.pyi]
22 [out]
23 main:5: error: Redundant cast to "str"
24
25 [case testCastToSuperclassNotRedundant]
26 # flags: --warn-redundant-casts
27 from typing import cast, TypeVar, List
28 T = TypeVar('T')
29 def add(xs: List[T], ys: List[T]) -> List[T]: pass
30 class A: pass
31 class B(A): pass
32 a = A()
33 b = B()
34 # Without the cast, the following line would fail to type check.
35 c = add([cast(A, b)], [a])
36 [builtins fixtures/list.pyi]
37
38 [case testCastToAnyTypeNotRedundant]
39 # flags: --warn-redundant-casts
40 from typing import cast, Any
41 a: Any
42 b = cast(Any, a)
43 [builtins fixtures/list.pyi]
44
45 [case testCastToObjectNotRedunant]
46 # flags: --warn-redundant-casts
47 from typing import cast
48
49 a = 1
50 b = cast(object, 1)
51
52 [case testCastFromLiteralRedundant]
53 # flags: --warn-redundant-casts
54 from typing import cast
55
56 cast(int, 1)
57 [out]
58 main:4: error: Redundant cast to "int"
59
60 [case testCastFromUnionOfAnyOk]
61 # flags: --warn-redundant-casts
62 from typing import Any, cast, Union
63
64 x = Any
65 y = Any
66 z = Any
67
68 def f(q: Union[x, y, z]) -> None:
69 cast(Union[x, y], q)
70
71 -- Unused 'type: ignore' comments
72 -- ------------------------------
73
74 [case testUnusedTypeIgnore]
75 # flags: --warn-unused-ignores
76 a = 1
77 if int():
78 a = 'a' # type: ignore
79 if int():
80 a = 2 # type: ignore # E: Unused "type: ignore" comment
81 if int():
82 a = 'b' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
83
84 [case testUnusedTypeIgnoreImport]
85 # flags: --warn-unused-ignores
86 import banana # type: ignore
87 import m # type: ignore
88 from m import * # type: ignore
89 [file m.py]
90 pass
91 [out]
92 main:3: error: Unused "type: ignore" comment
93 main:4: error: Unused "type: ignore" comment
94
95
96 -- No return
97 -- ---------
98
99 [case testNoReturn]
100 # flags: --warn-no-return
101 def f() -> int:
102 pass
103
104 def g() -> int:
105 if bool():
106 return 1
107 [builtins fixtures/list.pyi]
108 [out]
109 main:5: error: Missing return statement
110
111 [case testNoReturnWhile]
112 # flags: --warn-no-return
113 def h() -> int:
114 while True:
115 if bool():
116 return 1
117
118 def i() -> int:
119 while 1:
120 if bool():
121 return 1
122 if bool():
123 break
124
125 def j() -> int:
126 while 1:
127 if bool():
128 return 1
129 if bool():
130 continue
131 [builtins fixtures/list.pyi]
132 [out]
133 main:7: error: Missing return statement
134
135 [case testNoReturnExcept]
136 # flags: --warn-no-return
137 def f() -> int:
138 try:
139 return 1
140 except:
141 pass
142 def g() -> int:
143 try:
144 pass
145 except:
146 return 1
147 else:
148 return 1
149 def h() -> int:
150 try:
151 pass
152 except:
153 pass
154 else:
155 pass
156 finally:
157 return 1
158 [builtins fixtures/exception.pyi]
159 [out]
160 main:2: error: Missing return statement
161
162 [case testNoReturnEmptyBodyWithDocstring]
163 def f() -> int:
164 """Return the number of peppers."""
165 # This might be an @abstractmethod, for example
166 pass
167 [out]
168
169
170 -- Returning Any
171 -- -------------
172
173 [case testReturnAnyFromTypedFunction]
174 # flags: --warn-return-any
175 from typing import Any
176 def g() -> Any: pass
177 def f() -> int: return g()
178 [out]
179 main:4: error: Returning Any from function declared to return "int"
180
181 [case testReturnAnyForNotImplementedInBinaryMagicMethods]
182 # flags: --warn-return-any
183 class A:
184 def __eq__(self, other: object) -> bool: return NotImplemented
185 [builtins fixtures/notimplemented.pyi]
186 [out]
187
188 [case testReturnAnyForNotImplementedInNormalMethods]
189 # flags: --warn-return-any
190 class A:
191 def some(self) -> bool: return NotImplemented
192 [builtins fixtures/notimplemented.pyi]
193 [out]
194 main:3: error: Returning Any from function declared to return "bool"
195
196 [case testReturnAnyFromTypedFunctionWithSpecificFormatting]
197 # flags: --warn-return-any
198 from typing import Any, Tuple
199 typ = Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int,
200 int, int, int, int, int, int, int, int, int, int, int, int, int,
201 int, int, int, int, int, int, int, int, int, int, int, int, int,
202 int, int, int, int, int, int, int, int, int, int, int, int, int,
203 int, int, int, int, int, int, int, int, int, int, int, int, int,
204 int, int, int, int, int, int, int, int, int, int, int, int, int,
205 int, int, int, int, int, int, int, int, int, int, int, int, int]
206 def g() -> Any: pass
207 def f() -> typ: return g()
208 [builtins fixtures/tuple.pyi]
209 [out]
210 main:11: error: Returning Any from function declared to return "tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int]"
211
212 [case testReturnAnySilencedFromTypedFunction]
213 # flags: --warn-return-any
214 from typing import Any
215 def g() -> Any: pass
216 def f() -> int:
217 result = g() # type: int
218 return result
219 [out]
220
221 [case testReturnAnyFromUntypedFunction]
222 # flags: --warn-return-any
223 from typing import Any
224 def g() -> Any: pass
225 def f(): return g()
226 [out]
227
228 [case testReturnAnyFromAnyTypedFunction]
229 # flags: --warn-return-any
230 from typing import Any
231 def g() -> Any: pass
232 def f() -> Any: return g()
233 [out]
234
235 [case testOKReturnAnyIfProperSubtype]
236 # flags: --warn-return-any
237 from typing import Any, Optional
238
239 class Test(object):
240
241 def __init__(self) -> None:
242 self.attr = "foo" # type: Any
243
244 def foo(self, do_it: bool) -> Optional[Any]:
245 if do_it:
246 return self.attr # Should not warn here
247 else:
248 return None
249 [builtins fixtures/list.pyi]
250 [out]
251
252 [case testReturnAnyDeferred]
253 # flags: --warn-return-any
254 def foo(a1: A) -> int:
255 if a1._x:
256 return 1
257 n = 1
258 return n
259
260 class A:
261 def __init__(self, x: int) -> None:
262 self._x = x