-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP overload fillna #40737 #40887
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 overload fillna #40737 #40887
Conversation
LarWong
commented
Apr 11, 2021
•
edited
Loading
edited
- closes TYP overload fillna #40737
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
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.
You don't need all these overloads - you just need:
- one for
Literal[False]
=... - one for
bool=...
- one for each combination of default arguments which precede
inplace
that should work out to 10 in total
@MarcoGorelli Oh ok, I was under the impression that I needed an overload for each combination. I'll change it. |
Nice! And great that this makes a couple of Could include the output from running something like what was done in #40860 (comment) ? I think you might need to always include |
Sure, I'll post it soon |
@MarcoGorelli Yes, you were correct. |
Here are the inputs to and the outputs of mypy:
Output:
Output:
|
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 @LarWong ! Looks good, I just have a question about:
value: Scalar | dict | Series | DataFrame | None
Where did you get this from? Was it from inspection? cc @simonjayhawkins is this correct?
pandas/core/frame.py
Outdated
@overload | ||
def fillna( | ||
self, | ||
value: Scalar | dict | Series | DataFrame | 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.
you've typed value
in the overloads, perhaps let's type it in the function signature too? (line 5128)
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.
I can modify it, but let's wait until someone has confirmed the typing of value
first.
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.
@LarWong perhaps let's revert typing value
here, that can be done separately (and we'll probably need to be more precise than dict
), the rest looks good
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.
@MarcoGorelli Sorry, but I'm not sure what you mean by revert typing. Do you mean:
@overload
def fillna(
self,
value,
As in not typing value
in the overloads?
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.
Exactly
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.
@MarcoGorelli Ok done! The outputs from mypy
are the same after modifications (see above).
pandas/core/frame.py
Outdated
@overload | ||
def fillna( | ||
self, | ||
value: Scalar | dict | Series | DataFrame | 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.
can we make an alias for this
@@ -5007,6 +5007,121 @@ def rename( | |||
errors=errors, | |||
) | |||
|
|||
@overload |
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.
can we not put these in .pyi?
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.
I was told to add them directly to these files since existing overloads were already there
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.
@jreback yes, that would be great! Thing is though, the .pyi
files require you to define all methods of a module
Given the sheer number of methods this module has, I'd suggest taking this PR with the overloads here, and then moving the overloads (along with annotations for all other methods) to a pandas/core/frame.pyi
file
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.
@LarWong I'll get back to you on typing value
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.
can we not put these in .pyi?
I don't think we want to go the route of using stubs for python files
@jreback yes, that would be great! Thing is though, the
.pyi
files require you to define all methods of a module
not that I think it should be done here, but it is possible to partially type a module using stubs.
https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#incomplete-stubs
Partial modules (i.e. modules that are missing some or all classes, functions, or attributes) must include a top-level getattr() function marked with an # incomplete comment (see example below).
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 @simonjayhawkins , didn't know that was possible. Why not use partial stubs for overloads? Because some methods, like Series.drop
Lines 4478 to 4487 in 84d9c5e
def drop( | |
self, | |
labels=None, | |
axis=0, | |
index=None, | |
columns=None, | |
level=None, | |
inplace=False, | |
errors="raise", | |
) -> Series: |
have 5 (five!) arguments with defaults before inplace
, leading to...34 overloads 🤯 ! And even if we disallowed labels
being passed as None
in that one, that would still leave us with 18 overloads!
For typing value
here, do you think Scalar | Mapping[Hashable, Scalar] | Series | DataFrame | None
would be correct?
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.
The problem is that if a stub file is present it takes precedence over the python file. so we cannot ensure internal consistency and need to duplicate the type annotations to be able to check the functions in the module itself.
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.
have 5 (five!) arguments with defaults before
inplace
, leading to...34 overloads
I think we are planning to to drop the inplace argument, hopefully pandas 2.0 and we won't need all these overloads. #16529
@MarcoGorelli For |
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 @LarWong !
Will try to either move overloads to .pyi
files and have some way of keeping them in sync, or turning off the black formatter and putting some noqa
s in, tomorrow
* TYP: Added overloads for fillna() in frame.py and series.py * TYP: Added overloads for fillna() in frame.py and series.py pandas-dev#40737 * TYP: Added fillna() overloads to generic.py pandas-dev#40727 * TYP: removed generic overloads pandas-dev#40737 * fixed redundant cast error * reverting prior changes * remove cast again * removed unnecessary overloads in frame.py and series.py * fixed overloads * reverted value typing * remove extra types (lets keep this to overloads) Co-authored-by: Marco Gorelli <marcogorelli@protonmail.com>
* TYP: Added overloads for fillna() in frame.py and series.py * TYP: Added overloads for fillna() in frame.py and series.py pandas-dev#40737 * TYP: Added fillna() overloads to generic.py pandas-dev#40727 * TYP: removed generic overloads pandas-dev#40737 * fixed redundant cast error * reverting prior changes * remove cast again * removed unnecessary overloads in frame.py and series.py * fixed overloads * reverted value typing * remove extra types (lets keep this to overloads) Co-authored-by: Marco Gorelli <marcogorelli@protonmail.com>
* TYP: Added overloads for fillna() in frame.py and series.py * TYP: Added overloads for fillna() in frame.py and series.py pandas-dev#40737 * TYP: Added fillna() overloads to generic.py pandas-dev#40727 * TYP: removed generic overloads pandas-dev#40737 * fixed redundant cast error * reverting prior changes * remove cast again * removed unnecessary overloads in frame.py and series.py * fixed overloads * reverted value typing * remove extra types (lets keep this to overloads) Co-authored-by: Marco Gorelli <marcogorelli@protonmail.com>