Skip to content

TYP: DataFrame.(dot, __matmul__) #38765

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 4 commits into from
Jan 5, 2021

Conversation

arw2019
Copy link
Member

@arw2019 arw2019 commented Dec 28, 2020

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

xref https://github.com/pandas-dev/pandas/pull/38416/files#r549438292

@arw2019
Copy link
Member Author

arw2019 commented Dec 28, 2020

cc @simonjayhawkins This currently fails with mypy for reasons I haven't figured out:

pandas/core/frame.py:1162: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [misc]
pandas/core/frame.py:1280: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [misc]

but I believe what I have is the correct logic for these methods:

  • if other is Series we return Series
  • if other is a DataFrame, np.ndarray, ExtensionArray or Index we return a DataFrame
  • otherwise we throw

@arw2019 arw2019 mentioned this pull request Dec 28, 2020
5 tasks
@gfyoung gfyoung added the Typing type annotations, mypy/pyright type checking label Dec 29, 2020
...

@overload
def dot(self, other: Union[DataFrame, Index, ArrayLike]) -> Optional[DataFrame]:
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the return Optional?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a (likely misguided) attempt to please mypy. There's no return statement after the raise at the end so technically returns None, although ofc we never get there

(I'm honestly not sure why what I have, without the optional return, isn't working - I think it should be right)

@simonjayhawkins
Copy link
Member

cc @simonjayhawkins This currently fails with mypy for reasons I haven't figured out:

from experience, error: Overloaded function signatures 1 and 2 overlap with incompatible return types occur since np.ndarray resolves to Any.

@simonjayhawkins
Copy link
Member

Thanks @arw2019 for the PR. when adding overloads also add the types to the actual method.

The overloads are used by mypy when establishing the return type when checking functions with calls to this method. The typing on the actual method is used to check this method.

@arw2019
Copy link
Member Author

arw2019 commented Jan 4, 2021

cc @simonjayhawkins This currently fails with mypy for reasons I haven't figured out:

from experience, error: Overloaded function signatures 1 and 2 overlap with incompatible return types occur since np.ndarray resolves to Any.

Right. Is it right there's no great way to fix that before NumPy 1.2?

Currently I added a type: ignore which can be removed once we're checking against NumPy 1.2 (and I believe mypy will force that because the type: ignore will become unused).

That said, I know type: ignore use is an anti-pattern, so alternatively I can just delete the overloads and create an issue in the tracker to be taken up once we've upgraded NumPy.

Thanks @arw2019 for the PR. when adding overloads also add the types to the actual method.

The overloads are used by mypy when establishing the return type when checking functions with calls to this method. The typing on the actual method is used to check this method.

Thanks, that was the problem (in addition to the ndarray issue)

@@ -1141,7 +1142,15 @@ def __len__(self) -> int:
"""
return len(self.index)

def dot(self, other):
@overload
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this ignore?

Copy link
Contributor

Choose a reason for hiding this comment

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

i see your comment, can you make this the actual type error as a comment

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@jreback jreback added this to the 1.3 milestone Jan 5, 2021
@jreback jreback merged commit 26fa853 into pandas-dev:master Jan 5, 2021
@jreback
Copy link
Contributor

jreback commented Jan 5, 2021

thanks @arw2019

def dot(self, other: Union[DataFrame, Index, ArrayLike]) -> DataFrame:
...

def dot(self, other: Union[AnyArrayLike, FrameOrSeriesUnion]) -> FrameOrSeriesUnion:
Copy link
Member

Choose a reason for hiding this comment

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

@arw2019 just FYI

error: Overloaded function implementation cannot satisfy signature 2 due to inconsistencies in how they use type variables [misc]

when we add numpy types.

the problem is the Series in AnyArrayLike could be a Series subclass.

I would like to change AnyArrayLike to a Union instead of a TypeVar to avoid many issues like this

good news though. the ignore above is not needed.

luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
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.

4 participants