-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve protocol return types #7093
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
- Dunder comparisons must return bool.
- write() return type should be ignored.
* Dunder comparisons must return bool. * write() return type should be ignored.
FWIW, the reason I annotated the dunder comparison functions as returning |
I believe that's the case when the comparison operator are used for non-comparison purposes? For example, like pathlib uses the |
This comment has been minimized.
This comment has been minimized.
I don't think so; I think the issue is that the runtime doesn't care what these methods return, as long as it's something that |
I feel that this exactly one of the cases that type checkers should catch. While of course everything is "boolean-like", returning arbitrary non-bools from those dunders seems error-prone. I am not making sense of the typing of numpy, but the documentation for |
Also, it's just one primer hit, despite the length of the output indicating something different. |
Makes sense! |
|
Yes, in numpy all array operations do elementwise work where possible, so However, we use these protocols both for functions like |
Splitting them sounds like a good idea. It would make most sense to me to keep the protocols in the PR as they are in the PR, and add custom protocols to |
Let's add a short comment in |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Primer looks good now. |
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
- pandas/core/indexes/base.py:4053: error: Argument 1 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]" [arg-type]
+ pandas/core/indexes/base.py:4053: error: Argument 1 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[_SupportsDunderLE, _SupportsDunderGE, _SupportsDunderGT, _SupportsDunderLT]" [arg-type]
- pandas/core/indexes/base.py:4053: error: Argument 2 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]" [arg-type]
+ pandas/core/indexes/base.py:4053: error: Argument 2 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[_SupportsDunderLE, _SupportsDunderGE, _SupportsDunderGT, _SupportsDunderLT]" [arg-type]
- pandas/io/formats/style.py:3709: error: Argument 2 to "ge" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]" [arg-type]
+ pandas/io/formats/style.py:3709: error: Argument 2 to "ge" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[_SupportsDunderLE, _SupportsDunderGE, _SupportsDunderGT, _SupportsDunderLT]" [arg-type]
- pandas/io/formats/style.py:3714: error: Argument 2 to "le" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[SupportsDunderLE, SupportsDunderGE, SupportsDunderGT, SupportsDunderLT]" [arg-type]
+ pandas/io/formats/style.py:3714: error: Argument 2 to "le" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[_SupportsDunderLE, _SupportsDunderGE, _SupportsDunderGT, _SupportsDunderLT]" [arg-type]
|