Conversation
srittau
commented
Jan 31, 2022
- 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]
|