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