| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
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]
|
Sorry, something went wrong.
| # 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.
Why don't you make these two return RealLike?
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Followup to #11353 (fixes 2/3 new errors in #11353 (comment))