-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP fixup overloads for reset_index #40200
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
TYP fixup overloads for reset_index #40200
Conversation
watch this space! |
see #40202 |
pandas/core/frame.py
Outdated
... | ||
|
||
def reset_index( | ||
self, level=None, drop=False, inplace=False, col_level=0, col_fill="" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing the type annotations from the function means that we lose the type checking for the body of the function.
so if you reveal_type on say level
you will get Revealed type is 'Any'
IIUC correctly, the type annotations on the function are used to check the function itself, while the overloads are used when checking other functions that call this one.
so both are needed.
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
) -> None: | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the last overload needed now? won't it fall back to the function definition if no overloads match?
updated: comment should have been on the next one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure it's needed, see here for a toy example (uncomment the commented-out part and it'll pass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commenting out the last overload leads to the same errors observed on master
in the typing test from #40202 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok, and Revealed type is 'Any'
. I thought that it fell back to the non-overloaded function with no matching overloads. maybe something changed in mypy or only does so under different conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only does so under different conditions
If I've understood correctly, this is it - see #39501 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @MarcoGorelli it would be great to have some testing in place before merging this, not because there could be anything wrong with the additions, but because susequent changes could easily break things. but we are where we are, so merge when ready.
just one more thing! these extra overloads are needed because of an issue with mypy. we have now lost that link to the issue. although the mypy issue was initially dismissed, it appears that there is acknowledgement that the workaround can becomes impractical with many arguments. There therefore may be a fix on the mypy side in the future. We somehow need to track that issue. |
* fixup overloads for reset_index * another overload * add defaults for bool case * add types back to function signature
The (*, level, inplace) overload should probably have been (level, *, inplace), but I'll check tomorrow |
Took me longer than I'm proud to admit, but I think I've finally understood the deal with overload and default types.
We need one overload for the default case (here,
inplace=False
), and then an extra overload of each combination of keyword arguments which precedeinplace
.So here, we need:
inplace=True
level
, andinplace=True
drop
, andinplace=True
level
, nodrop
, andinplace=True
Then,
mypy
will be able to correctly infer the return type forreset_index
based on the value ofinplace
.This kind of thing will help resolve the mypy issues seen in #38594, where the return type from
fillna
couldn't be determined. It also means not needing to add the ignore error from #37137Some examples: