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
Overloads in super-class for sub-class specific return types #11759
Bug Report
It is probably not good coding style to implement sub-class specific behavior in a super-class, but it would be great to still be able to write type annotations for it.
The sub-class specific overloads in the example below make mypy and pyright correctly infer the return type. Unfortunately, mypy throws an error for the two sub-class specific overloads. Are these false-positives?
To Reproduce
from __future__ importannotationsfromtypingimportoverloadclassBase:
@overloaddefany(self: A) ->bool: # mypy: The erased type of self "test.A" is not a supertype of its class "test.Base"
...
@overloaddefany(self: B) ->str: # mypy: The erased type of self "test.B" is not a supertype of its class "test.Base"
...
@overloaddefany(self) ->bool|str:
...
defany(self) ->bool|str:
# implementation that handles sub-classes differently
...
classA(Base):
...
classB(Base):
...
# mypy&pyright: bool | strreveal_type(Base().any())
# mypy&pyright: boolreveal_type(A().any())
# mypy&pyright: strreveal_type(B().any())
Expected Behavior
No erased type errors?
Actual Behavior
See inline comments.
Your Environment
Mypy version used: 0.910
Mypy command-line flags: -
Mypy configuration options from mypy.ini (and other config files): -
Python version used: 3.9.6
Operating system and version: 3.10.0-693.5.2.el7.x86_64
Is this feature really necessary? You can achieve the same thing just by doing this:
from __future__ importannotationsfromtypingimportCallableclassBase:
defany(self) ->bool|str:
# implementation that handles sub-classes differently
...
classA(Base):
any: Callable[[A], bool]
classB(Base):
any: Callable[[B], str]
reveal_type(Base().any()) # Revealed type is "Union[builtins.bool, builtins.str]"reveal_type(A().any()) # Revealed type is "builtins.bool"reveal_type(B().any()) # Revealed type is "builtins.str"
Activity
AlexWaygood commentedon Mar 31, 2022
Is this feature really necessary? You can achieve the same thing just by doing this:
Mypy has no complaints, and it's even more concise.
twoertwein commentedon Mar 31, 2022
Thank you @AlexWaygood, I like it!