Closed
Description
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
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
Expected Behavior
No errors
Actual Behavior
error: Definition of "clone" in base class "Immutable" is incompatible with definition in base class "Cloneable" [misc]
Same code using old approach woks 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
pyright
doesn't raise an error.
Your Environment
- Mypy version used: 1.0.0
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini
(and other config files): - Python version used: 3.11