Skip to content

TYP: overload asarray_tuplesafe signature #46966

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 1 commit into from
May 9, 2022
Merged
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
21 changes: 17 additions & 4 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,27 @@ def count_not_none(*args) -> int:
return sum(x is not None for x in args)


def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
@overload
def asarray_tuplesafe(
values: ArrayLike | list | tuple | zip, dtype: NpDtype | None = ...
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively to special-casing, we could omit zip here and have the caller convert the argument to a list first, instead of having this function do the conversion (currently there is one call site using zip).

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with having zip here.

) -> np.ndarray:
# ExtensionArray can only be returned when values is an Index, all other iterables
# will return np.ndarray. Unfortunately "all other" cannot be encoded in a type
# signature, so instead we special-case some common types.
...


@overload
def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = ...) -> ArrayLike:
...


def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLike:

if not (isinstance(values, (list, tuple)) or hasattr(values, "__array__")):
values = list(values)
elif isinstance(values, ABCIndex):
# error: Incompatible return value type (got "Union[ExtensionArray, ndarray]",
# expected "ndarray")
return values._values # type: ignore[return-value]
return values._values

if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,7 @@ def __new__(
subarr = com.asarray_tuplesafe(data, dtype=_dtype_obj)
if dtype is None:
# with e.g. a list [1, 2, 3] casting to numeric is _not_ deprecated
# error: Incompatible types in assignment (expression has type
# "Union[ExtensionArray, ndarray[Any, Any]]", variable has type
# "ndarray[Any, Any]")
subarr = _maybe_cast_data_without_dtype( # type: ignore[assignment]
subarr = _maybe_cast_data_without_dtype(
subarr, cast_numeric_deprecated=False
)
dtype = subarr.dtype
Expand Down