-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Self
as return type annotation in base class method breaks multi-inheritance
#14640
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
Comments
I guess the issue is that the bounds are different? |
@hauntsaninja I guess code with So if this code works as expected: import copy
from typing import TypeVar
_Self = TypeVar("_Self")
class Cloneable:
def clone(self: _Self) -> _Self:
return copy.copy(self)
class Immutable:
def clone(self: _Self) -> _Self:
return self
class Collection(Cloneable):
pass
class Tuple(Immutable, Collection):
pass Then the code below should work too: import copy
from typing import Self
class Cloneable:
def clone(self) -> Self:
return copy.copy(self)
class Immutable:
def clone(self) -> Self:
return self
class Collection(Cloneable):
pass
class Tuple(Immutable, Collection):
pass I didn't find anything related to this in PEP 673, but Please, correct me if I missed smth. |
For the record, the from typing import Self
class Base1:
def foo(self) -> Self:
return self
class Base2:
def foo(self) -> Self:
return self
class Sub(Base1, Base2): # error: Definition of "foo" in base class "Base1" is incompatible with definition in base class "Base2" [misc]
pass (mypy 1.11.2) Real-life example: python/typeshed#12788, where peewee makes use of that pattern. |
@hauntsaninja this is one more ticket resolved by #18465 - sorry again, I should have searched for them more thoroughly:( |
No need to apologise, thank you for fixing so many things! |
Bug Report
Self
as return type annotation in base class method breaks multi-inheritance.(A clear and concise description of what the bug is.)
To Reproduce
Expected Behavior
No errors
Actual Behavior
Same code using old approach woks as expected:
pyright
doesn't raise an error.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: