-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive safe-super
errors for properties defined in protocol classes using inheritance
#14757
Comments
I think I might be able to add to this from abc import ABC, abstractmethod
from typing import Any
class Interface(ABC):
@abstractmethod
def method(self) -> Any:
pass
class Level1_1(Interface):
def method(self) -> str:
return ""
class Level1_2(Interface):
def method(self) -> str:
return ""
class Level2(Level1_1, Level1_2):
def method(self) -> str:
reveal_type(super(Level1_1, self).method()) # Line 23
reveal_type(super(Level1_2, self).method()) # Line 24
super(Level1_1, self).method()
super(Level1_2, self).method()
return "TEST" We get clone.py:23: note: Revealed type is builtins.str
clone.py:24: note: Revealed type is "Any"
Success: no issues found in 1 source file^ Which I believe should both be the |
Thanks @paperclipstudio. I believe mypy is actually correct here. The expression:
will call
will call |
Yes, Sorry my mistake, thanks for the help. |
I also want to add that this also happens even if we directly call a protocol's method in a class that inherits from that protocol.. _my_protocol.py
my_mixin.py
Error: |
Hi @ebram96 - I think mypy is correct in this case. Your |
I have a variant of this error for a mixin class using from typing import Protocol
class BaseClassProtocol(Protocol):
def some_method(self) -> str | None: ...
class SubClassProtocol(BaseClassProtocol, Protocol):
@property
def some_flag(self) -> str: ...
class MyMixin:
def some_method(self: SubClassProtocol) -> str | None:
if self.some_flag: # `self` type is defined to access this
return 'some_flag'
return super().some_method() # But this is flagged for `safe_super` Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=7129a6792261d89c8e3b414a6f12e0f8 |
The fix from #12344 / #14082 doesn't work to stop the from collections.abc import Callable
from typing import Protocol
class MixinProtocol(Protocol):
some_method: Callable
@property
def some_flag(self) -> str: ...
class MyMixin:
def some_method(self: MixinProtocol) -> str | None:
if self.some_flag:
return 'some_flag'
return super().some_method() # No error now! |
Temporarily ignore safe-super errors (may be python/mypy#14757).
Bug Report
Empty properties defined in a protocol class that is subclassed by another protocol are incorrectly classified by mypy as abstract methods - this can then result in a
safe-super
error if the parent property is accessed viasuper()
within a class that is type hinted with the child protocol, e.g. when type hintingself
in mixin classes (as described here).To Reproduce
Expected Behavior
No errors.
Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: