Skip to content

Commit 449eaae

Browse files
authored
TYP: type some un-typed decorators (#43400)
1 parent d16afea commit 449eaae

File tree

8 files changed

+35
-22
lines changed

8 files changed

+35
-22
lines changed

pandas/core/indexes/multi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pandas._typing import (
3030
AnyArrayLike,
3131
DtypeObj,
32+
F,
3233
Scalar,
3334
Shape,
3435
npt,
@@ -185,7 +186,7 @@ def _codes_to_ints(self, codes):
185186
return np.bitwise_or.reduce(codes, axis=1)
186187

187188

188-
def names_compat(meth):
189+
def names_compat(meth: F) -> F:
189190
"""
190191
A decorator to allow either `name` or `names` keyword but not both.
191192
@@ -201,7 +202,7 @@ def new_meth(self_or_cls, *args, **kwargs):
201202

202203
return meth(self_or_cls, *args, **kwargs)
203204

204-
return new_meth
205+
return cast(F, new_meth)
205206

206207

207208
class MultiIndex(Index):

pandas/core/nanops.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def set_use_bottleneck(v: bool = True) -> None:
7272

7373

7474
class disallow:
75-
def __init__(self, *dtypes):
75+
def __init__(self, *dtypes: Dtype):
7676
super().__init__()
7777
self.dtypes = tuple(pandas_dtype(dtype).type for dtype in dtypes)
7878

@@ -449,7 +449,7 @@ def _na_for_min_count(values: np.ndarray, axis: int | None) -> Scalar | np.ndarr
449449
return np.full(result_shape, fill_value, dtype=values.dtype)
450450

451451

452-
def maybe_operate_rowwise(func):
452+
def maybe_operate_rowwise(func: F) -> F:
453453
"""
454454
NumPy operations on C-contiguous ndarrays with axis=1 can be
455455
very slow. Operate row-by-row and concatenate the results.
@@ -475,7 +475,7 @@ def newfunc(values: np.ndarray, *, axis: int | None = None, **kwargs):
475475

476476
return func(values, axis=axis, **kwargs)
477477

478-
return newfunc
478+
return cast(F, newfunc)
479479

480480

481481
def nanany(

pandas/core/window/expanding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Expanding(RollingAndExpandingMixin):
9696
4 7.0
9797
"""
9898

99-
_attributes = ["min_periods", "center", "axis", "method"]
99+
_attributes: list[str] = ["min_periods", "center", "axis", "method"]
100100

101101
def __init__(
102102
self,

pandas/core/window/rolling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ class BaseWindowGroupby(BaseWindow):
583583

584584
_grouper: BaseGrouper
585585
_as_index: bool
586-
_attributes = ["_grouper"]
586+
_attributes: list[str] = ["_grouper"]
587587

588588
def __init__(
589589
self,
@@ -1500,7 +1500,7 @@ def corr_func(x, y):
15001500

15011501
class Rolling(RollingAndExpandingMixin):
15021502

1503-
_attributes = [
1503+
_attributes: list[str] = [
15041504
"window",
15051505
"min_periods",
15061506
"center",

pandas/io/pytables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4549,10 +4549,10 @@ def read(
45494549

45504550
# we could have a multi-index constructor here
45514551
# ensure_index doesn't recognized our list-of-tuples here
4552-
if info.get("type") == "MultiIndex":
4553-
cols = MultiIndex.from_tuples(index_vals)
4554-
else:
4552+
if info.get("type") != "MultiIndex":
45554553
cols = Index(index_vals)
4554+
else:
4555+
cols = MultiIndex.from_tuples(index_vals)
45564556

45574557
names = info.get("names")
45584558
if names is not None:

pandas/plotting/_matplotlib/converter.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
tzinfo,
99
)
1010
import functools
11-
from typing import Any
11+
from typing import (
12+
Any,
13+
cast,
14+
)
1215

1316
from dateutil.relativedelta import relativedelta
1417
import matplotlib.dates as dates
@@ -28,6 +31,7 @@
2831
)
2932
from pandas._libs.tslibs.dtypes import FreqGroup
3033
from pandas._libs.tslibs.offsets import BaseOffset
34+
from pandas._typing import F
3135

3236
from pandas.core.dtypes.common import (
3337
is_float,
@@ -76,7 +80,7 @@ def get_pairs():
7680
return pairs
7781

7882

79-
def register_pandas_matplotlib_converters(func):
83+
def register_pandas_matplotlib_converters(func: F) -> F:
8084
"""
8185
Decorator applying pandas_converters.
8286
"""
@@ -86,7 +90,7 @@ def wrapper(*args, **kwargs):
8690
with pandas_converters():
8791
return func(*args, **kwargs)
8892

89-
return wrapper
93+
return cast(F, wrapper)
9094

9195

9296
@contextlib.contextmanager

pandas/plotting/_matplotlib/core.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,9 @@ def _make_plot(self):
12481248
left, right = get_xlim(lines)
12491249
ax.set_xlim(left, right)
12501250

1251+
# error: Signature of "_plot" incompatible with supertype "MPLPlot"
12511252
@classmethod
1252-
def _plot(
1253+
def _plot( # type: ignore[override]
12531254
cls, ax: Axes, x, y, style=None, column_num=None, stacking_id=None, **kwds
12541255
):
12551256
# column_num is used to get the target column from plotf in line and
@@ -1383,16 +1384,17 @@ def __init__(self, data, **kwargs):
13831384
if self.logy or self.loglog:
13841385
raise ValueError("Log-y scales are not supported in area plot")
13851386

1387+
# error: Signature of "_plot" incompatible with supertype "MPLPlot"
13861388
@classmethod
1387-
def _plot(
1389+
def _plot( # type: ignore[override]
13881390
cls,
13891391
ax: Axes,
13901392
x,
13911393
y,
13921394
style=None,
13931395
column_num=None,
13941396
stacking_id=None,
1395-
is_errorbar=False,
1397+
is_errorbar: bool = False,
13961398
**kwds,
13971399
):
13981400

@@ -1483,8 +1485,11 @@ def _args_adjust(self):
14831485
if is_list_like(self.left):
14841486
self.left = np.array(self.left)
14851487

1488+
# error: Signature of "_plot" incompatible with supertype "MPLPlot"
14861489
@classmethod
1487-
def _plot(cls, ax: Axes, x, y, w, start=0, log=False, **kwds):
1490+
def _plot( # type: ignore[override]
1491+
cls, ax: Axes, x, y, w, start=0, log=False, **kwds
1492+
):
14881493
return ax.bar(x, y, w, bottom=start, log=log, **kwds)
14891494

14901495
@property
@@ -1601,8 +1606,11 @@ class BarhPlot(BarPlot):
16011606
def _start_base(self):
16021607
return self.left
16031608

1609+
# error: Signature of "_plot" incompatible with supertype "MPLPlot"
16041610
@classmethod
1605-
def _plot(cls, ax: Axes, x, y, w, start=0, log=False, **kwds):
1611+
def _plot( # type: ignore[override]
1612+
cls, ax: Axes, x, y, w, start=0, log=False, **kwds
1613+
):
16061614
return ax.barh(x, y, w, left=start, log=log, **kwds)
16071615

16081616
def _decorate_ticks(self, ax: Axes, name, ticklabels, start_edge, end_edge):

pandas/tests/frame/methods/test_sort_index.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,22 +441,22 @@ def test_sort_index_ignore_index(
441441
{"M1": [1, 2], "M2": [3, 4]},
442442
True,
443443
False,
444-
MultiIndex.from_tuples([[2, 1], [3, 4]], names=list("AB")),
444+
MultiIndex.from_tuples([(2, 1), (3, 4)], names=list("AB")),
445445
),
446446
(
447447
{"M1": [1, 2], "M2": [3, 4]},
448448
{"M1": [2, 1], "M2": [4, 3]},
449449
False,
450450
False,
451-
MultiIndex.from_tuples([[3, 4], [2, 1]], names=list("AB")),
451+
MultiIndex.from_tuples([(3, 4), (2, 1)], names=list("AB")),
452452
),
453453
],
454454
)
455455
def test_sort_index_ignore_index_multi_index(
456456
self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index
457457
):
458458
# GH 30114, this is to test ignore_index on MulitIndex of index
459-
mi = MultiIndex.from_tuples([[2, 1], [3, 4]], names=list("AB"))
459+
mi = MultiIndex.from_tuples([(2, 1), (3, 4)], names=list("AB"))
460460
df = DataFrame(original_dict, index=mi)
461461
expected_df = DataFrame(sorted_dict, index=output_index)
462462

0 commit comments

Comments
 (0)