Skip to content

CLN: Replace FrameOrSeriesUnion annotation by DataFrame | Series #41955

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 11 commits into from
Jul 4, 2021
Merged

CLN: Replace FrameOrSeriesUnion annotation by DataFrame | Series #41955

merged 11 commits into from
Jul 4, 2021

Conversation

Swanand01
Copy link
Contributor

@Swanand01 Swanand01 commented Jun 11, 2021

@Swanand01
Copy link
Contributor Author

@github-actions pre-commit

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.

 pandas/plotting/_matplotlib/timeseries.py:212:35: F821 undefined name 'DataFrame'
pandas/io/formats/info.py:112:23: F821 undefined name 'Series'
pandas/io/formats/info.py:415:35: F821 undefined name 'Series'
pandas/core/window/expanding.py:590:16: F821 undefined name 'DataFrame'
pandas/core/window/expanding.py:590:28: F821 undefined name 'Series'
pandas/core/window/expanding.py:655:16: F821 undefined name 'DataFrame'
pandas/core/window/expanding.py:655:28: F821 undefined name 'Series'
pandas/core/strings/accessor.py:2314:10: F821 undefined name 'DataFrame'
pandas/core/strings/accessor.py:2314:22: F821 undefined name 'Series'
pandas/plotting/_matplotlib/tools.py:56:15: F821 undefined name 'DataFrame'
pandas/plotting/_matplotlib/tools.py:56:27: F821 undefined name 'Series'
pandas/core/window/ewm.py:514:16: F821 undefined name 'DataFrame'
pandas/core/window/ewm.py:514:28: F821 undefined name 'Series'
pandas/core/window/ewm.py:581:16: F821 undefined name 'DataFrame'
pandas/core/window/ewm.py:581:28: F821 undefined name 'Series'
pandas/core/util/hashing.py:77:18: F821 undefined name 'DataFrame'

For such files you should import Series and DataFrame. If you need to avoid a circular import, you can do like they do here:

if TYPE_CHECKING:
from pandas import (
DataFrame,
Series,
)

@Swanand01
Copy link
Contributor Author

Hi @MarcoGorelli, would you mind taking a look at my last commit, I don't really know why the particular test is failing.

Thank you.

@simonjayhawkins
Copy link
Member

Hi @MarcoGorelli, would you mind taking a look at my last commit, I don't really know why the particular test is failing.

Thank you.

see #41955 (review) for the mypy failures.

There is also a pre-commit failure. see https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit

@Swanand01
Copy link
Contributor Author

I fixed all the mypy errors from the prev. commit in this commit. Should I run @github-actions pre-commit like it says in the documentation?

@MarcoGorelli
Copy link
Member

I fixed all the mypy errors from the prev. commit in this commit.

They're still failing, if you click "details" you'll see the failures:

Screenshot 2021-06-15 at 16 10 28

Should I run @github-actions pre-commit like it says in the documentation?

This won't fix the flake8 errors, you'll probably need to run it locally before committing

@Swanand01
Copy link
Contributor Author

Hi, I can't seem to understand these pre-commit errors. Are they formatting related?

@MarcoGorelli
Copy link
Member

Hey @Swanand01 - yeah, see here for how to run them

Seems like this is on the right track anyway!

@Swanand01
Copy link
Contributor Author

Hi @MarcoGorelli , all checks pass now. Thank you.

@lithomas1 lithomas1 added Code Style Code style, linting, code_checks Clean Typing type annotations, mypy/pyright type checking and removed Code Style Code style, linting, code_checks labels Jun 20, 2021
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.

Looks good to me, thanks for having stuck with this!

@Swanand01
Copy link
Contributor Author

Hi, how will this PR be merged? Do I have to close this PR now?

@simonjayhawkins
Copy link
Member

Hi, how will this PR be merged? Do I have to close this PR now?

no do not close. other reviewers could either request changes, add their approval or merge this.

@Swanand01
Copy link
Contributor Author

Hi, how will this PR be merged? Do I have to close this PR now?

no do not close. other reviewers could either request changes, add their approval or merge this.

Okay thanks.

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.

Thanks @Swanand01 lgtm. just a couple of suggestions.

@@ -1362,7 +1361,7 @@ def dot(self, other: Series) -> Series:
def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame:
...

def dot(self, other: AnyArrayLike | FrameOrSeriesUnion) -> FrameOrSeriesUnion:
def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
Copy link
Member

Choose a reason for hiding this comment

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

A Series is also AnyArrayLike

Suggested change
def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
def dot(self, other: AnyArrayLike | DataFrame) -> DataFrame | Series:

Copy link
Member

@MarcoGorelli MarcoGorelli Jul 3, 2021

Choose a reason for hiding this comment

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

ooh, we should add a check for this

EDIT

#42359

Copy link
Member

Choose a reason for hiding this comment

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

ok. we can merge this and let the check fix this

@@ -1478,13 +1477,13 @@ def __matmul__(self, other: Series) -> Series:

@overload
def __matmul__(
self, other: AnyArrayLike | FrameOrSeriesUnion
) -> FrameOrSeriesUnion:
self, other: AnyArrayLike | DataFrame | Series
Copy link
Member

Choose a reason for hiding this comment

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

same (and already included in previous overload anyway)

...

def __matmul__(
self, other: AnyArrayLike | FrameOrSeriesUnion
) -> FrameOrSeriesUnion:
self, other: AnyArrayLike | DataFrame | Series
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -1362,7 +1361,7 @@ def dot(self, other: Series) -> Series:
def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame:
...

def dot(self, other: AnyArrayLike | FrameOrSeriesUnion) -> FrameOrSeriesUnion:
def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
Copy link
Member

Choose a reason for hiding this comment

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

ok. we can merge this and let the check fix this

@simonjayhawkins simonjayhawkins added this to the 1.4 milestone Jul 4, 2021
@simonjayhawkins simonjayhawkins merged commit ed2af7e into pandas-dev:master Jul 4, 2021
@simonjayhawkins
Copy link
Member

Thanks @Swanand01

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Jul 4, 2021

@simonjayhawkins I thought we discussed in our monthly dev meeting to not do this change because you lose the typeahead feature in editors. I.e., you can do typeahead for FrameOrSeriesUnion but not DataFrame | Series

@MarcoGorelli
Copy link
Member

@Dr-Irv my understanding was that that conversation in the dev meeting was spurred from the fact that it looked like this PR had gone stale and it was marked for 1.3. In light of that, and of the typeahead issue you brought up, it didn't seem worth pushing it though

Once activity resumed, though, it seemed fine to stick with it. Perhaps I should've pinged you to check, sorry about that

No objection to reverting (or re-discussing in the next dev meeting) if you wish, but I do think this is fine to keep. An unexpected benefit is that it's helped make it clear that there are cases when it's redundant to have | Series, e.g. when there's AnyArrayLike.
Plus, I still think it's more explicit than FrameOrSeriesUnion (which can be confused with FrameOrSeries) and that this outweights having a shorter typeahead

@simonjayhawkins
Copy link
Member

but I do think this is fine to keep. An unexpected benefit is that it's helped make it clear that there are cases when it's redundant to have | Series, e.g. when there's AnyArrayLike.
Plus, I still think it's more explicit than FrameOrSeriesUnion (which can be confused with FrameOrSeries) and that this outweights having a shorter typeahead

agreed. and with #42367 removing the redundant typevars, things become clearer still.

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

Successfully merging this pull request may close these issues.

STYLE, TYP frame-or-series-union check no longer relevant
5 participants