Skip to content

Commit 2238dae

Browse files
TYP: use npt.DTypeLike for type annotation of dtype arg in to_numpy (#43512)
1 parent fb32477 commit 2238dae

File tree

5 files changed

+11
-31
lines changed

5 files changed

+11
-31
lines changed

pandas/core/arrays/base.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def __ne__(self, other: Any) -> ArrayLike: # type: ignore[override]
449449

450450
def to_numpy(
451451
self,
452-
dtype: Dtype | None = None,
452+
dtype: npt.DTypeLike | None = None,
453453
copy: bool = False,
454454
na_value=lib.no_default,
455455
) -> np.ndarray:
@@ -478,12 +478,7 @@ def to_numpy(
478478
-------
479479
numpy.ndarray
480480
"""
481-
# error: Argument "dtype" to "asarray" has incompatible type
482-
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
483-
# Type[complex], Type[bool], Type[object], None]"; expected "Union[dtype[Any],
484-
# None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int,
485-
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
486-
result = np.asarray(self, dtype=dtype) # type: ignore[arg-type]
481+
result = np.asarray(self, dtype=dtype)
487482
if copy or na_value is not lib.no_default:
488483
result = result.copy()
489484
if na_value is not lib.no_default:

pandas/core/arrays/masked.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,9 @@ def __len__(self) -> int:
224224
def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
225225
return type(self)(~self._data, self._mask.copy())
226226

227-
# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
228-
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
229-
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
230-
def to_numpy( # type: ignore[override]
227+
def to_numpy(
231228
self,
232-
dtype: NpDtype | None = None,
229+
dtype: npt.DTypeLike | None = None,
233230
copy: bool = False,
234231
na_value: Scalar = lib.no_default,
235232
) -> np.ndarray:

pandas/core/arrays/numpy_.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Dtype,
1111
NpDtype,
1212
Scalar,
13+
npt,
1314
)
1415
from pandas.compat.numpy import function as nv
1516

@@ -365,12 +366,9 @@ def skew(
365366
# ------------------------------------------------------------------------
366367
# Additional Methods
367368

368-
# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
369-
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
370-
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
371-
def to_numpy( # type: ignore[override]
369+
def to_numpy(
372370
self,
373-
dtype: NpDtype | None = None,
371+
dtype: npt.DTypeLike | None = None,
374372
copy: bool = False,
375373
na_value=lib.no_default,
376374
) -> np.ndarray:

pandas/core/arrays/string_arrow.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Scalar,
2525
ScalarIndexer,
2626
SequenceIndexer,
27+
npt,
2728
)
2829
from pandas.compat import (
2930
pa_version_under1p0,
@@ -199,12 +200,9 @@ def __arrow_array__(self, type=None):
199200
"""Convert myself to a pyarrow Array or ChunkedArray."""
200201
return self._data
201202

202-
# error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray";
203-
# supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any],
204-
# Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]"
205-
def to_numpy( # type: ignore[override]
203+
def to_numpy(
206204
self,
207-
dtype: NpDtype | None = None,
205+
dtype: npt.DTypeLike | None = None,
208206
copy: bool = False,
209207
na_value=lib.no_default,
210208
) -> np.ndarray:

pandas/core/base.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,8 @@ def to_numpy(
516516
"""
517517
if is_extension_array_dtype(self.dtype):
518518
# error: Too many arguments for "to_numpy" of "ExtensionArray"
519-
520-
# error: Argument 1 to "to_numpy" of "ExtensionArray" has incompatible type
521-
# "Optional[Union[dtype[Any], None, type, _SupportsDType[dtype[Any]], str,
522-
# Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex,
523-
# Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]]";
524-
# expected "Optional[Union[ExtensionDtype, Union[str, dtype[Any]],
525-
# Type[str], Type[float], Type[int], Type[complex], Type[bool],
526-
# Type[object]]]"
527519
return self.array.to_numpy( # type: ignore[call-arg]
528-
dtype, copy=copy, na_value=na_value, **kwargs # type: ignore[arg-type]
520+
dtype, copy=copy, na_value=na_value, **kwargs
529521
)
530522
elif kwargs:
531523
bad_keys = list(kwargs.keys())[0]

0 commit comments

Comments
 (0)