-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Further improve return types in the numbers
module
#11371
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 and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: pytest (https://github.com/pytest-dev/pytest)
- src/_pytest/python_api.py:453: error: Argument 1 to "abs" has incompatible type "SupportsComplex"; expected "SupportsAbs[Never]" [arg-type]
ibis (https://github.com/ibis-project/ibis)
- ibis/util.py:168: error: Unsupported left operand type for < (Never) [operator]
+ ibis/util.py:168: error: Unsupported left operand type for < ("_RealLike") [operator]
- ibis/util.py:168: error: Argument 1 to "abs" has incompatible type "SupportsComplex"; expected "SupportsAbs[Never]" [arg-type]
|
# as subtypes of _RealLike have a `__complex__` method | ||
# (e.g. `builtins.int.__complex__` does not exist) | ||
class _RealLike(Protocol): | ||
def __neg__(self) -> _ComplexLike: ... # TODO: ideally would be more precise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you make these two return RealLike?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried doing this:
--- a/stdlib/numbers.pyi
+++ b/stdlib/numbers.pyi
@@ -41,8 +41,8 @@ else:
# as subtypes of _RealLike have a `__complex__` method
# (e.g. `builtins.int.__complex__` does not exist)
class _RealLike(Protocol):
- def __neg__(self) -> _ComplexLike: ... # TODO: ideally would be more precise
- def __pos__(self) -> _ComplexLike: ... # TODO: ideally would be more precise
+ def __neg__(self) -> _RealLike: ...
+ def __pos__(self) -> _RealLike: ...
def __abs__(self) -> _RealLike: ...
def __float__(self) -> float: ...
def __trunc__(self) -> _IntegralLike: ...
@@ -143,6 +143,12 @@ class Real(Complex):
@property
def imag(self) -> Literal[0]: ...
def conjugate(self) -> _RealLike: ... # type: ignore[override]
+ # Not actually overridden at runtime,
+ # but we override these in the stub to give them more precise return types:
+ @abstractmethod
+ def __pos__(self) -> _RealLike: ...
+ @abstractmethod
+ def __neg__(self) -> _RealLike: ...
But on 3.12, mypy complains:
stdlib\numbers.pyi:149: error: Return type "_RealLike" of "__pos__" incompatible with return type "_SupportsComplex" in supertype "Complex" [override]
stdlib\numbers.pyi:151: error: Return type "_RealLike" of "__neg__" incompatible with return type "_SupportsComplex" in supertype "Complex" [override]
And on 3.8, mypy complains with:
stdlib\numbers.pyi:149: error: Return type "_RealLike" of "__pos__" incompatible with return type "Union[_SupportsComplex, complex]" in supertype "Complex" [override]
stdlib\numbers.pyi:151: error: Return type "_RealLike" of "__neg__" incompatible with return type "Union[_SupportsComplex, complex]" in supertype "Complex" [override]
I think this is because _RealLike
does not have a __complex__
method, so isn't seen as a subtype of _SupportsComplex
. (It can't have a __complex__
method, because int
doesn't have a __complex__
method, and we need int
to be understood as a subtype of RealLike
.)
This is yet another way in which these ABCs are subtly broken, unfortunately. Another approach might be to return SupportsAbs[_RealLike] | complex
(instead of _SupportsComplex | complex
) from most of the numbers.Complex
methods.
Closing in favour of #11375 |
Followup to #11353 (fixes 2/3 new errors in #11353 (comment))