Skip to content

STY: remove --keep-runtime-typing from pyupgrade Part-1 #40773

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 1 commit into from
Apr 6, 2021

Conversation

fathomer
Copy link
Contributor

@fathomer fathomer commented Apr 3, 2021

@fathomer fathomer changed the title STY: remove --keep-runtime-typing from pyupgrade #40759 STY: remove --keep-runtime-typing from pyupgrade Apr 3, 2021
@MarcoGorelli MarcoGorelli self-requested a review April 3, 2021 19:44
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Apr 4, 2021

The mypy failures are due to the fact that some of these methods have their own method type, which interferes with the built-in type

small example:

# t.py

from __future__ import annotations
from typing import Type


class Cat:
    
    def type(self) -> str:
        return 'this is my type'

    def type_cat(self) -> Type[Cat]:
        return Cat

works fine:

$ mypy t.py 
Success: no issues found in 1 source file

but the following:

# t.py

from __future__ import annotations
from typing import Type


class Cat:
    
    def type(self) -> str:
        return 'this is my type'

    def type_cat(self) -> type[Cat]:
        return Cat

doesn't:

$ mypy t.py 
t.py:10: error: Function "t.Cat.type" is not valid as a type
t.py:10: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file)

If we moved the definitions around so that type_cat came before type, it would work.


I think the solution here is to define the alias type_t = type in pandas/_typing.py, and then import that, and use it here. This seems analogous to using bool_t in pandas/core/generic.py (#40175)

cc @WillAyd @simonjayhawkins

EDIT

Actually, using type_t as I'd suggested doesn't work, as then we get

$ mypy t.py 
t.py:12: error: "type" expects no type arguments, but 1 given  [type-arg]
Found 1 error in 1 file (checked 1 source file)

will think of how to do this


EDIT2

This seems to work:

in pandas/_typing.py, apply the diff

diff --git a/pandas/_typing.py b/pandas/_typing.py
index f90ef33434..c11cfc0f21 100644
--- a/pandas/_typing.py
+++ b/pandas/_typing.py
@@ -25,7 +25,7 @@ from typing import (
     Optional,
     Sequence,
diff --git a/pandas/_typing.py b/pandas/_typing.py
index f90ef33434..c11cfc0f21 100644
--- a/pandas/_typing.py
+++ b/pandas/_typing.py
@@ -25,7 +25,7 @@ from typing import (
     Optional,
     Sequence,
     Tuple,
-    Type,
+    Type as Type_t,
     TypeVar,
     Union,
 )
@@ -119,7 +119,7 @@ Axes = Collection[Any]
 # dtypes
 NpDtype = Union[str, np.dtype]
 Dtype = Union[
-    "ExtensionDtype", NpDtype, Type[Union[str, float, int, complex, bool, object]]
+    "ExtensionDtype", NpDtype, Type_t[Union[str, float, int, complex, bool, object]]
 ]
 # DtypeArg specifies all allowable dtypes in a functions its dtype argument
 DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
@@ -187,3 +187,5 @@ ColspaceArgType = Union[
 # internals
 Manager = Union["ArrayManager", "BlockManager"]
 SingleManager = Union["SingleArrayManager", "SingleBlockManager"]

then for any of the lines that are throwing errors, in those files, import Type_t from pandas/_typing.py, and use Type_t instead of type in the problematic annotations

@fathomer
Copy link
Contributor Author

fathomer commented Apr 4, 2021

Thanks @MarcoGorelli for the nice explanation and suggestion, I have made changes according to that and pushed my code.

@MarcoGorelli
Copy link
Member

Nice, thanks @fathomer ! Let's wait to hear from the others about this import Type as Type_t workaround

@jreback jreback added the Code Style Code style, linting, code_checks label Apr 5, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

this will get quickly out of date. i would rebase now and we'll see if cna merge it. but maybe better to break into pieces.

confirming this is basically List -> list, Dict -> dict, Tuple -> tuple (can you put the list in the top).

if its more complicated than that we should do in steps.

@@ -25,7 +25,7 @@
Optional,
Sequence,
Tuple,
Type,
Type as Type_T,
Copy link
Contributor

Choose a reason for hiding this comment

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

why not TypeT

Copy link
Contributor

Choose a reason for hiding this comment

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

or type_t ?

Copy link
Member

Choose a reason for hiding this comment

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

you're right, type_t would be more consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I will change the variable name to type_t, keep the changes to 15-20 files as of now, and still keep the --keep-runtime-typing flag there in precommit hook.
Is that right @MarcoGorelli ?

Copy link
Member

Choose a reason for hiding this comment

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

yes, that's right @fathomer! Then, in the final PR, you can remove the flag

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 have reverted changes to all other files except some 20 files for this PR, did a rebase to upstream/master and pushed.
For the remaining files do you want me raise the other PRs just now or wait until this one gets merged ?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @fathomer! You can raise the others now if you want, I'll look over them tomorrow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MarcoGorelli @jreback Raised the other PRs as well, for some reason not getting the option to add you guys as reviewers there. Please feel free to review them.

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Agree on breaking into smaller pieces - if you apply these changes to, say, 15-20 files at a time, then the current check will still pass even if it has --keep-runtime-typing.

If you could break this into a few smaller pull requests, each with no more than 20 files, then we can get those in, and at the end, we can remove --keep-runtime-typing


Jeff - the changes here are from PEP585 (e.g. List[str] -> list[str]) and from PEP604 (Optional[str] -> str | None), both of which are only applied in the presence of from __future__ import annotations

@fathomer fathomer force-pushed the master branch 3 times, most recently from f9212a1 to db0aa16 Compare April 5, 2021 18:48
@fathomer fathomer changed the title STY: remove --keep-runtime-typing from pyupgrade STY: remove --keep-runtime-typing from pyupgrade Part 1 Apr 5, 2021
@fathomer fathomer changed the title STY: remove --keep-runtime-typing from pyupgrade Part 1 STY: remove --keep-runtime-typing from pyupgrade Part-1 Apr 5, 2021
@MarcoGorelli
Copy link
Member

Looks good, thanks!

@MarcoGorelli MarcoGorelli merged commit 757e151 into pandas-dev:master Apr 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants