-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
Remove pandas.core.index.datetimelike from MyPy Blacklist #26280
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
Conversation
Codecov Report
@@ Coverage Diff @@ ## master #26280 +/- ## ========================================== - Coverage 91.98% 91.98% -0.01% ========================================== Files 175 175 Lines 52379 52379 ========================================== - Hits 48183 48179 -4 - Misses 4196 4200 +4
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@ ## master #26280 +/- ## ========================================== + Coverage 91.71% 91.99% +0.27% ========================================== Files 178 180 +2 Lines 50755 50774 +19 ========================================== + Hits 46552 46708 +156 + Misses 4203 4066 -137
Continue to review full report at Codecov.
|
WillAyd left a comment
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 for the PR! Looks like there's an isort failure in CI
pandas/core/indexes/datetimelike.py Outdated
| _isnan = cache_readonly(DatetimeLikeArrayMixin._isnan.fget) | ||
| hasnans = cache_readonly(DatetimeLikeArrayMixin._hasnans.fget) | ||
| _hasnans = hasnans # for index / array -agnostic code | ||
| _resolution = cache_readonly(DatetimeLikeArrayMixin._resolution.fget) |
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 it looks like these cache_readonly calls are what's tripping up mypy. @jbrockmendel might be able to offer some insight here
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 the problem with isort or 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.
If its mypy then would it be that it wants something like # type: cache_readonly?
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 mypy failure. Not actually with the cache_readonly as much as accessing the .fget attribute of the properties.
What's the intention of this code? Is passing fget from the property to cache_readonly faster than just accessing the property?
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.
What's the intention of this code? Is passing fget from the property to cache_readonly faster than just accessing the property?
cache_readonly behaves like property, expecting to get a unary method as its argument. Does it even work if you don't make the .fget` explicit?
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 failure is probably due to mypy. https://github.com/python/mypy/issues/6185
Deleting .fget run into other errors.
pandas/core/indexes/datetimelike.py Outdated
| return self._data._data | ||
| | ||
| @property | ||
| @property # type: ignore |
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?
8e67d73 to 7c56581 Compare | Can you merge master? |
| Can you merge master? Think this will be the last remaining module from the blacklist |
| can you merge master |
| mypy has the following error for the annotated value function:
I add annotation pandas/pandas/core/indexes/datetimes.py Line 327 in 8154efb
Do you have any better way? |
| # ------------------------------------------------------------------------ | ||
| # Abstract data attributes | ||
| | ||
| @property |
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 this change?
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.
mypy has the following error for the annotated value function:
pandas/core/indexes/datetimelike.py:139: error: "None" has no attribute "_data"
I add annotation # type: DatetimeArray to _data. But mypy still has the same error.
pandas/pandas/core/indexes/datetimes.py
Line 327 in 8154efb
| result._data = dtarr |
I guess that mypy mixed up the instance attribute _data with the class attributes _data which is set None in the beginning of DatetimeIndexOpsMixin.
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.
cc @WillAyd
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.
Sorry missed this ping. This could be a similar conversation to https://github.com/pandas-dev/pandas/pull/26518/files#r287573810
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 a better solution is to provide the appropriate type to _data. Can you create a DatetimeLikeArray in pandas._typing which should be something like DatetimeLikeArray = TypeVar('DatetimeLikeArray', DatetimeArray, PeriodArray, TimedeltaArray) and assign that type to _data in the class? I think that should resolve.
cc @jbrockmendel in case he has other insights on the types here
| # ------------------------------------------------------------------------ | ||
| # Abstract data attributes | ||
| | ||
| @property |
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 a better solution is to provide the appropriate type to _data. Can you create a DatetimeLikeArray in pandas._typing which should be something like DatetimeLikeArray = TypeVar('DatetimeLikeArray', DatetimeArray, PeriodArray, TimedeltaArray) and assign that type to _data in the class? I think that should resolve.
cc @jbrockmendel in case he has other insights on the types here
| @makbigc can you merge master and address last comment? This should close out the reference issue (updated OP to reflect) |
| I can't figure out why DatetimeArray, PeriodArray and TimedeltaArray cannot be imported inside _typing.py
|
pandas/_typing.py Outdated
| ABCExtensionArray, ABCIndexClass, ABCSeries, ABCSparseSeries) | ||
| | ||
| from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray | ||
| |
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.
don’t try to import these use a quoted 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.
The following is the import error in the failed tests:
ImportError: cannot import name '_INT64_DTYPE'
The name of the module _typing.py is started with underscore. In PEP 8 (https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles ), it states
_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose names start with an underscore.
Should we remove underscore from the name _typing? Or something else?
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 can’t import these names as they would cause circular imports
and don’t need to just use ‘DatetimeArray’ and such
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.
_INT64_DTYPE is just an alias, i.e., _INT64_DTYPE = np.dtype(np.int64). The import of numpy and the definition of _INT64_DTYPE are in the same file pandas/core/dtype/common.py.
I did a little experiment. Open a file called 'typing.py' (without underscore prefix) in the same directory as '_typing.py'. One can import DatetimeArray in typing.py.
Importing PeriodArray and TImedeltaArray also run into the same ImportError. Without them, one cannot define the mypy object TypeVar.
DatetimeLikeArray = TypeVar('DatetimeLikeArray', DatetimeArray, PeriodArray, TimedeltaArray).
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.
@WillAyd Would you tell me your opinion? Should we change the filename of _typing.py?
| No I don’t think this needs to be renamed. I don’t have a problem using the TYPE_CHECKING constant in that module alone if it resolves circular imports. …Sent from my iPhone On Jun 18, 2019, at 10:23 AM, Mak Sze Chun ***@***.***> wrote: @makbigc commented on this pull request. In pandas/_typing.py: > @@ -11,12 +11,17 @@ from pandas.core.dtypes.generic import ( ABCExtensionArray, ABCIndexClass, ABCSeries, ABCSparseSeries) +from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray + @WillAyd Would you tell me your opinion? Should we change the filename of _typing.py? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread. |
| Yes, the problem is due to the circular import. |
| Doesn’t using the TYPE_CHECKING constant from the stdlib typing module in pandas._typing resolve the circular import? I think this is really close! |
| @WillAyd Thanks for your reply. I have added the following in and added the following to datetimelike.py The circular import is solved. But mypy cannot find the DatetimeLikeArray! |
| lgtm. @WillAyd merge when ready. |
| Thanks @makbigc ! |
git diff upstream/master -u -- "*.py" | flake8 --diff