-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: Add return types to some top-level func #30565
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: Add return types to some top-level func #30565
Conversation
pandas/util/_tester.py
Outdated
|
||
PKG = os.path.dirname(os.path.dirname(__file__)) | ||
|
||
|
||
def test(extra_args=None): | ||
def test(extra_args=None) -> NoReturn: |
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.
do we use this pattern elsewhere?
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 don't think so, but is the correct one because of sys.exit
is used here, if I underdtand it correctly.
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.
Hmm, I see now NoReturn was only added in Python 3.6.2. We support 3.6.1 currently.
I'll remove this annotation unless peope think it's good idea to raise the python minimum version (I don't think it's a good idea myself).
pandas/core/reshape/pivot.py
Outdated
@@ -148,7 +148,8 @@ def pivot_table( | |||
table = table.sort_index(axis=1) | |||
|
|||
if fill_value is not None: | |||
table = table.fillna(value=fill_value, downcast="infer") | |||
filled = table.fillna(value=fill_value, downcast="infer") | |||
table = cast(DataFrame, filled) |
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 think the preference is for assert instead of cast @jbrockmendel
table = cast(DataFrame, filled) | |
assert table is not None # needed for mypy |
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 error is error: Incompatible types in assignment (expression has type "Optional[DataFrame]", variable has type "DataFrame")
. I cant reassign table
to a broader type. So that pattern won't work.
Could do
filled = table.fillna(value=fill_value, downcast="infer")
assert filled is not None
table = filled
but that's also ugly. But I agree that asserts are better, so I'll do that one above (unless you got something less ugly :-))
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.
(unless you got something less ugly :-)
we probably should be overloading all methods that have an inplace parameter, but that would require the use of Literal.
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.
Literals won’t come until 3.8 is the minimum version, so will be a while. Should we allow assignment to incompatible type in mypy, until 3.8? Seems like a lesser evil?
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.
Ok, should probably use ignore commands in the first line: https://mypy.readthedocs.io/en/latest/error_codes.html#silencing-errors-based-on-error-codes. Then ‘’table =...’’ can be used in the first line and the third line can be dropped.
I’ll update later to use this pattern.
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.
we are using the default for allow_redefinition. It does result in lots of variable renaming and some messy code, but the additional strictness is probably worth it.
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 there any way we could get mypy to understand that without inplace=True
isna
returns non-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.
Not until 3.8 and Literal, AFAIK.
There has been an idea floating around that the inplace
parameter should be deprecated everywhere (unless there is a performance benefit to it). That would simplify the return types.
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've changed this section to use # type: ignore
directive.
EDIT: didn't work, got a Flake8 F821 error (undefined name). I've reverted the commit back to the 3-line version.
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.
it's a good idea, we need to move the adoption of error codes forward. see #29197 could add # noqa for now.
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.
@topper-123 lgtm
bfcf579
to
648c113
Compare
This reverts commit 648c113.
Adds return type hints to some top-level funcs. I will take the rest over 1-2 more PRs.
Having return types typed up will be good for the user experience (better piping etc.).