Debian Codegraph @

test-data/unit/check-super.test

raw
1 -- Test cases for type checker related to super().
2
3
4 -- Supertype member reference
5 -- --------------------------
6
7
8 [case testAccessingSupertypeMethod]
9
10 class B:
11 def f(self) -> 'B': pass
12 class A(B):
13 def f(self) -> 'A':
14 a: A
15 b: B
16 if int():
17 a = super().f() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
18 a = super().g() # E: "g" undefined in superclass
19 b = super().f()
20 return a
21 [builtins fixtures/tuple.pyi]
22 [out]
23
24 [case testAccessingSuperTypeMethodWithArgs]
25 from typing import Any
26 class B:
27 def f(self, y: 'A') -> None: pass
28 class A(B):
29 def f(self, y: Any) -> None:
30 a: A
31 b: B
32 super().f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
33 super().f(a)
34 self.f(b)
35 self.f(a)
36 [builtins fixtures/tuple.pyi]
37 [out]
38
39 [case testAccessingSuperInit]
40 # flags: --no-strict-optional
41 import typing
42 class B:
43 def __init__(self, x: A) -> None: pass
44 class A(B):
45 def __init__(self) -> None:
46 super().__init__(B(None)) # E: Argument 1 to "__init__" of "B" has incompatible type "B"; expected "A"
47 super().__init__() # E: Missing positional argument "x" in call to "__init__" of "B"
48 super().__init__(A())
49 [out]
50
51 [case testAccessingSuperMemberWithDeepHierarchy]
52 import typing
53 class C:
54 def f(self) -> None: pass
55 class B(C): pass
56 class A(B):
57 def f(self) -> None:
58 super().g() # E: "g" undefined in superclass
59 super().f()
60 [out]
61
62 [case testAssignToBaseClassMethod]
63 import typing
64 class A:
65 def f(self) -> None: pass
66 class B(A):
67 def g(self) -> None:
68 super().f = None
69 [out]
70 main:6: error: Invalid assignment target
71
72 [case testSuperWithMultipleInheritance]
73 import typing
74 class A:
75 def f(self) -> None: pass
76 class B:
77 def g(self, x: int) -> None: pass
78 class C(A, B):
79 def f(self) -> None:
80 super().f()
81 super().g(1)
82 super().f(1) # E: Too many arguments for "f" of "A"
83 super().g() # E: Missing positional argument "x" in call to "g" of "B"
84 super().not_there() # E: "not_there" undefined in superclass
85 [out]
86
87 [case testSuperWithNew]
88 class A:
89 def __new__(cls, x: int) -> 'A':
90 return object.__new__(cls)
91
92 class B(A):
93 def __new__(cls, x: int, y: str = '') -> 'B':
94 super().__new__(cls, 1)
95 super().__new__(cls, 1, '') # E: Too many arguments for "__new__" of "A"
96 return cls(1)
97 B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
98 B(1)
99 B(1, 'x')
100 [builtins fixtures/__new__.pyi]
101
102 reveal_type(C.a) # N: Revealed type is "Any"
103 [out]
104
105 [case testSuperWithUnknownBase]
106 from typing import Any
107 B = None # type: Any
108 class C(B):
109 def __init__(self, arg=0):
110 super(C, self).__init__(arg, arg=arg)
111 [out]
112
113 [case testSuperSilentInDynamicFunction]
114 class A:
115 pass
116
117 class B(A):
118 def foo(self):
119 super(B, self).foo() # Not an error
120 [out]
121
122 [case testSuperWithAny]
123 class B:
124 def f(self) -> None: pass
125 class C(B):
126 def h(self, x) -> None:
127 reveal_type(super(x, x).f) # N: Revealed type is "def ()"
128 reveal_type(super(C, x).f) # N: Revealed type is "def ()"
129 reveal_type(super(C, type(x)).f) # N: Revealed type is "def (self: __main__.B)"
130
131 [case testSuperInUnannotatedMethod]
132 class C:
133 def h(self):
134 super(C, self).xyz
135
136 [case testSuperWithTypeObjects]
137 from typing import Type
138
139 class A:
140 def f(self) -> object: pass
141
142 class B(A):
143 def f(self) -> int: pass
144
145 @classmethod
146 def g(cls, x) -> None:
147 reveal_type(super(cls, x).f) # N: Revealed type is "def () -> builtins.object"
148
149 def h(self, t: Type[B]) -> None:
150 reveal_type(super(t, self).f) # N: Revealed type is "def () -> builtins.object"
151 [builtins fixtures/classmethod.pyi]
152
153 [case testSuperWithTypeTypeAsSecondArgument]
154 class B:
155 def f(self) -> None: pass
156
157 class C(B):
158 def __new__(cls) -> 'C':
159 super(C, cls).f
160 return C()
161
162 [case testSuperWithGenericSelf]
163 from typing import TypeVar
164
165 T = TypeVar('T', bound='C')
166
167 class B:
168 def f(self) -> float: pass
169
170 class C(B):
171 def f(self) -> int: pass
172
173 def g(self: T) -> T:
174 reveal_type(super(C, self).f) # N: Revealed type is "def () -> builtins.float"
175 return self
176
177 [case testSuperWithTypeVarValues1]
178 from typing import TypeVar
179
180 T = TypeVar('T', 'C', 'D')
181 S = TypeVar('S', 'B', 'C')
182
183 class B:
184 def f(self) -> None: pass
185
186 class C(B):
187 def f(self) -> None: pass
188
189 def g(self, x: T, y: S) -> None:
190 super(C, x).f
191 super(C, y).f # E: Argument 2 for "super" not an instance of argument 1
192
193 class D(C): pass
194
195 [case testSuperWithTypeVarValues2]
196 from typing import TypeVar, Generic
197
198 T = TypeVar('T', 'C', 'D')
199 S = TypeVar('S', 'B', 'C')
200
201 class B:
202 def f(self) -> None: pass
203
204 class C(B, Generic[T, S]):
205 def f(self) -> None: pass
206
207 def g(self, x: T, y: S) -> None:
208 super(C, x).f
209 super(C, y).f # E: Argument 2 for "super" not an instance of argument 1
210
211 class D(C): pass
212
213 [case testSuperInClassMethod]
214 from typing import Union
215
216 class A:
217 def f(self, i: int) -> None: pass
218
219 class B(A):
220 def f(self, i: Union[int, str]) -> None: pass
221
222 @classmethod
223 def g(cls, i: int) -> None:
224 super().f(B(), i)
225 super(B, cls).f(cls(), i)
226 super(B, B()).f(i)
227
228 super().f(B(), '') # E: Argument 2 to "f" of "A" has incompatible type "str"; expected "int"
229 super(B, cls).f(cls(), '') # E: Argument 2 to "f" of "A" has incompatible type "str"; expected "int"
230 super(B, B()).f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
231 [builtins fixtures/classmethod.pyi]
232
233 [case testSuperWithUnrelatedTypes]
234 from typing import Union
235
236 class A:
237 def f(self, s: str) -> None: pass
238
239 class B(A):
240 def f(self, i: Union[int, str]) -> None: pass
241
242 class C:
243 def g(self, b: B) -> None:
244 super(B, b).f('42')
245 super(B, b).f(42) # E: Argument 1 to "f" of "A" has incompatible type "int"; expected "str"
246
247 [case testSuperOutsideClass]
248 from typing import Union
249
250 class A:
251 def f(self, s: str) -> None: pass
252
253 class B(A):
254 def f(self, i: Union[int, str]) -> None: pass
255
256 def g(b: B) -> None:
257 super(B, b).f('42')
258 super(B, b).f(42) # E: Argument 1 to "f" of "A" has incompatible type "int"; expected "str"
259
260 [case testSuperInInitSubclass]
261 class A:
262 def __init_subclass__(cls) -> None:
263 super().__init_subclass__()
264 [builtins fixtures/__init_subclass__.pyi]
265
266 [case testSuperClassGetItem]
267 from typing import TypeVar, Type, Any
268
269 T = TypeVar("T", bound="B")
270
271 class A:
272 def __class_getitem__(cls, item) -> None: pass
273
274 class B(A):
275 def __class_getitem__(cls: Type[T], item: Any) -> None:
276 super(B, cls).__class_getitem__(item)
277
278 -- Invalid uses of super()
279 -- -----------------------
280
281
282 [case testSuperOutsideMethodNoCrash]
283 class A:
284 x = 1
285 class B(A): pass
286 class C(B):
287 b = super(B, B).x
288 a = super().whatever # E: "super()" outside of a method is not supported
289
290 [case testSuperWithObjectClassAsFirstArgument]
291 class A:
292 def f(self) -> None:
293 super(object, self).f() # E: Target class has no base class
294
295 [case testSuperWithTypeVarAsFirstArgument]
296 from typing import TypeVar
297
298 T = TypeVar('T')
299
300 def f(obj: T) -> None:
301 super(obj.__class__, obj).f() # E: Target class has no base class
302 [builtins fixtures/__new__.pyi]
303
304 [case testSuperWithSingleArgument]
305 class B:
306 def f(self) -> None: pass
307 class C(B):
308 def __init__(self) -> None:
309 super(C).f() # E: "super" with a single argument not supported
310
311 [case testSuperWithThreeArguments]
312 class B:
313 def f(self) -> None: pass
314 class C(B):
315 def h(self) -> None:
316 super(C, self, 1).f() # E: Too many arguments for "super"
317
318 [case testSuperWithNonPositionalArguments]
319 class B:
320 def f(self) -> None: pass
321 class C(B):
322 def h(self) -> None:
323 super(C, x=self).f() # E: "super" only accepts positional arguments
324 super(**{}).f() # E: "super" only accepts positional arguments
325
326 [case testSuperWithVarArgs]
327 class B:
328 def f(self) -> None: pass
329 class C(B):
330 def h(self) -> None:
331 super(*(C, self)).f() # E: Varargs not supported with "super"
332
333 [case testInvalidSuperArg]
334 class B:
335 def f(self) -> None: pass
336 class C(B):
337 def h(self) -> None:
338 super(x, y).f # E: Name "x" is not defined # E: Name "y" is not defined
339
340 [case testTypeErrorInSuperArg]
341 class B:
342 def f(self) -> None: pass
343 class C(B):
344 def h(self) -> None:
345 super(1(), self).f # E: "int" not callable
346 super(C, ''()).f # E: "str" not callable
347
348 [case testFlippedSuperArgs]
349 class B:
350 def f(self) -> None: pass
351 class C(B):
352 def h(self) -> None:
353 super(self, C).f # E: Argument 1 for "super" must be a type object; got a non-type instance
354
355 [case testInvalidFirstSuperArg]
356 class B:
357 def f(self) -> None: pass
358 class C(B):
359 def h(self) -> None:
360 super(None, C).f # E: Argument 1 for "super" must be a type object; got "None"
361
362 [case testInvalidSecondArgumentToSuper]
363 class B:
364 def f(self) -> None: pass
365 class C(B):
366 def h(self) -> None:
367 super(C, 1).f # E: Argument 2 for "super" not an instance of argument 1
368 super(C, None).f # E: Unsupported argument 2 for "super"
369
370 [case testSuperInMethodWithNoArguments]
371 class A:
372 def f(self) -> None: pass
373 @staticmethod
374 def st() -> int:
375 return 1
376
377 class B(A):
378 def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument?
379 super().f() # E: "super()" requires one or two positional arguments in enclosing function
380 def h(self) -> None:
381 def a() -> None:
382 super().f() # E: "super()" requires one or two positional arguments in enclosing function
383 @staticmethod
384 def st() -> int:
385 reveal_type(super(B, B).st()) # N: Revealed type is "builtins.int"
386 super().st() # E: "super()" requires one or two positional arguments in enclosing function
387 return 2
388 [builtins fixtures/staticmethod.pyi]
389
390 [case testSuperWithUnsupportedTypeObject]
391 from typing import Type
392
393 class A:
394 def f(self) -> int: pass
395
396 class B(A):
397 def h(self, t: Type[None]) -> None:
398 super(t, self).f # E: Unsupported argument 1 for "super"
399
400 [case testSuperSelfTypeInstanceMethod]
401 from typing import TypeVar, Type
402
403 T = TypeVar("T", bound="A")
404
405 class A:
406 def foo(self: T) -> T: ...
407
408 class B(A):
409 def foo(self: T) -> T:
410 reveal_type(super().foo()) # N: Revealed type is "T`-1"
411 return super().foo()
412
413 [case testSuperSelfTypeClassMethod]
414 from typing import TypeVar, Type
415
416 T = TypeVar("T", bound="A")
417
418 class A:
419 @classmethod
420 def foo(cls: Type[T]) -> T: ...
421
422 class B(A):
423 @classmethod
424 def foo(cls: Type[T]) -> T:
425 reveal_type(super().foo()) # N: Revealed type is "T`-1"
426 return super().foo()
427 [builtins fixtures/classmethod.pyi]
428
429 [case testWrongSuperOutsideMethodNoCrash]
430 class B:
431 x: int
432 class C1(B): ...
433 class C2(B): ...
434 super(C1, C2).x # E: Argument 2 for "super" not an instance of argument 1