Skip to content

Commit 1cc40fa

Browse files
authored
TYP: fix type-ignores in core (pandas-dev#40423)
1 parent 454194e commit 1cc40fa

File tree

20 files changed

+62
-119
lines changed

20 files changed

+62
-119
lines changed

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ def _sort_mixed(values):
18671867
return np.concatenate([nums, np.asarray(strs, dtype=object)])
18681868

18691869

1870-
def _sort_tuples(values: np.ndarray):
1870+
def _sort_tuples(values: np.ndarray) -> np.ndarray:
18711871
"""
18721872
Convert array of tuples (1d) to array or array (2d).
18731873
We need to keep the columns separately as they contain different types and

pandas/core/arraylike.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
ExtensionArray
66
"""
77
import operator
8-
from typing import (
9-
Any,
10-
Callable,
11-
)
8+
from typing import Any
129
import warnings
1310

1411
import numpy as np
@@ -172,7 +169,7 @@ def _is_aligned(frame, other):
172169
return frame.columns.equals(other.index)
173170

174171

175-
def _maybe_fallback(ufunc: Callable, method: str, *inputs: Any, **kwargs: Any):
172+
def _maybe_fallback(ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any):
176173
"""
177174
In the future DataFrame, inputs to ufuncs will be aligned before applying
178175
the ufunc, but for now we ignore the index but raise a warning if behaviour

pandas/core/arrays/boolean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def map_string(s):
331331

332332
_HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_)
333333

334-
def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
334+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
335335
# For BooleanArray inputs, we apply the ufunc to ._data
336336
# and mask the result.
337337
if method == "reduce":

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
13881388
# ndarray.
13891389
return np.asarray(ret)
13901390

1391-
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
1391+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
13921392
# for binary ops, use our custom dunder methods
13931393
result = ops.maybe_dispatch_ufunc_to_dunder_op(
13941394
self, ufunc, method, *inputs, **kwargs
@@ -2429,7 +2429,7 @@ def replace(self, to_replace, value, inplace: bool = False):
24292429

24302430
# ------------------------------------------------------------------------
24312431
# String methods interface
2432-
def _str_map(self, f, na_value=np.nan, dtype=np.dtype(object)):
2432+
def _str_map(self, f, na_value=np.nan, dtype=np.dtype("object")):
24332433
# Optimization to apply the callable `f` to the categories once
24342434
# and rebuild the result by `take`ing from the result with the codes.
24352435
# Returns the same type as the object-dtype implementation though.

pandas/core/arrays/numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _arith_method(self, other, op):
152152

153153
_HANDLED_TYPES = (np.ndarray, numbers.Number)
154154

155-
def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
155+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
156156
# For NumericArray inputs, we apply the ufunc to ._data
157157
# and mask the result.
158158
if method == "reduce":

pandas/core/arrays/numpy_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
137137

138138
_HANDLED_TYPES = (np.ndarray, numbers.Number)
139139

140-
def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
140+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
141141
# Lightly modified version of
142142
# https://numpy.org/doc/stable/reference/generated/numpy.lib.mixins.NDArrayOperatorsMixin.html
143143
# The primary modification is not boxing scalar return values

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ def mean(self, axis=0, *args, **kwargs):
13961396

13971397
_HANDLED_TYPES = (np.ndarray, numbers.Number)
13981398

1399-
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
1399+
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
14001400
out = kwargs.get("out", ())
14011401

14021402
for x in inputs + out:

pandas/core/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Dtype,
2424
DtypeObj,
2525
IndexLabel,
26+
Shape,
2627
)
2728
from pandas.compat import PYPY
2829
from pandas.compat.numpy import function as nv
@@ -389,7 +390,7 @@ def transpose(self: _T, *args, **kwargs) -> _T:
389390
)
390391

391392
@property
392-
def shape(self):
393+
def shape(self) -> Shape:
393394
"""
394395
Return a tuple of the shape of the underlying data.
395396
"""
@@ -511,7 +512,7 @@ def to_numpy(
511512
copy: bool = False,
512513
na_value=lib.no_default,
513514
**kwargs,
514-
):
515+
) -> np.ndarray:
515516
"""
516517
A NumPy ndarray representing the values in this Series or Index.
517518
@@ -852,7 +853,7 @@ def __iter__(self):
852853
return map(self._values.item, range(self._values.size))
853854

854855
@cache_readonly
855-
def hasnans(self):
856+
def hasnans(self) -> bool:
856857
"""
857858
Return if I have any nans; enables various perf speedups.
858859
"""

pandas/core/dtypes/cast.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,14 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
689689
if fv.tz is None:
690690
return dtype, fv.asm8
691691

692-
# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
693-
return np.dtype(object), fill_value # type: ignore[type-var]
692+
return np.dtype("object"), fill_value
694693

695694
elif issubclass(dtype.type, np.timedelta64):
696695
inferred, fv = infer_dtype_from_scalar(fill_value, pandas_dtype=True)
697696
if inferred == dtype:
698697
return dtype, fv
699-
# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
700-
return np.dtype(object), fill_value # type: ignore[type-var]
698+
699+
return np.dtype("object"), fill_value
701700

702701
elif is_float(fill_value):
703702
if issubclass(dtype.type, np.bool_):

pandas/core/dtypes/missing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,7 @@ def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool:
648648
# Numeric
649649
return obj is not NaT and not isinstance(obj, (np.datetime64, np.timedelta64))
650650

651-
# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
652-
elif dtype == np.dtype(object): # type: ignore[type-var]
651+
elif dtype == np.dtype("object"):
653652
# This is needed for Categorical, but is kind of weird
654653
return True
655654

0 commit comments

Comments
 (0)