-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Fix type annotations in pandas.core.ops #26377
Conversation
Codecov Report
@@ Coverage Diff @@
## master #26377 +/- ##
==========================================
- Coverage 91.68% 91.67% -0.01%
==========================================
Files 174 174
Lines 50703 50705 +2
==========================================
- Hits 46488 46485 -3
- Misses 4215 4220 +5
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #26377 +/- ##
==========================================
- Coverage 91.68% 91.68% -0.01%
==========================================
Files 174 174
Lines 50703 50736 +33
==========================================
+ Hits 46488 46517 +29
- Misses 4215 4219 +4
Continue to review full report at Codecov.
|
Ignore previous comment. What if instead of adding all of the casts though we did away with the |
Looking at it fresh today, we can get by with a single cast like this: reverse_op = cast(str, _op_descriptions[key]['reverse'])
if reverse_op is not None:
_op_descriptions[reverse_op] = _op_descriptions[key].copy()
_op_descriptions[reverse_op]['reversed'] = True
_op_descriptions[reverse_op]['reverse'] = key Can we live with that? It's a reasonable middle ground between my first solution and making a bunch of substantive changes (see below).
It's marginally more complicated than that. In addition to being used in the code we're looking at for typing, it's used to build documentation in op_name = op_name.replace('__', '')
op_desc = _op_descriptions[op_name]
if op_desc['reversed']:
equiv = 'other ' + op_desc['op'] + ' ' + typ
else:
equiv = typ + ' ' + op_desc['op'] + ' other' This file takes the operators described in It's probably possible to simplify it, but how far do you want to go to save a couple casts? |
Thanks for digging further. So my point is that the current code looks something like this: from typing import ... cast
...
_op_descriptions[key]['reversed'] = False
reverse_op = cast(str, _op_descriptions[key]['reverse'])
if reverse_op is not None:
_op_descriptions[reverse_op] = _op_descriptions[key].copy()
_op_descriptions[reverse_op]['reversed'] = True
_op_descriptions[reverse_op]['reverse'] = key
...
# down in _make_flex_doc
if op_desc['reversed']:
... AFAICT ...
if reverse_op is not None:
_op_descriptions[reverse_op] = _op_descriptions[key].copy()
_op_descriptions[reverse_op]['reverse'] = key
...
# down in _make_flex_doc
if op_desc.get('reverse'):
... Would definitely prefer the latter from a readability and maintenance perspective but lmk if I am missing anything. Not drastic in either case but would like to avoid using cast if a simple refactor of the code makes type inference possible |
That approach won't work. But all of the reverse operator names start with an 'r', so we could use that as the condition: if op_name.startswith('r'):
equiv = 'other ' + op_desc['op'] + ' ' + typ
else:
… It's unlikely there will be more operators added, so this is unlikely to cause a problem in the future. That approach is also used in |
Ah understood now thanks for clarifying. The alternative you have above seems fine There's a lot of cleanup / refactor that can get done here but maybe more suitable for follow up PRs |
Ha sorry my last comment was referencing your single cast idea...but I think this should do just as well. Should probably remove the large comment block now but otherwise lgtm - over to @jreback |
thanks @gwrome |
Part of #25882
git diff upstream/master -u -- "*.py" | flake8 --diff
Corrects these errors in
pandas.core.ops
: