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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from pandas._libs.lib import no_default
from pandas._typing import (
AggFuncType,
AnyArrayLike,
ArrayLike,
Axes,
Axis,
Expand Down Expand Up @@ -1141,7 +1142,17 @@ def __len__(self) -> int:
"""
return len(self.index)

def dot(self, other):
# pandas/core/frame.py:1146: error: Overloaded function signatures 1 and 2
# overlap with incompatible return types [misc]
@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

def dot(self, other: Series) -> Series: # type: ignore[misc]
...

@overload
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.

"""
Compute the matrix multiplication between the DataFrame and other.

Expand Down Expand Up @@ -1251,7 +1262,19 @@ def dot(self, other):
else: # pragma: no cover
raise TypeError(f"unsupported type: {type(other)}")

def __matmul__(self, other):
@overload
def __matmul__(self, other: Series) -> Series:
...

@overload
def __matmul__(
self, other: Union[AnyArrayLike, FrameOrSeriesUnion]
) -> FrameOrSeriesUnion:
...

def __matmul__(
self, other: Union[AnyArrayLike, FrameOrSeriesUnion]
) -> FrameOrSeriesUnion:
"""
Matrix multiplication using binary `@` operator in Python>=3.5.
"""
Expand Down