Repository navigation
Fully type _operator module - #16014
Conversation
This comment has been minimized.
This comment has been minimized.
|
This is a lot of changes and the mypy-primer output is not clearly positive. These changes may be correct in principle but it's not clear they all help users. Could you make smaller PRs and if mypy-primer shows any changes, write a comment showing that the change is positive? |
|
I struggle to see why they couldn't help users. As of today the use of operator module is encouraged by populars linters such as Ruff when it can replace lambdas, but some basic function are not typed, defeating all their purpose on strict type checking |
Sorry, but I'm not sure what you mean. Besides, some of my changes are interdependent. I think I can still split them up, it would just be a Herculean task. |
This comment has been minimized.
This comment has been minimized.
|
mypy-primer shows that this change will introduce new type errors into a number of projects. Is that a good thing? |
|
Certainly not, but as I said in the previous iteration of this PR, I am unable to understand most of those errors and have little idea how to fix them. It would be much appreciated if I could get some guidance on this. |
This comment has been minimized.
This comment has been minimized.
|
I use We are choosing to not merge because @jonathandung "only" knows how to write good stubs and doesn't know how to navigate the illogical politics of the Python ecosystem. Full stop. There are no other reasons for not merging. There are many effects of not merging, but most of those effects are invisible to the Pythonati, so they are ignored. If we know the merge will affect pandas and scipy and we don't want to ruffle their feathers, then use the automated system that identifies the "victims" of this merge to automatically open Issues in the repos of the victims warning them of the impending destruction of their fragile project due to the marge of patch-5 by @jonathandung. |
This comment has been minimized.
This comment has been minimized.
srittau
left a comment
There was a problem hiding this comment.
I think this change is worth it overall, since it finally adds types to this module. Most primer hits are situations where multiple operators are used, for example like this:
dict = {"+": add, "-": sub}
# or
if is_bool:
op = operator.xor
else:
op = operator.subThis will need explicit annotations. Not great, but also not terrible.
One genuine problem below, though.
| @overload | ||
| def iand(a: _T_contra, b: _SupportsRAnd[_T_contra, _T_co], /) -> _T_co: ... | ||
|
|
||
| def iconcat(a: MutableSequence[_T], b: Sequence[_T], /) -> MutableSequence[_T]: ... # currently impossible to type more precisely |
There was a problem hiding this comment.
The kornia primer hit shows a problem with this. I think we should try the following:
_R_co = TypeVar("_R_co", contravariant=True)
class _SupportsIConcat(Protocol[_T_contra, _R_co]):
def __iadd__(self, value: _T_contra, /) -> _R_co: ...
def __getitem__(self, key: Any) -> Any: ... # method needs only to exists
# Currently impossible to type more precisely.
# Ideally this would be typed as (a: _IConcatList[_T], b: _T) -> _IConcatList[_T].
def iconcat(a: _SupportsIConcat[_T, _R], b: _T, /) -> _R: ... (Move the protocol and type var up.)
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Sebastian Rittau <sebastian.rittau@zfutura.de>
|
Diff from mypy_primer, showing the effect of this PR on open source code: colour (https://github.com/colour-science/colour)
+ colour/io/luts/lut.py:642: error: Cannot call function of unknown type [operator]
+ colour/continuous/signal.py:1203: error: Cannot call function of unknown type [operator]
+ colour/continuous/signal.py:1207: error: Cannot call function of unknown type [operator]
+ colour/continuous/multi_signals.py:1531: error: Cannot call function of unknown type [operator]
+ colour/continuous/multi_signals.py:1535: error: Cannot call function of unknown type [operator]
core (https://github.com/home-assistant/core)
+ homeassistant/components/python_script/helpers.py:127: error: Cannot call function of unknown type [operator]
pandas (https://github.com/pandas-dev/pandas)
- pandas/core/roperator.py:56: error: Call to untyped function "and_" in typed context [no-untyped-call]
- pandas/core/roperator.py:60: error: Call to untyped function "or_" in typed context [no-untyped-call]
- pandas/core/roperator.py:64: error: Call to untyped function "xor" in typed context [no-untyped-call]
+ pandas/core/computation/ops.py:346: error: Need type annotation for "_binary_ops_dict" (hint: "_binary_ops_dict: dict[<type>, <type>] = ...") [var-annotated]
+ pandas/core/algorithms.py:1617: error: Incompatible types in assignment (expression has type overloaded function, variable has type overloaded function) [assignment]
+ pandas/core/indexes/datetimes.py:2515: error: Incompatible types in assignment (expression has type overloaded function, variable has type overloaded function) [assignment]
egglog-python (https://github.com/egraphs-good/egglog-python)
+ python/tests/param_eq/evaluation.py:15: error: Dict entry 4 has incompatible type "type[Pow]": overloaded function; expected "type[operator]": "Callable[[float, float], float]" [dict-item]
ibis (https://github.com/ibis-project/ibis)
+ ibis/util.py:268: error: Incompatible types in assignment (expression has type "overloaded function | overloaded function", variable has type overloaded function) [assignment]
+ ibis/expr/api.py:746: error: Argument 1 to "reduce" has incompatible type overloaded function; expected "Callable[[BooleanValue | bool, BooleanValue | bool], BooleanValue | bool]" [arg-type]
+ ibis/expr/api.py:766: error: Argument 1 to "reduce" has incompatible type overloaded function; expected "Callable[[BooleanValue | bool, BooleanValue | bool], BooleanValue | bool]" [arg-type]
|
|
Thank you to everyone. @srittau, I hope I have told you before: I aspire to have your temperament. We are all blessed every time you gift us with your hard work. |
Closes #15611.