Skip to content

Commit 5c175c5

Browse files
twoertweinnoatamir
authored andcommitted
TYP: type all arguments with int default values (pandas-dev#48761)
* TYP: type all arguments with int default values * multiindex * _get_ordinal_range * fix plotting/_matplotlib/hist.py * mark only bins as keyword-only
1 parent 02725fb commit 5c175c5

34 files changed

+241
-145
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ repos:
268268
|/_testing/
269269
- id: autotyping
270270
name: autotyping
271-
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics --bool-param --bytes-param --str-param --float-param
271+
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --aggressive
272272
types_or: [python, pyi]
273273
files: ^pandas
274274
exclude: ^(pandas/tests|pandas/_version.py|pandas/io/clipboard)

pandas/_config/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def _build_option_description(k: str) -> str:
704704
return s
705705

706706

707-
def pp_options_list(keys: Iterable[str], width=80, _print: bool = False):
707+
def pp_options_list(keys: Iterable[str], width: int = 80, _print: bool = False):
708708
"""Builds a concise listing of available options, grouped by prefix"""
709709
from itertools import groupby
710710
from textwrap import wrap

pandas/_testing/__init__.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -341,33 +341,35 @@ def getCols(k) -> str:
341341

342342

343343
# make index
344-
def makeStringIndex(k=10, name=None) -> Index:
344+
def makeStringIndex(k: int = 10, name=None) -> Index:
345345
return Index(rands_array(nchars=10, size=k), name=name)
346346

347347

348-
def makeCategoricalIndex(k=10, n=3, name=None, **kwargs) -> CategoricalIndex:
348+
def makeCategoricalIndex(
349+
k: int = 10, n: int = 3, name=None, **kwargs
350+
) -> CategoricalIndex:
349351
"""make a length k index or n categories"""
350352
x = rands_array(nchars=4, size=n, replace=False)
351353
return CategoricalIndex(
352354
Categorical.from_codes(np.arange(k) % n, categories=x), name=name, **kwargs
353355
)
354356

355357

356-
def makeIntervalIndex(k=10, name=None, **kwargs) -> IntervalIndex:
358+
def makeIntervalIndex(k: int = 10, name=None, **kwargs) -> IntervalIndex:
357359
"""make a length k IntervalIndex"""
358360
x = np.linspace(0, 100, num=(k + 1))
359361
return IntervalIndex.from_breaks(x, name=name, **kwargs)
360362

361363

362-
def makeBoolIndex(k=10, name=None) -> Index:
364+
def makeBoolIndex(k: int = 10, name=None) -> Index:
363365
if k == 1:
364366
return Index([True], name=name)
365367
elif k == 2:
366368
return Index([False, True], name=name)
367369
return Index([False, True] + [False] * (k - 2), name=name)
368370

369371

370-
def makeNumericIndex(k=10, name=None, *, dtype) -> NumericIndex:
372+
def makeNumericIndex(k: int = 10, name=None, *, dtype) -> NumericIndex:
371373
dtype = pandas_dtype(dtype)
372374
assert isinstance(dtype, np.dtype)
373375

@@ -385,21 +387,21 @@ def makeNumericIndex(k=10, name=None, *, dtype) -> NumericIndex:
385387
return NumericIndex(values, dtype=dtype, name=name)
386388

387389

388-
def makeIntIndex(k=10, name=None) -> Int64Index:
390+
def makeIntIndex(k: int = 10, name=None) -> Int64Index:
389391
base_idx = makeNumericIndex(k, name=name, dtype="int64")
390392
return Int64Index(base_idx)
391393

392394

393-
def makeUIntIndex(k=10, name=None) -> UInt64Index:
395+
def makeUIntIndex(k: int = 10, name=None) -> UInt64Index:
394396
base_idx = makeNumericIndex(k, name=name, dtype="uint64")
395397
return UInt64Index(base_idx)
396398

397399

398-
def makeRangeIndex(k=10, name=None, **kwargs) -> RangeIndex:
400+
def makeRangeIndex(k: int = 10, name=None, **kwargs) -> RangeIndex:
399401
return RangeIndex(0, k, 1, name=name, **kwargs)
400402

401403

402-
def makeFloatIndex(k=10, name=None) -> Float64Index:
404+
def makeFloatIndex(k: int = 10, name=None) -> Float64Index:
403405
base_idx = makeNumericIndex(k, name=name, dtype="float64")
404406
return Float64Index(base_idx)
405407

@@ -423,7 +425,7 @@ def makePeriodIndex(k: int = 10, name=None, **kwargs) -> PeriodIndex:
423425
return pd.period_range(start=dt, periods=k, freq="B", name=name, **kwargs)
424426

425427

426-
def makeMultiIndex(k=10, names=None, **kwargs):
428+
def makeMultiIndex(k: int = 10, names=None, **kwargs):
427429
N = (k // 2) + 1
428430
rng = range(N)
429431
mi = MultiIndex.from_product([("foo", "bar"), rng], names=names, **kwargs)
@@ -665,8 +667,8 @@ def makeCustomDataframe(
665667
ncols,
666668
c_idx_names: bool | list[str] = True,
667669
r_idx_names: bool | list[str] = True,
668-
c_idx_nlevels=1,
669-
r_idx_nlevels=1,
670+
c_idx_nlevels: int = 1,
671+
r_idx_nlevels: int = 1,
670672
data_gen_f=None,
671673
c_ndupe_l=None,
672674
r_ndupe_l=None,

pandas/core/arrays/_mixins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def _validate_searchsorted_value(
250250
return value
251251

252252
@doc(ExtensionArray.shift)
253-
def shift(self, periods=1, fill_value=None, axis=0):
253+
def shift(self, periods: int = 1, fill_value=None, axis: AxisInt = 0):
254254

255255
fill_value = self._validate_shift_value(fill_value)
256256
new_values = shift(self._ndarray, periods, axis, fill_value)

pandas/core/arrays/masked.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,12 @@ def _wrap_reduction_result(self, name: str, result, skipna, **kwargs):
10621062
return result
10631063

10641064
def sum(
1065-
self, *, skipna: bool = True, min_count=0, axis: AxisInt | None = 0, **kwargs
1065+
self,
1066+
*,
1067+
skipna: bool = True,
1068+
min_count: int = 0,
1069+
axis: AxisInt | None = 0,
1070+
**kwargs,
10661071
):
10671072
nv.validate_sum((), kwargs)
10681073

@@ -1085,7 +1090,12 @@ def sum(
10851090
)
10861091

10871092
def prod(
1088-
self, *, skipna: bool = True, min_count=0, axis: AxisInt | None = 0, **kwargs
1093+
self,
1094+
*,
1095+
skipna: bool = True,
1096+
min_count: int = 0,
1097+
axis: AxisInt | None = 0,
1098+
**kwargs,
10891099
):
10901100
nv.validate_prod((), kwargs)
10911101
result = masked_reductions.prod(

pandas/core/arrays/numpy_.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ def max(
243243
return self._wrap_reduction_result(axis, result)
244244

245245
def sum(
246-
self, *, axis: AxisInt | None = None, skipna: bool = True, min_count=0, **kwargs
246+
self,
247+
*,
248+
axis: AxisInt | None = None,
249+
skipna: bool = True,
250+
min_count: int = 0,
251+
**kwargs,
247252
) -> Scalar:
248253
nv.validate_sum((), kwargs)
249254
result = nanops.nansum(
@@ -252,7 +257,12 @@ def sum(
252257
return self._wrap_reduction_result(axis, result)
253258

254259
def prod(
255-
self, *, axis: AxisInt | None = None, skipna: bool = True, min_count=0, **kwargs
260+
self,
261+
*,
262+
axis: AxisInt | None = None,
263+
skipna: bool = True,
264+
min_count: int = 0,
265+
**kwargs,
256266
) -> Scalar:
257267
nv.validate_prod((), kwargs)
258268
result = nanops.nanprod(
@@ -294,7 +304,7 @@ def std(
294304
axis: AxisInt | None = None,
295305
dtype: NpDtype | None = None,
296306
out=None,
297-
ddof=1,
307+
ddof: int = 1,
298308
keepdims: bool = False,
299309
skipna: bool = True,
300310
):
@@ -310,7 +320,7 @@ def var(
310320
axis: AxisInt | None = None,
311321
dtype: NpDtype | None = None,
312322
out=None,
313-
ddof=1,
323+
ddof: int = 1,
314324
keepdims: bool = False,
315325
skipna: bool = True,
316326
):
@@ -326,7 +336,7 @@ def sem(
326336
axis: AxisInt | None = None,
327337
dtype: NpDtype | None = None,
328338
out=None,
329-
ddof=1,
339+
ddof: int = 1,
330340
keepdims: bool = False,
331341
skipna: bool = True,
332342
):

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def dt64arr_to_periodarr(
10671067
return c_dt64arr_to_periodarr(data.view("i8"), base, tz, reso=reso), freq
10681068

10691069

1070-
def _get_ordinal_range(start, end, periods, freq, mult=1):
1070+
def _get_ordinal_range(start, end, periods, freq, mult: int = 1):
10711071
if com.count_not_none(start, end, periods) != 2:
10721072
raise ValueError(
10731073
"Of the three parameters: start, end, and periods, "

pandas/core/arrays/sparse/array.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pandas._typing import (
3333
ArrayLike,
3434
AstypeArg,
35+
Axis,
3536
AxisInt,
3637
Dtype,
3738
NpDtype,
@@ -1479,7 +1480,7 @@ def all(self, axis=None, *args, **kwargs):
14791480

14801481
return values.all()
14811482

1482-
def any(self, axis=0, *args, **kwargs):
1483+
def any(self, axis: AxisInt = 0, *args, **kwargs):
14831484
"""
14841485
Tests whether at least one of elements evaluate True
14851486
@@ -1576,7 +1577,7 @@ def cumsum(self, axis: AxisInt = 0, *args, **kwargs) -> SparseArray:
15761577
fill_value=self.fill_value,
15771578
)
15781579

1579-
def mean(self, axis=0, *args, **kwargs):
1580+
def mean(self, axis: Axis = 0, *args, **kwargs):
15801581
"""
15811582
Mean of non-NA/null values
15821583

pandas/core/arrays/string_arrow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def _str_map(
310310
return lib.map_infer_mask(arr, f, mask.view("uint8"))
311311

312312
def _str_contains(
313-
self, pat, case: bool = True, flags=0, na=np.nan, regex: bool = True
313+
self, pat, case: bool = True, flags: int = 0, na=np.nan, regex: bool = True
314314
):
315315
if flags:
316316
fallback_performancewarning()

pandas/core/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import pandas._libs.lib as lib
2626
from pandas._typing import (
2727
ArrayLike,
28+
Axis,
2829
AxisInt,
2930
DtypeObj,
3031
IndexLabel,
@@ -784,7 +785,7 @@ def _reduce(
784785
op,
785786
name: str,
786787
*,
787-
axis=0,
788+
axis: Axis = 0,
788789
skipna: bool = True,
789790
numeric_only=None,
790791
filter_type=None,

pandas/core/computation/eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def eval(
176176
local_dict=None,
177177
global_dict=None,
178178
resolvers=(),
179-
level=0,
179+
level: int = 0,
180180
target=None,
181181
inplace: bool = False,
182182
):

pandas/core/dtypes/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def _concatenate_2d(to_concat, axis: AxisInt):
337337
return np.concatenate(to_concat, axis=axis)
338338

339339

340-
def _concat_datetime(to_concat, axis=0):
340+
def _concat_datetime(to_concat, axis: AxisInt = 0):
341341
"""
342342
provide concatenation of an datetimelike array of arrays each of which is a
343343
single M8[ns], datetime64[ns, tz] or m8[ns] dtype

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10867,7 +10867,7 @@ def func(values: np.ndarray):
1086710867
# We only use this in the case that operates on self.values
1086810868
return op(values, axis=axis, skipna=skipna, **kwds)
1086910869

10870-
def blk_func(values, axis=1):
10870+
def blk_func(values, axis: Axis = 1):
1087110871
if isinstance(values, ExtensionArray):
1087210872
if not is_1d_only_ea_dtype(values.dtype) and not isinstance(
1087310873
self._mgr, ArrayManager

0 commit comments

Comments
 (0)