Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4316,8 +4316,18 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
mro = e.info.mro
index = mro.index(type_info)
if index is None:
self.chk.fail(message_registry.SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1, e)
return AnyType(TypeOfAny.from_error)
if (
instance_info.is_protocol
and instance_info != type_info
and not type_info.is_protocol
):
# A special case for mixins, in this case super() should point
# directly to the host protocol, this is not safe, since the real MRO
# is not known yet for mixin, but this feature is more like an escape hatch.
index = -1
else:
self.chk.fail(message_registry.SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1, e)
return AnyType(TypeOfAny.from_error)

if len(mro) == index + 1:
self.chk.fail(message_registry.TARGET_CLASS_HAS_NO_BASE_CLASS, e)
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,26 @@ reveal_type(f.copy()) # N: Revealed type is "__main__.File"
b.copy() # E: Invalid self argument "Bad" to attribute function "copy" with type "Callable[[T], T]"
[builtins fixtures/tuple.pyi]

[case testMixinProtocolSuper]
from typing import Protocol

class Base(Protocol):
def func(self) -> int:
...

class TweakFunc:
def func(self: Base) -> int:
return reveal_type(super().func()) # N: Revealed type is "builtins.int"

class Good:
def func(self) -> int: ...
class C(TweakFunc, Good): pass
C().func() # OK

class Bad:
def func(self) -> str: ...
class CC(TweakFunc, Bad): pass # E: Definition of "func" in base class "TweakFunc" is incompatible with definition in base class "Bad"

[case testBadClassLevelDecoratorHack]
from typing_extensions import Protocol
from typing import TypeVar, Any
Expand Down