Skip to content

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

Merged
merged 6 commits into from
Dec 31, 2019

Conversation

topper-123
Copy link
Contributor

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.).

@topper-123 topper-123 changed the title TYP: Add types to some top-level func return values TYP: Add return types to some top-level func Dec 30, 2019

PKG = os.path.dirname(os.path.dirname(__file__))


def test(extra_args=None):
def test(extra_args=None) -> NoReturn:
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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).

@@ -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)
Copy link
Member

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

Suggested change
table = cast(DataFrame, filled)
assert table is not None # needed for mypy

Copy link
Contributor Author

@topper-123 topper-123 Dec 30, 2019

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 :-))

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

@topper-123 topper-123 Dec 30, 2019

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@topper-123 topper-123 Dec 31, 2019

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.

Copy link
Member

@simonjayhawkins simonjayhawkins Dec 31, 2019

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.

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Dec 30, 2019
@simonjayhawkins simonjayhawkins added this to the 1.0 milestone Dec 30, 2019
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants