You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive when using Self as the return type with multiple base classes #17704
If you try to derive from multiple base classes that both define the same function returning Self, mypy incorrectly warns that the definitions are incompatible.
To Reproduce
# https://mypy-play.net/?mypy=latest&python=3.12&gist=e5d3734df862e8f3ada5c11c076cae57# main.pyfromtypingimportSelfclassMyBase1:
deffoo(self) ->Self:
returnselfclassMyBase2:
deffoo(self) ->Self:
returnselfclassMyDerived(MyBase1, MyBase2):
passMyBase1().foo() # Should be Valid. Has type MyBase1MyBase1().foo() # Should be Valid. Has type MyBase2MyDerived().foo() # Should be Valid. Has type MyDerived
Expected Behavior
Shouldn't report any errors. In the context of the line class MyDerived(MyBase1, MyBase2): both MyBase1 and MyBase2 returns Self aka MyDerived
Actual Behavior
$ mypy main.py
main.py:13: error: Definition of "foo" in base class "MyBase1" is incompatible with definition in base class "MyBase2" [misc]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Mypy version used: mypy 1.11.1 (compiled: yes)
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
fromtypingimportSelfclassMyBase1:
@classmethoddeffoo(cls) ->Self:
returncls()
classMyBase2:
@classmethoddeffoo(cls) ->Self:
returncls()
classMyDerived(MyBase1, MyBase2):
passMyBase1.foo() # Should be Valid. Has type MyBase1MyBase1.foo() # Should be Valid. Has type MyBase2MyDerived.foo() # Should be Valid. Has type MyDerived
Interestingly, mypy is also able to derive the correct type of MyDerived.foo() or MyDerived().foo() if you do reveal_type on them.
Activity
Cnoor0171 commentedon Aug 22, 2024
A similar problem if the methods are classmethods
Interestingly, mypy is also able to derive the correct type of
MyDerived.foo()orMyDerived().foo()if you doreveal_typeon them.sterliakov commentedon Jan 15, 2025
Missed this ticket - also resolved by #18465 and passes on current master.