diff --git a/pandas/_typing.py b/pandas/_typing.py index a58dc0dba1bf1..c9031b52a90d1 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -11,7 +11,7 @@ ) from mmap import mmap from os import PathLike -from typing import ( +from typing import ( # noqa: F401 IO, TYPE_CHECKING, Any, @@ -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 @@ -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] # DtypeArg specifies all allowable dtypes in a functions its dtype argument DtypeArg = Union[Dtype, Dict[Hashable, Dtype]] DtypeObj = Union[np.dtype, "ExtensionDtype"] diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 86cde647cc798..155be0cabcc96 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -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"): diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index 11f9f645920ec..cee8b1c2bc24a 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -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) @@ -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: diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 37898ce682e4f..1fb31779f2f48 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -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 @@ -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 diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index b7a0e70180ae4..682911f4f2d6a 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -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 diff --git a/pandas/core/common.py b/pandas/core/common.py index 04ff2d2c4618f..1296a6b1447c7 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index f3133480108a6..eb9a5207dafba 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -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: diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index c5efd8f77495c..e4984f81eb857 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -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)})" diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8628429f18b05..e6c51cf3c206a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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( diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 0a2c0820f20a3..8ce402227de29 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -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 @@ -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 diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index b7493ebeadf34..99e783c0ac330 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -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 diff --git a/pandas/io/sql.py b/pandas/io/sql.py index d797fa51984d6..8dd7325c58658 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -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. @@ -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.