Skip to content

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

Closed
wants to merge 6 commits into from

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Feb 6, 2024

Followup to #11353 (fixes 2/3 new errors in #11353 (comment))

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as ready for review February 6, 2024 16:05

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as draft February 6, 2024 16:15

This comment has been minimized.

@AlexWaygood AlexWaygood marked this pull request as ready for review February 6, 2024 16:35
Copy link
Contributor

github-actions bot commented Feb 6, 2024

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
Copy link
Member

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?

Copy link
Member Author

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.

@AlexWaygood
Copy link
Member Author

Closing in favour of #11375

@AlexWaygood AlexWaygood closed this Feb 7, 2024
@AlexWaygood AlexWaygood deleted the numbers-returns branch February 7, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants