Skip to content
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

TYP: misc fixes for numpy types 3 #36100

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
# array-like

AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
AnyArrayLikeUnion = Union["ExtensionArray", "Index", "Series", np.ndarray]
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment on when you should use the AnyArryLike TypeVar and when the Union (for readers of this code!)

ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray)
ArrayLikeUnion = Union["ExtensionArray", np.ndarray]

# scalars

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pandas._libs import Timedelta, hashtable as libhashtable, lib
import pandas._libs.join as libjoin
from pandas._typing import ArrayLike, FrameOrSeries
from pandas._typing import ArrayLikeUnion, FrameOrSeries
from pandas.errors import MergeError
from pandas.util._decorators import Appender, Substitution

Expand Down Expand Up @@ -1869,7 +1869,7 @@ def _right_outer_join(x, y, max_groups):


def _factorize_keys(
lk: ArrayLike, rk: ArrayLike, sort: bool = True, how: str = "inner"
lk: ArrayLikeUnion, rk: ArrayLikeUnion, sort: bool = True, how: str = "inner"
) -> Tuple[np.array, np.array, int]:
"""
Encode left and right keys as enumerated types.
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pandas._libs import lib, writers as libwriters
from pandas._libs.tslibs import timezones
from pandas._typing import ArrayLike, FrameOrSeries, Label
from pandas._typing import AnyArrayLikeUnion, ArrayLike, FrameOrSeries, Label
from pandas.compat._optional import import_optional_dependency
from pandas.compat.pickle_compat import patch_pickle
from pandas.errors import PerformanceWarning
Expand Down Expand Up @@ -5076,7 +5076,7 @@ def _dtype_to_kind(dtype_str: str) -> str:
return kind


def _get_data_and_dtype_name(data: ArrayLike):
def _get_data_and_dtype_name(data: AnyArrayLikeUnion):
Copy link
Member

Choose a reason for hiding this comment

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

might be better to ensure we unwrap to avoid Index/Series?

for my edification: i was under the impression that AnyArrayLike vs AnyArrayLikeUnion only mattered when we had more than one of them in a signature; i.e.

def foo(arg: AnyArrayLike) -> AnyArrayLike:

implies the return type matches the arg type, while using AnyArrayLikeUnion does not imply that. if that's correct, does it matter which we use in cases like this with only one annotation in the signature?

Copy link
Member Author

Choose a reason for hiding this comment

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

in this case we change the type of data in the body. i.e assign to data and hence no preserving type of data.

I've used ..Union to be consistent with FrameOrSeries.

we could change ArrayLike and AnyArrayLike to be unions and ArrayLikeT and AnyArrayLikeT for type variables.

or we could assign to another variable name in the body (simpler, but imo messy and less clear)

Copy link
Member

Choose a reason for hiding this comment

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

im happy to follow your lead on this

Copy link
Member Author

Choose a reason for hiding this comment

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

@WillAyd wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

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

or we could assign to another variable name in the body (simpler, but imo messy and less clear)

I don't think worth going down this path. We will likely need a Union type for variable type annotations anyway, since if we use ArrayLike we would get error: Type variable "pandas._typing.ArrayLike" is unbound [valid-type]

"""
Convert the passed data into a storable form and a dtype string.
"""
Expand Down