Skip to content

Commit b7d72f6

Browse files
authored
TYP: overload asarray_tuplesafe signature (#46966)
1 parent ba2fdb1 commit b7d72f6

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pandas/core/common.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,27 @@ def count_not_none(*args) -> int:
225225
return sum(x is not None for x in args)
226226

227227

228-
def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
228+
@overload
229+
def asarray_tuplesafe(
230+
values: ArrayLike | list | tuple | zip, dtype: NpDtype | None = ...
231+
) -> np.ndarray:
232+
# ExtensionArray can only be returned when values is an Index, all other iterables
233+
# will return np.ndarray. Unfortunately "all other" cannot be encoded in a type
234+
# signature, so instead we special-case some common types.
235+
...
236+
237+
238+
@overload
239+
def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = ...) -> ArrayLike:
240+
...
241+
242+
243+
def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLike:
229244

230245
if not (isinstance(values, (list, tuple)) or hasattr(values, "__array__")):
231246
values = list(values)
232247
elif isinstance(values, ABCIndex):
233-
# error: Incompatible return value type (got "Union[ExtensionArray, ndarray]",
234-
# expected "ndarray")
235-
return values._values # type: ignore[return-value]
248+
return values._values
236249

237250
if isinstance(values, list) and dtype in [np.object_, object]:
238251
return construct_1d_object_array_from_listlike(values)

pandas/core/indexes/base.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,7 @@ def __new__(
555555
subarr = com.asarray_tuplesafe(data, dtype=_dtype_obj)
556556
if dtype is None:
557557
# with e.g. a list [1, 2, 3] casting to numeric is _not_ deprecated
558-
# error: Incompatible types in assignment (expression has type
559-
# "Union[ExtensionArray, ndarray[Any, Any]]", variable has type
560-
# "ndarray[Any, Any]")
561-
subarr = _maybe_cast_data_without_dtype( # type: ignore[assignment]
558+
subarr = _maybe_cast_data_without_dtype(
562559
subarr, cast_numeric_deprecated=False
563560
)
564561
dtype = subarr.dtype

0 commit comments

Comments
 (0)