File tree 2 files changed +32
-2
lines changed 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -4321,8 +4321,18 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
4321
4321
mro = e .info .mro
4322
4322
index = mro .index (type_info )
4323
4323
if index is None :
4324
- self .chk .fail (message_registry .SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1 , e )
4325
- return AnyType (TypeOfAny .from_error )
4324
+ if (
4325
+ instance_info .is_protocol
4326
+ and instance_info != type_info
4327
+ and not type_info .is_protocol
4328
+ ):
4329
+ # A special case for mixins, in this case super() should point
4330
+ # directly to the host protocol, this is not safe, since the real MRO
4331
+ # is not known yet for mixin, but this feature is more like an escape hatch.
4332
+ index = - 1
4333
+ else :
4334
+ self .chk .fail (message_registry .SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1 , e )
4335
+ return AnyType (TypeOfAny .from_error )
4326
4336
4327
4337
if len (mro ) == index + 1 :
4328
4338
self .chk .fail (message_registry .TARGET_CLASS_HAS_NO_BASE_CLASS , e )
Original file line number Diff line number Diff line change @@ -792,6 +792,26 @@ reveal_type(f.copy()) # N: Revealed type is "__main__.File"
792
792
b.copy() # E: Invalid self argument "Bad" to attribute function "copy" with type "Callable[[T], T]"
793
793
[builtins fixtures/tuple.pyi]
794
794
795
+ [case testMixinProtocolSuper]
796
+ from typing import Protocol
797
+
798
+ class Base(Protocol):
799
+ def func(self) -> int:
800
+ ...
801
+
802
+ class TweakFunc:
803
+ def func(self: Base) -> int:
804
+ return reveal_type(super().func()) # N: Revealed type is "builtins.int"
805
+
806
+ class Good:
807
+ def func(self) -> int: ...
808
+ class C(TweakFunc, Good): pass
809
+ C().func() # OK
810
+
811
+ class Bad:
812
+ def func(self) -> str: ...
813
+ class CC(TweakFunc, Bad): pass # E: Definition of "func" in base class "TweakFunc" is incompatible with definition in base class "Bad"
814
+
795
815
[case testBadClassLevelDecoratorHack]
796
816
from typing_extensions import Protocol
797
817
from typing import TypeVar, Any
You can’t perform that action at this time.
0 commit comments