FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Further improve return types in the `numbers` module by AlexWaygood · Pull Request #11371 · python/typeshed · GitHub

Repository navigation

Further improve return types in the numbers module - #11371

Closed
AlexWaygood wants to merge 6 commits into
python:mainfrom
AlexWaygood:numbers-returns
Closed

AlexWaygood wants to merge 6 commits into
python:mainfrom
AlexWaygood:numbers-returns

Conversation

AlexWaygood commented Feb 6, 2024 •
edited
Loading

Copy link
Copy Markdown
Member

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 marked this pull request as ready for review February 6, 2024 16:05

This comment has been minimized.

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

This comment has been minimized.

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

github-actions Bot commented Feb 6, 2024

Copy link
Copy Markdown
Contributor

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]

Comment thread stdlib/numbers.pyi
# 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
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why don't you make these two return RealLike?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

Closing in favour of #11375

AlexWaygood closed this Feb 7, 2024
AlexWaygood deleted the numbers-returns branch February 7, 2024 10:33
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
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


Back | FazBrowse Home | New Git URL