diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index a888bfabd6f80..15f54c11be0a0 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1867,7 +1867,7 @@ def _sort_mixed(values): return np.concatenate([nums, np.asarray(strs, dtype=object)]) -def _sort_tuples(values: np.ndarray): +def _sort_tuples(values: np.ndarray) -> np.ndarray: """ Convert array of tuples (1d) to array or array (2d). We need to keep the columns separately as they contain different types and diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py index 588fe8adc7241..b110d62b606d9 100644 --- a/pandas/core/arraylike.py +++ b/pandas/core/arraylike.py @@ -5,10 +5,7 @@ ExtensionArray """ import operator -from typing import ( - Any, - Callable, -) +from typing import Any import warnings import numpy as np @@ -172,7 +169,7 @@ def _is_aligned(frame, other): return frame.columns.equals(other.index) -def _maybe_fallback(ufunc: Callable, method: str, *inputs: Any, **kwargs: Any): +def _maybe_fallback(ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any): """ In the future DataFrame, inputs to ufuncs will be aligned before applying the ufunc, but for now we ignore the index but raise a warning if behaviour diff --git a/pandas/core/arrays/boolean.py b/pandas/core/arrays/boolean.py index 4258279e37551..5455b0b92a179 100644 --- a/pandas/core/arrays/boolean.py +++ b/pandas/core/arrays/boolean.py @@ -331,7 +331,7 @@ def map_string(s): _HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_) - def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # For BooleanArray inputs, we apply the ufunc to ._data # and mask the result. if method == "reduce": diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 0062ed01e957a..eb4bbb14a0135 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1388,7 +1388,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray: # ndarray. return np.asarray(ret) - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # for binary ops, use our custom dunder methods result = ops.maybe_dispatch_ufunc_to_dunder_op( self, ufunc, method, *inputs, **kwargs @@ -2429,7 +2429,7 @@ def replace(self, to_replace, value, inplace: bool = False): # ------------------------------------------------------------------------ # String methods interface - def _str_map(self, f, na_value=np.nan, dtype=np.dtype(object)): + def _str_map(self, f, na_value=np.nan, dtype=np.dtype("object")): # Optimization to apply the callable `f` to the categories once # and rebuild the result by `take`ing from the result with the codes. # Returns the same type as the object-dtype implementation though. diff --git a/pandas/core/arrays/numeric.py b/pandas/core/arrays/numeric.py index f06099a642833..a5ead2485801b 100644 --- a/pandas/core/arrays/numeric.py +++ b/pandas/core/arrays/numeric.py @@ -152,7 +152,7 @@ def _arith_method(self, other, op): _HANDLED_TYPES = (np.ndarray, numbers.Number) - def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # For NumericArray inputs, we apply the ufunc to ._data # and mask the result. if method == "reduce": diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 5ef3c24726924..89988349132e6 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -137,7 +137,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray: _HANDLED_TYPES = (np.ndarray, numbers.Number) - def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # Lightly modified version of # https://numpy.org/doc/stable/reference/generated/numpy.lib.mixins.NDArrayOperatorsMixin.html # The primary modification is not boxing scalar return values diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index c798870e4126a..f5dc95590c963 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1396,7 +1396,7 @@ def mean(self, axis=0, *args, **kwargs): _HANDLED_TYPES = (np.ndarray, numbers.Number) - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): out = kwargs.get("out", ()) for x in inputs + out: diff --git a/pandas/core/base.py b/pandas/core/base.py index 56ec2597314b2..f30430dd394ca 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -23,6 +23,7 @@ Dtype, DtypeObj, IndexLabel, + Shape, ) from pandas.compat import PYPY from pandas.compat.numpy import function as nv @@ -389,7 +390,7 @@ def transpose(self: _T, *args, **kwargs) -> _T: ) @property - def shape(self): + def shape(self) -> Shape: """ Return a tuple of the shape of the underlying data. """ @@ -511,7 +512,7 @@ def to_numpy( copy: bool = False, na_value=lib.no_default, **kwargs, - ): + ) -> np.ndarray: """ A NumPy ndarray representing the values in this Series or Index. @@ -852,7 +853,7 @@ def __iter__(self): return map(self._values.item, range(self._values.size)) @cache_readonly - def hasnans(self): + def hasnans(self) -> bool: """ Return if I have any nans; enables various perf speedups. """ diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index ce91276bc6cf4..be535495de8d0 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -689,15 +689,14 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan): if fv.tz is None: return dtype, fv.asm8 - # error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object" - return np.dtype(object), fill_value # type: ignore[type-var] + return np.dtype("object"), fill_value elif issubclass(dtype.type, np.timedelta64): inferred, fv = infer_dtype_from_scalar(fill_value, pandas_dtype=True) if inferred == dtype: return dtype, fv - # error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object" - return np.dtype(object), fill_value # type: ignore[type-var] + + return np.dtype("object"), fill_value elif is_float(fill_value): if issubclass(dtype.type, np.bool_): diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index de981c39228ae..d9fbc3ed122fa 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -648,8 +648,7 @@ def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool: # Numeric return obj is not NaT and not isinstance(obj, (np.datetime64, np.timedelta64)) - # error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object" - elif dtype == np.dtype(object): # type: ignore[type-var] + elif dtype == np.dtype("object"): # This is needed for Categorical, but is kind of weird return True diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c048088ec2350..23c61773daf5a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -123,6 +123,7 @@ is_sequence, pandas_dtype, ) +from pandas.core.dtypes.dtypes import ExtensionDtype from pandas.core.dtypes.missing import ( isna, notna, @@ -584,25 +585,17 @@ def __init__( ) elif isinstance(data, dict): - # error: Argument "dtype" to "dict_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" - mgr = dict_to_mgr( - data, index, columns, dtype=dtype, typ=manager # type: ignore[arg-type] - ) + mgr = dict_to_mgr(data, index, columns, dtype=dtype, typ=manager) elif isinstance(data, ma.MaskedArray): import numpy.ma.mrecords as mrecords # masked recarray if isinstance(data, mrecords.MaskedRecords): - # error: Argument 4 to "rec_array_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" mgr = rec_array_to_mgr( data, index, columns, - dtype, # type: ignore[arg-type] + dtype, copy, typ=manager, ) @@ -611,13 +604,10 @@ def __init__( else: data = sanitize_masked_array(data) mgr = ndarray_to_mgr( - # error: Argument "dtype" to "ndarray_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; - # expected "Union[dtype[Any], ExtensionDtype, None]" data, index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, copy=copy, typ=manager, ) @@ -626,14 +616,11 @@ def __init__( if data.dtype.names: # i.e. numpy structured array - # error: Argument 4 to "rec_array_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" mgr = rec_array_to_mgr( data, index, columns, - dtype, # type: ignore[arg-type] + dtype, copy, typ=manager, ) @@ -642,24 +629,18 @@ def __init__( mgr = dict_to_mgr( # error: Item "ndarray" of "Union[ndarray, Series, Index]" has no # attribute "name" - # error: Argument "dtype" to "dict_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; - # expected "Union[dtype[Any], ExtensionDtype, None]" {data.name: data}, # type: ignore[union-attr] index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, typ=manager, ) else: mgr = ndarray_to_mgr( - # error: Argument "dtype" to "ndarray_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; - # expected "Union[dtype[Any], ExtensionDtype, None]" data, index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, copy=copy, typ=manager, ) @@ -680,46 +661,34 @@ def __init__( arrays, columns, index = nested_data_to_arrays( # error: Argument 3 to "nested_data_to_arrays" has incompatible # type "Optional[Collection[Any]]"; expected "Optional[Index]" - # error: Argument 4 to "nested_data_to_arrays" has incompatible - # type "Union[ExtensionDtype, str, dtype[Any], Type[object], - # None]"; expected "Union[dtype[Any], ExtensionDtype, None]" data, columns, index, # type: ignore[arg-type] - dtype, # type: ignore[arg-type] + dtype, ) mgr = arrays_to_mgr( - # error: Argument "dtype" to "arrays_to_mgr" has incompatible - # type "Union[ExtensionDtype, str, dtype[Any], Type[object], - # None]"; expected "Union[dtype[Any], ExtensionDtype, None]" arrays, columns, index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, typ=manager, ) else: mgr = ndarray_to_mgr( - # error: Argument "dtype" to "ndarray_to_mgr" has incompatible - # type "Union[ExtensionDtype, str, dtype[Any], Type[object], - # None]"; expected "Union[dtype[Any], ExtensionDtype, None]" data, index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, copy=copy, typ=manager, ) else: - # error: Argument "dtype" to "dict_to_mgr" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" mgr = dict_to_mgr( {}, index, columns, - dtype=dtype, # type: ignore[arg-type] + dtype=dtype, typ=manager, ) # For data is scalar @@ -731,16 +700,11 @@ def __init__( dtype, _ = infer_dtype_from_scalar(data, pandas_dtype=True) # For data is a scalar extension dtype - if is_extension_array_dtype(dtype): + if isinstance(dtype, ExtensionDtype): # TODO(EA2D): special case not needed with 2D EAs values = [ - # error: Argument 3 to "construct_1d_arraylike_from_scalar" - # has incompatible type "Union[ExtensionDtype, str, dtype, - # Type[object]]"; expected "Union[dtype, ExtensionDtype]" - construct_1d_arraylike_from_scalar( - data, len(index), dtype # type: ignore[arg-type] - ) + construct_1d_arraylike_from_scalar(data, len(index), dtype) for _ in range(len(columns)) ] mgr = arrays_to_mgr( @@ -750,13 +714,10 @@ def __init__( # error: Incompatible types in assignment (expression has type # "ndarray", variable has type "List[ExtensionArray]") values = construct_2d_arraylike_from_scalar( # type: ignore[assignment] - # error: Argument 4 to "construct_2d_arraylike_from_scalar" has - # incompatible type "Union[ExtensionDtype, str, dtype[Any], - # Type[object]]"; expected "dtype[Any]" data, len(index), len(columns), - dtype, # type: ignore[arg-type] + dtype, copy, ) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 67533259ae0c2..050550f8add50 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -44,6 +44,7 @@ CompressionOptions, Dtype, DtypeArg, + DtypeObj, FilePathOrBuffer, FrameOrSeries, IndexKeyFunc, @@ -411,7 +412,7 @@ def set_flags( @final @classmethod - def _validate_dtype(cls, dtype): + def _validate_dtype(cls, dtype) -> Optional[DtypeObj]: """ validate the passed dtype """ if dtype is not None: dtype = pandas_dtype(dtype) @@ -1995,13 +1996,9 @@ def __array_wrap__( ) def __array_ufunc__( - self, ufunc: Callable, method: str, *inputs: Any, **kwargs: Any + self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any ): - # error: Argument 2 to "array_ufunc" has incompatible type "Callable[..., Any]"; - # expected "ufunc" - return arraylike.array_ufunc( - self, ufunc, method, *inputs, **kwargs # type: ignore[arg-type] - ) + return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs) # ideally we would define this to avoid the getattr checks, but # is slower @@ -4900,7 +4897,6 @@ def _reindex_axes( return obj - @final def _needs_reindex_multi(self, axes, method, level) -> bool_t: """Check if we do need a multi reindex.""" return ( @@ -6998,10 +6994,7 @@ def interpolate( f"`limit_direction` must be 'backward' for method `{method}`" ) - # error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object" - if obj.ndim == 2 and np.all( - obj.dtypes == np.dtype(object) # type: ignore[type-var] - ): + if obj.ndim == 2 and np.all(obj.dtypes == np.dtype("object")): raise TypeError( "Cannot interpolate with all object-dtype columns " "in the DataFrame. Try setting at least one " @@ -8488,15 +8481,12 @@ def ranker(data): na_option=na_option, pct=pct, ) - # error: Incompatible types in assignment (expression has type - # "FrameOrSeries", variable has type "ndarray") # error: Argument 1 to "NDFrame" has incompatible type "ndarray"; expected # "Union[ArrayManager, BlockManager]" - ranks = self._constructor( # type: ignore[assignment] + ranks_obj = self._constructor( ranks, **data._construct_axes_dict() # type: ignore[arg-type] ) - # error: "ndarray" has no attribute "__finalize__" - return ranks.__finalize__(self, method="rank") # type: ignore[attr-defined] + return ranks_obj.__finalize__(self, method="rank") # if numeric_only is None, and we can't get anything, we try with # numeric_only=True diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 26d25645b02c6..2597724b3d948 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4648,7 +4648,7 @@ def _concat(self, to_concat: List[Index], name: Hashable) -> Index: result = concat_compat(to_concat_vals) return Index(result, name=name) - def putmask(self, mask, value): + def putmask(self, mask, value) -> Index: """ Return a new Index of the values set with the mask. diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 62941a23c6459..5cdf4c1ecef55 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -15,6 +15,7 @@ from pandas._typing import ( ArrayLike, Dtype, + DtypeObj, ) from pandas.util._decorators import ( Appender, @@ -538,7 +539,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind): # -------------------------------------------------------------------- - def _is_comparable_dtype(self, dtype): + def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: return self.categories._is_comparable_dtype(dtype) def take_nd(self, *args, **kwargs): diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 4c15e9df534ba..f714da0d0e303 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -430,7 +430,7 @@ def insert(self: _T, loc: int, item) -> _T: new_arr = arr._from_backing_data(new_vals) return type(self)._simple_new(new_arr, name=self.name) - def putmask(self, mask, value): + def putmask(self, mask, value) -> Index: res_values = self._data.copy() try: res_values.putmask(mask, value) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 86ff95a588217..eea66a481a72f 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -836,14 +836,14 @@ def right(self) -> Index: return Index(self._data.right, copy=False) @cache_readonly - def mid(self): + def mid(self) -> Index: return Index(self._data.mid, copy=False) @property - def length(self): + def length(self) -> Index: return Index(self._data.length, copy=False) - def putmask(self, mask, value): + def putmask(self, mask, value) -> Index: mask, noop = validate_putmask(self._data, mask) if noop: return self.copy() @@ -891,7 +891,7 @@ def _format_native_types(self, na_rep="NaN", quoting=None, **kwargs): # GH 28210: use base method but with different default na_rep return super()._format_native_types(na_rep=na_rep, quoting=quoting, **kwargs) - def _format_data(self, name=None): + def _format_data(self, name=None) -> str: # TODO: integrate with categorical and make generic # name argument is unused here; just for compat with base / categorical diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 05bb32dad6cab..e446786802239 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -188,7 +188,7 @@ def _constructor(self) -> Type[Int64Index]: return Int64Index @cache_readonly - def _data(self): + def _data(self) -> np.ndarray: """ An int array that for performance reasons is created only when needed. @@ -201,7 +201,7 @@ def _cached_int64index(self) -> Int64Index: return Int64Index._simple_new(self._data, name=self.name) @property - def _int64index(self): + def _int64index(self) -> Int64Index: # wrap _cached_int64index so we can be sure its name matches self.name res = self._cached_int64index res._name = self._name @@ -425,13 +425,15 @@ def _get_indexer(self, target: Index, method=None, limit=None, tolerance=None): # -------------------------------------------------------------------- - def repeat(self, repeats, axis=None): + def repeat(self, repeats, axis=None) -> Int64Index: return self._int64index.repeat(repeats, axis=axis) - def delete(self, loc): + def delete(self, loc) -> Int64Index: return self._int64index.delete(loc) - def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): + def take( + self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs + ) -> Int64Index: with rewrite_exception("Int64Index", type(self).__name__): return self._int64index.take( indices, diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 48b2084319292..c2193056cc974 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -679,7 +679,7 @@ def interpolate_2d( return result -def _fillna_prep(values, mask=None): +def _fillna_prep(values, mask: Optional[np.ndarray] = None) -> np.ndarray: # boilerplate for _pad_1d, _backfill_1d, _pad_2d, _backfill_2d if mask is None: @@ -717,9 +717,7 @@ def _pad_1d( ) -> tuple[np.ndarray, np.ndarray]: mask = _fillna_prep(values, mask) algos.pad_inplace(values, mask, limit=limit) - # error: Incompatible return value type (got "Tuple[ndarray, Optional[ndarray]]", - # expected "Tuple[ndarray, ndarray]") - return values, mask # type: ignore[return-value] + return values, mask @_datetimelike_compat @@ -730,9 +728,7 @@ def _backfill_1d( ) -> tuple[np.ndarray, np.ndarray]: mask = _fillna_prep(values, mask) algos.backfill_inplace(values, mask, limit=limit) - # error: Incompatible return value type (got "Tuple[ndarray, Optional[ndarray]]", - # expected "Tuple[ndarray, ndarray]") - return values, mask # type: ignore[return-value] + return values, mask @_datetimelike_compat diff --git a/pandas/core/series.py b/pandas/core/series.py index 9feec7acae4c6..1f5a42265ec98 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -267,7 +267,9 @@ class Series(base.IndexOpsMixin, generic.NDFrame): ) # Override cache_readonly bc Series is mutable - hasnans = property( + # error: Incompatible types in assignment (expression has type "property", + # base class "IndexOpsMixin" defined the type as "Callable[[IndexOpsMixin], bool]") + hasnans = property( # type: ignore[assignment] base.IndexOpsMixin.hasnans.func, doc=base.IndexOpsMixin.hasnans.__doc__ ) __hash__ = generic.NDFrame.__hash__ @@ -404,12 +406,7 @@ def __init__( elif copy: data = data.copy() else: - # error: Argument 3 to "sanitize_array" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" - data = sanitize_array( - data, index, dtype, copy # type: ignore[arg-type] - ) + data = sanitize_array(data, index, dtype, copy) manager = get_option("mode.data_manager") if manager == "block": @@ -4231,7 +4228,7 @@ def _reindex_indexer(self, new_index, indexer, copy): ) return self._constructor(new_values, index=new_index) - def _needs_reindex_multi(self, axes, method, level): + def _needs_reindex_multi(self, axes, method, level) -> bool: """ Check if we do need a multi reindex; this is for compat with higher dims. diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index 58e5dc34d59d5..366b24e328642 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -110,7 +110,7 @@ def to_numpy( result = np.asarray([round(x, decimals) for x in result]) return result - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # if not all( isinstance(t, self._HANDLED_TYPES + (DecimalArray,)) for t in inputs