diff --git a/pandas/core/common.py b/pandas/core/common.py index 098b501cc95c9..2e8d6dbced4e3 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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 = ... +) -> 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) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 59e55bdcb405a..8ebaaa28e13a5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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