-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
#25790 Updating type hints to Python3 syntax in pandas/core/array #25829
Conversation
Codecov Report
@@ Coverage Diff @@
## master #25829 +/- ##
==========================================
- Coverage 91.27% 91.27% -0.01%
==========================================
Files 173 173
Lines 53002 53017 +15
==========================================
+ Hits 48376 48389 +13
- Misses 4626 4628 +2
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #25829 +/- ##
==========================================
+ Coverage 91.53% 91.56% +0.02%
==========================================
Files 175 175
Lines 52808 52789 -19
==========================================
- Hits 48338 48335 -3
+ Misses 4470 4454 -16
Continue to review full report at Codecov.
|
pandas/core/arrays/array_.py
Outdated
copy=True, # type: bool | ||
): | ||
# type: (...) -> ExtensionArray | ||
if TYPE_CHECKING: |
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.
why is this needed? conditional imports for type checking are not supportable
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.
As I understand it, we need ExtensionArray to be defined to check this function's return type. Since ExtensionArray is defined elsewhere, we need to import it during type checking. I based this code on the approach described in PEP 484 — Runtime or type checking.
Is there a better, supportable way to do this?
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.
Yea this is the same conversation as #25802 (comment)
@jreback our support of 3.5 includes 3.5.0 and 3.5.1 right? This wasn't introduced until 3.5.2
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 would instead simply use the ABCExtensionArray classes which would work for this (and lots of other clases). we should maybe just do this generally. These are by-definition available with no dependencies for this exact purpose.
pandas/core/arrays/array_.py
Outdated
copy=True, # type: bool | ||
): | ||
# type: (...) -> ExtensionArray | ||
if TYPE_CHECKING: |
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 would instead simply use the ABCExtensionArray classes which would work for this (and lots of other clases). we should maybe just do this generally. These are by-definition available with no dependencies for this exact purpose.
pandas/core/arrays/base.py
Outdated
@@ -343,8 +337,7 @@ def astype(self, dtype, copy=True): | |||
""" | |||
return np.array(self, dtype=dtype, copy=copy) | |||
|
|||
def isna(self): | |||
# type: () -> Union[ExtensionArray, np.ndarray] | |||
def isna(self) -> Union['ExtensionArray', np.ndarray]: |
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.
Might as well use the ABC here and throughout module
pandas/core/arrays/datetimelike.py
Outdated
@@ -58,8 +58,7 @@ def _get_attributes_dict(self): | |||
return {k: getattr(self, k, None) for k in self._attributes} | |||
|
|||
@property | |||
def _scalar_type(self): | |||
# type: () -> Union[type, Tuple[type]] | |||
def _scalar_type(self) -> Union[type, Tuple[type]]: |
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.
Let's use Type consistently
pandas/core/arrays/period.py
Outdated
scalars: Sequence[Optional[Period]], | ||
dtype: PeriodDtype = None, | ||
copy: bool = False, | ||
) -> 'PeriodArray': |
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.
ABCPeriodArray
pandas/core/arrays/sparse.py
Outdated
def _sparse_array_op(left, right, op, name): | ||
# type: (SparseArray, SparseArray, Callable, str) -> Any | ||
def _sparse_array_op( | ||
left: 'SparseArray', |
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.
ABCSparseArray
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.
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.
@gwrome one other thing that could be helpful - since some if not all of these modules are blacklisted from mypy could you manually run locally for the modules and compare this PR to head? Just want to make sure there are no new regressions
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.
Couple minor things. IMO this is close - obviously some of these annotations are wrong and may need to be fixed but can be done in subsequent PRs
# TODO: ABCIndexClass is a valid type for other but had to be excluded | ||
# due to length of Py2 compatability comment; add back in once migrated | ||
# to Py3 syntax | ||
other: Union[ExtensionArray, np.ndarray, ABCIndexClass], |
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.
Any reason for removing the type for ndarray?
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.
During my testing at the time (before you added mypy.ini and the rest of the type checking framework to the project), mypy was throwing an error that suggested that ndarrays couldn't take a subscript, so I dropped it. I probably had something in my testing environment set up incorrectly (or maybe more strictly, not sure), because it doesn't throw that error with the current mypy.ini minus the blacklist. Will add it back.
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.
Wait, it wasn't mypy. code_checks doctests
throws
...pandas/core/arrays/period.py", line 545, in PeriodArray
other: Union[ExtensionArray, np.ndarray[int], ABCIndexClass],
TypeError: 'type' object is not subscriptable
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.
Gotcha. Kind of strange this throws an error now but not before. Something to revisit later
@@ -343,8 +338,7 @@ def astype(self, dtype, copy=True): | |||
""" | |||
return np.array(self, dtype=dtype, copy=copy) | |||
|
|||
def isna(self): | |||
# type: () -> Union[ExtensionArray, np.ndarray] | |||
def isna(self) -> Union[ABCExtensionArray, np.ndarray]: |
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.
so this is a the first stab at ArrayLike (e.g. to put into pandas.typing)
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.
Seems like a reasonable place to start.
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.
these are ok here for now, mainly adding things that we should add
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.
Agreed this would be great @gwrome do you mind opening up an issue as a follow up?
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.
No problem. I should be able to do that this weekend.
# type: (Union[str, np.dtype, 'ExtensionDtype', type], Any) -> None | ||
def __init__( | ||
self, | ||
dtype: Union[str, np.dtype, ExtensionDtype, Type] = np.float64, |
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.
this could be Dtype (in pandas.typing)
lgtm. @WillAyd merge when ready |
nice job @gwrome - thanks for helping to kick off these typing initiatives |
git diff upstream/master -u -- "*.py" | flake8 --diff