Allow union-with-callable attributes to be overridden by methods by brianschubert · Pull Request #18018 · python/mypy (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation10 Commits7 Checks18 Files changed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
Fixes #12569
Before
from typing import Callable
class Parent: f1: Callable[[str], str] f2: Callable[[str], str] | str
class Child(Parent): def f1(self, x: str) -> str: return x # ok def f2(self, x: str) -> str: return x # Signature of "f2" incompatible with supertype "Parent"
After
from typing import Callable
class Parent: f1: Callable[[str], str] f2: Callable[[str], str] | str
class Child(Parent): def f1(self, x: str) -> str: return x # ok def f2(self, x: str) -> str: return x # ok
This comment has been minimized.
This comment has been minimized.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diff from mypy_primer, showing the effect of this PR on open source code:
packaging (https://github.com/pypa/packaging)
- src/packaging/specifiers.py:248: error: Unused "type: ignore" comment [unused-ignore]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/models.py:239: error: Signature of "url" incompatible with supertype "_IOMixin" [override]
- steam/models.py:239: note: Superclass:
- steam/models.py:239: note: str | _ReadOnlyProto[str]
- steam/models.py:239: note: Subclass:
- steam/models.py:239: note: str
- steam/reaction.py:236: error: Signature of "url" incompatible with supertype "_IOMixin" [override]
- steam/reaction.py:236: note: Superclass:
- steam/reaction.py:236: note: str | _ReadOnlyProto[str]
- steam/reaction.py:236: note: Subclass:
- steam/reaction.py:236: note: str
- steam/reaction.py:273: error: Signature of "url" incompatible with supertype "_IOMixin" [override]
- steam/reaction.py:273: note: Superclass:
- steam/reaction.py:273: note: str | _ReadOnlyProto[str]
- steam/reaction.py:273: note: Subclass:
- steam/reaction.py:273: note: str
- steam/reaction.py:312: error: Signature of "url" incompatible with supertype "_IOMixin" [override]
- steam/reaction.py:312: note: Superclass:
- steam/reaction.py:312: note: str | _ReadOnlyProto[str]
- steam/reaction.py:312: note: Subclass:
- steam/reaction.py:312: note: str
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I kind of want to try ignore_pos_arg_names=False
. This is something we probably want to eventually do anyway and should be fairly harmless in this case. But it's likely wiser to be consistent.
I was thinking about this PR some more and I think it introduces some unsoundness. We should have a Liskov error in the following:
import typing
class X:
@property
def x(self) -> typing.Optional[bool]: ...
@x.setter
def x(self, value: typing.Optional[bool]) -> None: ...
class Y(X):
@property
def x(self) -> bool: ...
@x.setter
def x(self, value: bool) -> None: ...
Input to setter should be wider, output of getter should be narrower.
Hmm, I think that setup is currently caught by mutable-override, is it not? Is the behavior after this PR different from what mypy does for this setup?
class X: x: bool | None
class Y(X): x: bool
Oh looks like I must have messed up my test. It does error with mutable-override, sorry for the noise!
I think what happened is I messed up the branches I tested. I think I probably did this, which mypy doesn't error on:
class AA:
@property
def a(self) -> int: ...
@a.setter
def a(self, value: int | str) -> None: ...
class BB(AA):
@property
def a(self) -> int: ...
@a.setter
def a(self, value: int) -> None: ...
This would be unsound, except that mypy doesn't support this at all and will error if you attempt to set a
to str. This is the (very popular) issue for that: #3004