Skip to content

TYP: use DTypeLike alias from numpy.typing #41185

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

Closed
wants to merge 1 commit into from
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
14 changes: 9 additions & 5 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from mmap import mmap
from os import PathLike
from typing import (
from typing import ( # noqa: F401
IO,
TYPE_CHECKING,
Any,
Expand All @@ -32,6 +32,13 @@

import numpy as np

try:
from numpy.typing import DTypeLike as NpDtype
except ImportError:
# error: Name 'NpDtype' already defined (possibly by an import)
NpDtype: Any = None # type: ignore[no-redef]


# To prevent import cycles place any internal imports in the branch below
# and use a string literal forward reference to it in subsequent types
# https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
Expand Down Expand Up @@ -122,10 +129,7 @@
Axes = Collection[Any]

# dtypes
NpDtype = Union[str, np.dtype]
Dtype = Union[
"ExtensionDtype", NpDtype, type_t[Union[str, float, int, complex, bool, object]]
]
Dtype = Union["ExtensionDtype", NpDtype]
Copy link
Member

Choose a reason for hiding this comment

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

do we need to worry about strings like "categorical" that can be interpreted as pandas dtypes but not np.dtype?

# DtypeArg specifies all allowable dtypes in a functions its dtype argument
DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
DtypeObj = Union[np.dtype, "ExtensionDtype"]
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,7 @@ def apply_standard(self) -> FrameOrSeriesUnion:

with np.errstate(all="ignore"):
if isinstance(f, np.ufunc):
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
# "Series"; expected "Union[Union[int, float, complex, str, bytes,
# generic], Sequence[Union[int, float, complex, str, bytes, generic]],
# Sequence[Sequence[Any]], _SupportsArray]"
return f(obj) # type: ignore[arg-type]
return f(obj)

# row-wise access
if is_extension_array_dtype(obj.dtype) and hasattr(obj._values, "map"):
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ def to_numpy( # type: ignore[override]
if na_value is lib.no_default:
na_value = libmissing.NA
if dtype is None:
# error: Incompatible types in assignment (expression has type
# "Type[object]", variable has type "Union[str, dtype[Any], None]")
dtype = object # type: ignore[assignment]
dtype = object
if self._hasna:
if (
not is_object_dtype(dtype)
Expand Down Expand Up @@ -409,9 +407,7 @@ def isin(self, values) -> BooleanArray: # type: ignore[override]
result += self._mask
else:
result *= np.invert(self._mask)
# error: No overload variant of "zeros_like" matches argument types
# "BaseMaskedArray", "Type[bool]"
mask = np.zeros_like(self, dtype=bool) # type: ignore[call-overload]
mask = np.zeros_like(self, dtype=bool)
return BooleanArray(result, mask, copy=False)

def copy(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
Expand Down
10 changes: 2 additions & 8 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
try:
dtype = np.result_type(self.sp_values.dtype, type(fill_value))
except TypeError:
# error: Incompatible types in assignment (expression has type
# "Type[object]", variable has type "Union[str, dtype[Any], None]")
dtype = object # type: ignore[assignment]
dtype = object

out = np.full(self.shape, fill_value, dtype=dtype)
out[self.sp_index.to_int_index().indices] = self.sp_values
Expand Down Expand Up @@ -1449,11 +1447,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
return type(self)(result)

def __abs__(self):
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
# "SparseArray"; expected "Union[Union[int, float, complex, str, bytes,
# generic], Sequence[Union[int, float, complex, str, bytes, generic]],
# Sequence[Sequence[Any]], _SupportsArray]"
return np.abs(self) # type: ignore[arg-type]
return np.abs(self)

# ------------------------------------------------------------------------
# Ops
Expand Down
8 changes: 1 addition & 7 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,7 @@ def fillna(self, value=None, method=None, limit=None):
if mask.any():
if method is not None:
func = missing.get_fill_func(method)
# error: Argument 1 to "to_numpy" of "ArrowStringArray" has incompatible
# type "Type[object]"; expected "Union[str, dtype[Any], None]"
new_values, _ = func(
self.to_numpy(object), # type: ignore[arg-type]
limit=limit,
mask=mask,
)
new_values, _ = func(self.to_numpy(object), limit=limit, mask=mask)
new_values = self._from_sequence(new_values)
else:
# fill with value
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,7 @@ def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
# expected "ndarray")
return values._values # type: ignore[return-value]

# error: Non-overlapping container check (element type: "Union[str, dtype[Any],
# None]", container item type: "type")
if isinstance(values, list) and dtype in [ # type: ignore[comparison-overlap]
np.object_,
object,
]:
if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)

result = np.asarray(values, dtype=dtype)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,7 @@ def _sanitize_ndim(
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
# i.e. PandasDtype("O")

# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
# "Type[object]"; expected "Union[str, dtype[Any], None]"
result = com.asarray_tuplesafe(data, dtype=object) # type: ignore[arg-type]
result = com.asarray_tuplesafe(data, dtype=object)
cls = dtype.construct_array_type()
result = cls._from_sequence(result, dtype=dtype)
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ def __init__(self, dtype: NpDtype | PandasDtype | None):
if isinstance(dtype, PandasDtype):
# make constructor univalent
dtype = dtype.numpy_dtype
self._dtype = np.dtype(dtype)
self._dtype: np.dtype = np.dtype(dtype)

def __repr__(self) -> str:
return f"PandasDtype({repr(self.name)})"
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10048,11 +10048,7 @@ def abs(self: FrameOrSeries) -> FrameOrSeries:
2 6 30 -30
3 7 40 -50
"""
# error: Argument 1 to "__call__" of "ufunc" has incompatible type
# "FrameOrSeries"; expected "Union[Union[int, float, complex, str, bytes,
# generic], Sequence[Union[int, float, complex, str, bytes, generic]],
# Sequence[Sequence[Any]], _SupportsArray]"
return np.abs(self) # type: ignore[arg-type]
return np.abs(self)

@final
def describe(
Expand Down
14 changes: 2 additions & 12 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ def __new__(
name: Hashable = None,
) -> RangeIndex:

# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
# Type[int], Type[complex], Type[bool], Type[object]]"
cls._validate_dtype(dtype) # type: ignore[arg-type]
cls._validate_dtype(dtype)
name = maybe_extract_name(name, start, cls)

# RangeIndex
Expand Down Expand Up @@ -160,12 +155,7 @@ def from_range(
f"range, {repr(data)} was passed"
)

# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
# Type[int], Type[complex], Type[bool], Type[object]]"
cls._validate_dtype(dtype) # type: ignore[arg-type]
cls._validate_dtype(dtype)
return cls._simple_new(data, name=name)

@classmethod
Expand Down
7 changes: 3 additions & 4 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,10 +933,9 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
)
if dtype is not None:
try:
# error: Argument 1 to "dtype" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[object]]";
# expected "Type[Any]"
dtype = np.dtype(dtype) # type: ignore[arg-type]
# error: No overload variant of "dtype" matches argument type
# "object"
dtype = np.dtype(dtype) # type: ignore[call-overload]
return data.astype(dtype), True
except (TypeError, ValueError):
return data, False
Expand Down
8 changes: 6 additions & 2 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,9 @@ def _sqlalchemy_type(self, col):
if is_dict_like(dtype):
dtype = cast(dict, dtype)
if col.name in dtype:
return dtype[col.name]
# error: TypedDict key must be a string literal; expected one of
# ('names', 'formats', 'offsets', 'titles', 'itemsize', ...)
return dtype[col.name] # type: ignore[misc]

# Infer type of column, while ignoring missing values.
# Needed for inserting typed data containing NULLs, GH 8778.
Expand Down Expand Up @@ -1841,7 +1843,9 @@ def _sql_type_name(self, col):
if is_dict_like(dtype):
dtype = cast(dict, dtype)
if col.name in dtype:
return dtype[col.name]
# error: TypedDict key must be a string literal; expected one of
# ('names', 'formats', 'offsets', 'titles', 'itemsize', ...)
return dtype[col.name] # type: ignore[misc]

# Infer type of column, while ignoring missing values.
# Needed for inserting typed data containing NULLs, GH 8778.
Expand Down