Debian Codegraph @

test-data/unit/check-multiple-inheritance.test

raw
1 -- Test cases for multiple inheritance.
2 --
3 -- Related: check-abstract.test
4
5
6 -- No name collisions
7 -- ------------------
8
9
10 [case testSimpleMultipleInheritanceAndMethods]
11 import typing
12 class A:
13 def f(self, x: int) -> None: pass
14 class B:
15 def g(self, x: str) -> None: pass
16 class C(A, B): pass
17 c = C()
18 c.f(1)
19 c.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
20 c.g('')
21 c.g(1) # E: Argument 1 to "g" of "B" has incompatible type "int"; expected "str"
22
23 [case testSimpleMultipleInheritanceAndMethods2]
24 import typing
25 class A:
26 def f(self, x: int) -> None: pass
27 class B:
28 def g(self, x): pass
29 class C(A, B): pass
30 c = C()
31 c.f(1)
32 c.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
33 c.g('')
34 c.g(1)
35
36 [case testSimpleMultipleInheritanceAndInstanceVariables]
37 import typing
38 class A:
39 def f(self) -> None:
40 self.x = 1
41 class B:
42 def g(self) -> None:
43 self.y = ''
44 class C(A, B): pass
45 c = C()
46 c.x = 1
47 c.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
48 c.y = ''
49 c.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
50
51 [case testSimpleMultipleInheritanceAndInstanceVariableInClassBody]
52 import typing
53 class A:
54 x = 1
55 class B:
56 y = ''
57 class C(A, B): pass
58 c = C()
59 c.x = 1
60 c.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
61 c.y = ''
62 c.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
63
64 [case testSimpleMultipleInheritanceAndClassVariable]
65 import typing
66 class A:
67 x = 1
68 class B:
69 y = ''
70 class C(A, B): pass
71 C.x = 1
72 C.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
73 C.y = ''
74 C.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
75
76
77 -- Name collisions
78 -- ---------------
79
80
81 [case testMethodNameCollisionInMultipleInheritanceWithValidSigs]
82 import typing
83 class A:
84 def f(self, x: int) -> None: pass
85 class B:
86 def f(self, x: int) -> None: pass
87 class C(A, B): pass
88 c = C()
89 c.f(1)
90 c.f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
91
92 [case testInstanceVarNameOverlapInMultipleInheritanceWithCompatibleTypes]
93 import typing
94 class A:
95 def f(self) -> None:
96 self.x = 1
97 class B:
98 def g(self) -> None:
99 self.x = 1
100 class C(A, B): pass
101 c = C()
102 c.x = 1
103 c.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
104
105 [case testClassVarNameOverlapInMultipleInheritanceWithCompatibleTypes]
106 import typing
107 class A:
108 x = 1
109 class B:
110 x = 1
111 class C(A, B): pass
112 c = C()
113 c.x = 1
114 c.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
115 C.x = 1
116 C.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
117
118 [case testMethodNameCollisionInMultipleInheritanceWithIncompatibleSigs]
119 import typing
120 class A:
121 def f(self, x: int) -> None: pass
122 class B:
123 def f(self, x: str) -> None: pass
124 class C(A, B): pass
125 [out]
126 main:6: error: Definition of "f" in base class "A" is incompatible with definition in base class "B"
127
128 [case testMethodNameCollisionInMultipleInheritanceWithIncompatibleSigs2]
129 import typing
130 class A:
131 def f(self, x: int) -> None: pass
132 class B:
133 def f(self, x, y): pass
134 class C(A, B): pass
135 class D(B, A): pass
136 [out]
137 main:6: error: Definition of "f" in base class "A" is incompatible with definition in base class "B"
138 main:7: error: Definition of "f" in base class "B" is incompatible with definition in base class "A"
139
140
141 [case testMethodOverridingWithBothDynamicallyAndStaticallyTypedMethods]
142 class A:
143 def f(self) -> int: pass
144 class B:
145 def f(self): pass
146 class C(B, A): pass
147 class D(A, B): pass
148 [out]
149
150 [case testInstanceVarNameOverlapInMultipleInheritanceWithInvalidTypes]
151 import typing
152 class A:
153 def f(self) -> None:
154 self.x = 1
155 class B:
156 def g(self) -> None:
157 self.x = ''
158 class C(A, B): pass
159 [out]
160 main:8: error: Definition of "x" in base class "A" is incompatible with definition in base class "B"
161
162 [case testClassVarNameOverlapInMultipleInheritanceWithInvalidTypes]
163 import typing
164 class A:
165 x = 1
166 class B:
167 x = ''
168 class C(A, B): pass
169 [out]
170 main:6: error: Definition of "x" in base class "A" is incompatible with definition in base class "B"
171
172 [case testMethodOverlapsWithClassVariableInMultipleInheritance]
173 from typing import Callable
174 class A:
175 def f(self) -> None: pass
176 class B:
177 f = ''
178 class C(A, B): pass
179 [out]
180 main:6: error: Definition of "f" in base class "A" is incompatible with definition in base class "B"
181
182 [case testMethodOverlapsWithInstanceVariableInMultipleInheritance]
183 from typing import Callable
184 class A:
185 def f(self) -> None: pass
186 class B:
187 def g(self) -> None:
188 self.f = ''
189 class C(A, B): pass
190 [out]
191 main:7: error: Definition of "f" in base class "A" is incompatible with definition in base class "B"
192
193 [case testMultipleInheritanceAndInit]
194 import typing
195 class A:
196 def __init__(self, x: int) -> None: pass
197 class B:
198 def __init__(self) -> None: pass
199 class C(A, B): pass
200
201 [case testMultipleInheritanceAndDifferentButCompatibleSignatures]
202 class A:
203 def clear(self): pass
204
205 class B:
206 def clear(self, x=None): pass
207
208 class C(B, A): pass
209 class D(A, B): pass
210 [out]
211 main:8: error: Definition of "clear" in base class "A" is incompatible with definition in base class "B"
212
213
214 -- Special cases
215 -- -------------
216
217
218 [case testGenericInheritanceAndOverridingWithMultipleInheritance]
219 from typing import Generic, TypeVar
220 T = TypeVar('T')
221 class G(Generic[T]):
222 def f(self, s: int) -> 'G[T]': pass
223 class A(G[int]):
224 def f(self, s: int) -> 'A': pass
225 class B(A, int): pass
226
227 [case testCannotDetermineTypeInMultipleInheritance]
228 from typing import Callable, TypeVar
229 T = TypeVar('T')
230 class A(B, C):
231 pass
232 class B:
233 @dec
234 def f(self): pass
235 class C:
236 @dec
237 def f(self): pass
238 def dec(f: Callable[..., T]) -> Callable[..., T]:
239 return f
240 [out]
241 main:3: error: Cannot determine type of "f" in base class "B"
242 main:3: error: Cannot determine type of "f" in base class "C"
243
244 [case testMultipleInheritance_NestedClassesWithSameName]
245 class Mixin1:
246 class Meta:
247 pass
248 class Mixin2:
249 class Meta:
250 pass
251 class A(Mixin1, Mixin2):
252 pass
253 [out]
254 main:7: error: Definition of "Meta" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
255
256 [case testMultipleInheritance_NestedClassesWithSameNameCustomMetaclass]
257 class Metaclass(type):
258 pass
259 class Mixin1:
260 class Meta(metaclass=Metaclass):
261 pass
262 class Mixin2:
263 class Meta(metaclass=Metaclass):
264 pass
265 class A(Mixin1, Mixin2):
266 pass
267 [out]
268 main:9: error: Definition of "Meta" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
269
270 [case testMultipleInheritance_NestedClassesWithSameNameOverloadedNew]
271 from mixins import Mixin1, Mixin2
272 class A(Mixin1, Mixin2):
273 pass
274 [file mixins.py]
275 class Mixin1:
276 class Meta:
277 pass
278 class Mixin2:
279 class Meta:
280 pass
281 [file mixins.pyi]
282 from typing import overload, Any, Mapping, Dict
283 class Mixin1:
284 class Meta:
285 @overload
286 def __new__(cls, *args, **kwargs: None) -> Mixin1.Meta:
287 pass
288 @overload
289 def __new__(cls, *args, **kwargs: Dict[str, Any]) -> Mixin1.Meta:
290 pass
291 class Mixin2:
292 class Meta:
293 pass
294 [builtins fixtures/dict.pyi]
295 [out]
296 main:2: error: Definition of "Meta" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
297
298 [case testMultipleInheritance_NestedClassAndAttrHaveSameName]
299 class Mixin1:
300 class Nested1:
301 pass
302 class Mixin2:
303 Nested1: str
304 class A(Mixin1, Mixin2):
305 pass
306 [out]
307 main:6: error: Definition of "Nested1" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
308
309 [case testMultipleInheritance_NestedClassAndFunctionHaveSameName]
310 class Mixin1:
311 class Nested1:
312 pass
313 class Mixin2:
314 def Nested1(self) -> str:
315 pass
316 class A(Mixin1, Mixin2):
317 pass
318 [out]
319 main:7: error: Definition of "Nested1" in base class "Mixin1" is incompatible with definition in base class "Mixin2"
320
321 [case testMultipleInheritance_NestedClassAndRefToOtherClass]
322 class Outer:
323 pass
324 class Mixin1:
325 class Nested1:
326 pass
327 class Mixin2:
328 Nested1 = Outer
329 class A(Mixin2, Mixin1):
330 pass
331 [out]
332 main:8: error: Definition of "Nested1" in base class "Mixin2" is incompatible with definition in base class "Mixin1"
333
334 [case testMultipleInheritance_ReferenceToSubclassesFromSameMRO]
335 class A:
336 def __init__(self, arg: str) -> None:
337 pass
338 class B(A):
339 pass
340 class Base1:
341 NestedVar = A
342 class Base2:
343 NestedVar = B
344 class Combo(Base2, Base1): ...
345 [out]
346
347 [case testMultipleInheritance_ReferenceToSubclassesFromSameMROCustomMetaclass]
348 class Metaclass(type):
349 pass
350 class A(metaclass=Metaclass):
351 pass
352 class B(A):
353 pass
354 class Base1:
355 NestedVar = A
356 class Base2:
357 NestedVar = B
358 class Combo(Base2, Base1): ...
359 [out]
360
361 [case testMultipleInheritance_ReferenceToSubclassesFromSameMROOverloadedNew]
362 from mixins import A, B
363 class Base1:
364 NestedVar = A
365 class Base2:
366 NestedVar = B
367 class Combo(Base2, Base1): ...
368 [file mixins.py]
369 class A:
370 pass
371 class B(A):
372 pass
373 [file mixins.pyi]
374 from typing import overload, Dict, Any
375 class A:
376 @overload
377 def __new__(cls, *args, **kwargs: None) -> A:
378 pass
379 @overload
380 def __new__(cls, *args, **kwargs: Dict[str, Any]) -> A:
381 pass
382 class B:
383 pass
384 [builtins fixtures/dict.pyi]
385 [out]
386 main:6: error: Definition of "NestedVar" in base class "Base2" is incompatible with definition in base class "Base1"
387
388 [case testMultipleInheritance_ReferenceToGenericClasses]
389 from typing import TypeVar, Generic
390 T = TypeVar('T')
391 class Generic1(Generic[T]):
392 pass
393 class Generic2(Generic[T]):
394 pass
395 class Base1:
396 Nested = Generic1
397 class Base2:
398 Nested = Generic2
399 class A(Base1, Base2):
400 pass
401 [out]
402 main:11: error: Definition of "Nested" in base class "Base1" is incompatible with definition in base class "Base2"
403
404 [case testMultipleInheritance_GenericSubclasses_SuperclassFirst]
405 from typing import TypeVar, Generic
406 T = TypeVar('T')
407 class ParentGeneric(Generic[T]):
408 pass
409 class ChildGeneric(ParentGeneric[T]):
410 pass
411 class Base1:
412 Nested = ParentGeneric
413 class Base2:
414 Nested = ChildGeneric
415 class A(Base1, Base2):
416 pass
417 [out]
418 main:11: error: Definition of "Nested" in base class "Base1" is incompatible with definition in base class "Base2"
419
420 [case testMultipleInheritance_GenericSubclasses_SubclassFirst]
421 from typing import TypeVar, Generic
422 T = TypeVar('T')
423 class ParentGeneric(Generic[T]):
424 pass
425 class ChildGeneric(ParentGeneric[T]):
426 pass
427 class Base1:
428 Nested = ParentGeneric
429 class Base2:
430 Nested = ChildGeneric
431 class A(Base2, Base1):
432 pass
433 [out]
434
435 [case testMultipleInheritance_RefersToNamedTuples]
436 from typing import NamedTuple
437 class NamedTuple1:
438 attr1: int
439 class NamedTuple2:
440 attr2: int
441 class Base1:
442 Nested = NamedTuple1
443 class Base2:
444 Nested = NamedTuple2
445 class A(Base1, Base2):
446 pass
447 [out]
448 main:10: error: Definition of "Nested" in base class "Base1" is incompatible with definition in base class "Base2"
449
450 [case testMultipleInheritance_NestedVariableRefersToSuperlassUnderSubclass]
451 class A:
452 def __init__(self, arg: str) -> None:
453 pass
454 class B(A):
455 pass
456 class Base1:
457 NestedVar = B
458 class Base2:
459 NestedVar = A
460 class Combo(Base2, Base1): ...
461 [out]
462 main:10: error: Definition of "NestedVar" in base class "Base2" is incompatible with definition in base class "Base1"
463
464 [case testMultipleInheritance_NestedVariableOverriddenWithCompatibleType]
465 from typing import TypeVar, Generic
466 T = TypeVar('T', covariant=True)
467 class GenericBase(Generic[T]):
468 pass
469 class Base1:
470 Nested: GenericBase['Base1']
471 class Base2:
472 Nested: GenericBase['Base2']
473 class A(Base1, Base2):
474 Nested: GenericBase['A']
475 [out]
476
477 [case testMultipleInheritance_NestedVariableOverriddenWithIncompatibleType1]
478 from typing import TypeVar, Generic
479 T = TypeVar('T', covariant=True)
480 class GenericBase(Generic[T]):
481 pass
482 class Base1:
483 Nested: GenericBase['Base1']
484 class Base2:
485 Nested: GenericBase['Base2']
486 class A(Base1, Base2):
487 Nested: GenericBase['Base1']
488 [out]
489 main:10: error: Incompatible types in assignment (expression has type "GenericBase[Base1]", base class "Base2" defined the type as "GenericBase[Base2]")
490
491 [case testMultipleInheritance_NestedVariableOverriddenWithIncompatibleType2]
492 from typing import TypeVar, Generic
493 T = TypeVar('T', covariant=True)
494 class GenericBase(Generic[T]):
495 pass
496 class Base1:
497 Nested: GenericBase['Base1']
498 class Base2:
499 Nested: GenericBase['Base2']
500 class A(Base1, Base2):
501 Nested: GenericBase['Base2']
502 [out]
503 main:10: error: Incompatible types in assignment (expression has type "GenericBase[Base2]", base class "Base1" defined the type as "GenericBase[Base1]")
504
505 [case testMultipleInheritance_NestedVariableOverriddenWithCompatibleType2]
506 from typing import TypeVar, Generic
507 T = TypeVar('T', covariant=True)
508 class GenericBase(Generic[T]):
509 pass
510 class Base1:
511 Nested: GenericBase['Base1']
512 class Base2:
513 Nested: GenericBase['Base1']
514 class A(Base1, Base2):
515 Nested: GenericBase['Base1']
516 [out]
517
518 [case testMultipleInheritance_MethodDefinitionsCompatibleWithOverride]
519 from typing import TypeVar, Union
520 _T = TypeVar('_T')
521
522 class Flag:
523 def __or__(self: _T, other: _T) -> _T: ...
524
525 # int defines __or__ as:
526 # def __or__(self, n: int) -> int: ...
527 class IntFlag(int, Flag):
528 def __or__(self: _T, other: Union[int, _T]) -> _T: ...
529 [out]
530
531 [case testMultipleInheritance_MethodDefinitionsIncompatibleOverride]
532 from typing import TypeVar, Union
533 _T = TypeVar('_T')
534
535 class Flag:
536 def __or__(self: _T, other: _T) -> _T: ...
537
538 class IntFlag(int, Flag):
539 def __or__(self: _T, other: str) -> _T: ...
540 [out]
541 main:8: error: Argument 1 of "__or__" is incompatible with supertype "Flag"; supertype defines the argument type as "IntFlag"
542 main:8: note: This violates the Liskov substitution principle
543 main:8: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
544
545 [case testMultipleInheritance_MethodDefinitionsCompatibleNoOverride]
546 from typing import TypeVar, Union
547 _T = TypeVar('_T')
548
549 class Flag:
550 def __or__(self: _T, other: _T) -> _T: ...
551
552 class IntFlag(int, Flag):
553 pass
554 [out]
555
556 [case testMultipleInheritance_MethodsReturningSelfCompatible]
557 class A(object):
558 def x(self) -> 'A':
559 return self
560
561 class B(object):
562 def x(self) -> 'B':
563 return self
564
565 class C(A, B):
566 def x(self) -> 'C':
567 return self
568
569 [case testMultipleInheritance_MethodsReturningSelfIncompatible]
570 class A(object):
571 def x(self) -> 'A':
572 return self
573
574 class B(object):
575 def x(self) -> 'B':
576 return self
577
578 class C(A, B): # E: Definition of "x" in base class "A" is incompatible with definition in base class "B"
579 pass
580
581 [case testNestedVariableRefersToSubclassOfAnotherNestedClass]
582 class Mixin1:
583 class Meta:
584 pass
585 class Outer(Mixin1.Meta):
586 pass
587 class Mixin2:
588 NestedVar = Outer
589 class Combo(Mixin2, Mixin1): ...
590 [out]
591
592 [case testNestedVariableRefersToCompletelyDifferentClasses]
593 class A:
594 pass
595 class B:
596 pass
597 class Base1:
598 NestedVar = A
599 class Base2:
600 NestedVar = B
601 class Combo(Base2, Base1): ...
602 [out]
603 main:9: error: Definition of "NestedVar" in base class "Base2" is incompatible with definition in base class "Base1"
604
605 [case testDoNotFailIfBothNestedClassesInheritFromAny]
606 from typing import Any
607 class Mixin1:
608 class Meta(Any):
609 pass
610 class Mixin2:
611 class Meta(Any):
612 pass
613 class A(Mixin1, Mixin2):
614 pass
615 [out]
616
617 [case testDoNotFailIfOneOfNestedClassesIsOuterInheritedFromAny]
618 from typing import Any
619 class Outer(Any):
620 pass
621 class Mixin1:
622 Meta = Outer
623 class Mixin2:
624 class Meta(Any):
625 pass
626 class A(Mixin1, Mixin2):
627 pass
628 [out]
629
630 [case testGenericMultipleOverrideRemap]
631 from typing import TypeVar, Generic, Tuple
632
633 K = TypeVar('K')
634 V = TypeVar('V')
635 T = TypeVar('T')
636
637 class ItemsView(Generic[K, V]):
638 def __iter__(self) -> Tuple[K, V]: ...
639
640 class Sequence(Generic[T]):
641 def __iter__(self) -> T: ...
642
643 # Override compatible between bases.
644 class OrderedItemsView(ItemsView[K, V], Sequence[Tuple[K, V]]):
645 def __iter__(self) -> Tuple[K, V]: ...
646
647 class OrderedItemsViewDirect(ItemsView[K, V], Sequence[Tuple[K, V]]):
648 pass
649 [builtins fixtures/tuple.pyi]
650
651 [case testGenericMultipleOverrideReplace]
652 from typing import TypeVar, Generic, Union
653
654 T = TypeVar('T')
655
656 class A(Generic[T]):
657 def foo(self, x: T) -> None: ...
658
659 class B(A[T]): ...
660
661 class C1:
662 def foo(self, x: str) -> None: ...
663
664 class C2:
665 def foo(self, x: Union[str, int]) -> None: ...
666
667 class D1(B[str], C1): ...
668 class D2(B[Union[int, str]], C2): ...
669 class D3(C2, B[str]): ...
670 class D4(B[str], C2): ... # E: Definition of "foo" in base class "A" is incompatible with definition in base class "C2"
671
672
673 [case testMultipleInheritanceOverridingOfFunctionsWithCallableInstances]
674 from typing import Any, Callable
675
676 def dec1(f: Callable[[Any, int], None]) -> Callable[[Any, int], None]: ...
677
678 class F:
679 def __call__(self, x: int) -> None: ...
680
681 def dec2(f: Callable[[Any, int], None]) -> F: ...
682
683 class B1:
684 def f(self, x: int) -> None: ...
685
686 class B2:
687 @dec1
688 def f(self, x: int) -> None: ...
689
690 class B3:
691 @dec2
692 def f(self, x: int) -> None: ...
693
694 class B4:
695 f = F()
696
697 class C12(B1, B2): ...
698 class C13(B1, B3): ... # E: Definition of "f" in base class "B1" is incompatible with definition in base class "B3"
699 class C14(B1, B4): ... # E: Definition of "f" in base class "B1" is incompatible with definition in base class "B4"
700 class C21(B2, B1): ...
701 class C23(B2, B3): ... # E: Definition of "f" in base class "B2" is incompatible with definition in base class "B3"
702 class C24(B2, B4): ... # E: Definition of "f" in base class "B2" is incompatible with definition in base class "B4"
703 class C31(B3, B1): ...
704 class C32(B3, B2): ...
705 class C34(B3, B4): ...
706 class C41(B4, B1): ...
707 class C42(B4, B2): ...
708 class C43(B4, B3): ...
709
710 [case testMultipleInheritanceExplicitDiamondResolution]
711 # Adapted from #14279
712 class A:
713 class M:
714 pass
715
716 class B0(A):
717 class M(A.M):
718 pass
719
720 class B1(A):
721 class M(A.M):
722 pass
723
724 class C(B0,B1):
725 class M(B0.M, B1.M):
726 pass
727
728 class D0(B0):
729 pass
730 class D1(B1):
731 pass
732
733 class D(D0,D1,C):
734 pass