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
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
The text was updated successfully, but these errors were encountered:
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"
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
Expected Behavior
No erased type errors?
Actual Behavior
See inline comments.
Your Environment
mypy.ini
(and other config files): -The text was updated successfully, but these errors were encountered: