Skip to content

Commit 5c66e65

Browse files
authored
TYP: type all arguments with bool default values (#48624)
* TYP: type all arguments with bool default values * bool_t * ignore type error in pandas/core/arrays/sparse/accessor.py
1 parent 57f20ec commit 5c66e65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+341
-234
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ repos:
258258
|/_testing/
259259
- id: autotyping
260260
name: autotyping
261-
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics
261+
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics --bool-param
262262
types_or: [python, pyi]
263263
files: ^pandas
264-
exclude: ^(pandas/tests|pandas/io/clipboard)
264+
exclude: ^(pandas/tests|pandas/_version.py|pandas/io/clipboard)
265265
language: python
266266
additional_dependencies:
267267
- autotyping==22.9.0

pandas/_testing/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def equalContents(arr1, arr2) -> bool:
275275
return frozenset(arr1) == frozenset(arr2)
276276

277277

278-
def box_expected(expected, box_cls, transpose=True):
278+
def box_expected(expected, box_cls, transpose: bool = True):
279279
"""
280280
Helper function to wrap the expected output of a test in a given box_class.
281281
@@ -656,8 +656,8 @@ def keyfunc(x):
656656
def makeCustomDataframe(
657657
nrows,
658658
ncols,
659-
c_idx_names=True,
660-
r_idx_names=True,
659+
c_idx_names: bool | list[str] = True,
660+
r_idx_names: bool | list[str] = True,
661661
c_idx_nlevels=1,
662662
r_idx_nlevels=1,
663663
data_gen_f=None,
@@ -673,7 +673,7 @@ def makeCustomDataframe(
673673
Parameters
674674
----------
675675
nrows, ncols - number of data rows/cols
676-
c_idx_names, idx_names - False/True/list of strings, yields No names ,
676+
c_idx_names, r_idx_names - False/True/list of strings, yields No names ,
677677
default names or uses the provided names for the levels of the
678678
corresponding index. You can provide a single string when
679679
c_idx_nlevels ==1.

pandas/_testing/_io.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ def dec(f):
113113
return wrapper
114114

115115

116-
@optional_args
116+
# error: Untyped decorator makes function "network" untyped
117+
@optional_args # type: ignore[misc]
117118
def network(
118119
t,
119120
url="https://www.google.com",
120-
raise_on_error=False,
121-
check_before_test=False,
121+
raise_on_error: bool = False,
122+
check_before_test: bool = False,
122123
error_classes=None,
123124
skip_errnos=_network_errno_vals,
124125
_skip_on_messages=_network_error_messages,

pandas/_testing/_random.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def randbool(size=(), p: float = 0.5):
1414
)
1515

1616

17-
def rands_array(nchars, size, dtype="O", replace=True) -> np.ndarray:
17+
def rands_array(nchars, size, dtype="O", replace: bool = True) -> np.ndarray:
1818
"""
1919
Generate an array of byte strings.
2020
"""

pandas/_testing/asserters.py

+33-29
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,11 @@ def assert_is_sorted(seq) -> None:
523523

524524

525525
def assert_categorical_equal(
526-
left, right, check_dtype=True, check_category_order=True, obj="Categorical"
526+
left,
527+
right,
528+
check_dtype: bool = True,
529+
check_category_order: bool = True,
530+
obj="Categorical",
527531
) -> None:
528532
"""
529533
Test that Categoricals are equivalent.
@@ -618,7 +622,7 @@ def assert_period_array_equal(left, right, obj="PeriodArray") -> None:
618622

619623

620624
def assert_datetime_array_equal(
621-
left, right, obj="DatetimeArray", check_freq=True
625+
left, right, obj="DatetimeArray", check_freq: bool = True
622626
) -> None:
623627
__tracebackhide__ = True
624628
_check_isinstance(left, right, DatetimeArray)
@@ -630,7 +634,7 @@ def assert_datetime_array_equal(
630634

631635

632636
def assert_timedelta_array_equal(
633-
left, right, obj="TimedeltaArray", check_freq=True
637+
left, right, obj="TimedeltaArray", check_freq: bool = True
634638
) -> None:
635639
__tracebackhide__ = True
636640
_check_isinstance(left, right, TimedeltaArray)
@@ -685,7 +689,7 @@ def raise_assert_detail(
685689
def assert_numpy_array_equal(
686690
left,
687691
right,
688-
strict_nan=False,
692+
strict_nan: bool = False,
689693
check_dtype: bool | Literal["equiv"] = True,
690694
err_msg=None,
691695
check_same=None,
@@ -768,7 +772,7 @@ def assert_extension_array_equal(
768772
check_dtype: bool | Literal["equiv"] = True,
769773
index_values=None,
770774
check_less_precise=no_default,
771-
check_exact=False,
775+
check_exact: bool = False,
772776
rtol: float = 1.0e-5,
773777
atol: float = 1.0e-8,
774778
) -> None:
@@ -872,21 +876,21 @@ def assert_series_equal(
872876
right,
873877
check_dtype: bool | Literal["equiv"] = True,
874878
check_index_type: bool | Literal["equiv"] = "equiv",
875-
check_series_type=True,
879+
check_series_type: bool = True,
876880
check_less_precise: bool | int | NoDefault = no_default,
877-
check_names=True,
878-
check_exact=False,
879-
check_datetimelike_compat=False,
880-
check_categorical=True,
881-
check_category_order=True,
882-
check_freq=True,
883-
check_flags=True,
884-
rtol=1.0e-5,
885-
atol=1.0e-8,
881+
check_names: bool = True,
882+
check_exact: bool = False,
883+
check_datetimelike_compat: bool = False,
884+
check_categorical: bool = True,
885+
check_category_order: bool = True,
886+
check_freq: bool = True,
887+
check_flags: bool = True,
888+
rtol: float = 1.0e-5,
889+
atol: float = 1.0e-8,
886890
obj="Series",
887891
*,
888-
check_index=True,
889-
check_like=False,
892+
check_index: bool = True,
893+
check_like: bool = False,
890894
) -> None:
891895
"""
892896
Check that left and right Series are equal.
@@ -1140,19 +1144,19 @@ def assert_frame_equal(
11401144
right,
11411145
check_dtype: bool | Literal["equiv"] = True,
11421146
check_index_type: bool | Literal["equiv"] = "equiv",
1143-
check_column_type="equiv",
1144-
check_frame_type=True,
1147+
check_column_type: bool | Literal["equiv"] = "equiv",
1148+
check_frame_type: bool = True,
11451149
check_less_precise=no_default,
1146-
check_names=True,
1147-
by_blocks=False,
1148-
check_exact=False,
1149-
check_datetimelike_compat=False,
1150-
check_categorical=True,
1151-
check_like=False,
1152-
check_freq=True,
1153-
check_flags=True,
1154-
rtol=1.0e-5,
1155-
atol=1.0e-8,
1150+
check_names: bool = True,
1151+
by_blocks: bool = False,
1152+
check_exact: bool = False,
1153+
check_datetimelike_compat: bool = False,
1154+
check_categorical: bool = True,
1155+
check_like: bool = False,
1156+
check_freq: bool = True,
1157+
check_flags: bool = True,
1158+
rtol: float = 1.0e-5,
1159+
atol: float = 1.0e-8,
11561160
obj="DataFrame",
11571161
) -> None:
11581162
"""

pandas/conftest.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,7 @@ def frame_or_series(request):
449449
return request.param
450450

451451

452-
# error: List item 0 has incompatible type "Type[Index]"; expected "Type[IndexOpsMixin]"
453-
@pytest.fixture(
454-
params=[Index, Series], ids=["index", "series"] # type: ignore[list-item]
455-
)
452+
@pytest.fixture(params=[Index, Series], ids=["index", "series"])
456453
def index_or_series(request):
457454
"""
458455
Fixture to parametrize over Index and Series, made necessary by a mypy

pandas/core/arrays/arrow/array.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def __init__(self, values: pa.Array | pa.ChunkedArray) -> None:
219219
self._dtype = ArrowDtype(self._data.type)
220220

221221
@classmethod
222-
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
222+
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = False):
223223
"""
224224
Construct a new ExtensionArray from a sequence of scalars.
225225
"""
@@ -238,7 +238,7 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
238238

239239
@classmethod
240240
def _from_sequence_of_strings(
241-
cls, strings, *, dtype: Dtype | None = None, copy=False
241+
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
242242
):
243243
"""
244244
Construct a new ExtensionArray from a sequence of strings.

pandas/core/arrays/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class ExtensionArray:
247247
# ------------------------------------------------------------------------
248248

249249
@classmethod
250-
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
250+
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = False):
251251
"""
252252
Construct a new ExtensionArray from a sequence of scalars.
253253
@@ -270,7 +270,7 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
270270

271271
@classmethod
272272
def _from_sequence_of_strings(
273-
cls, strings, *, dtype: Dtype | None = None, copy=False
273+
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
274274
):
275275
"""
276276
Construct a new ExtensionArray from a sequence of strings.
@@ -1772,7 +1772,7 @@ class ExtensionScalarOpsMixin(ExtensionOpsMixin):
17721772
"""
17731773

17741774
@classmethod
1775-
def _create_method(cls, op, coerce_to_dtype=True, result_dtype=None):
1775+
def _create_method(cls, op, coerce_to_dtype: bool = True, result_dtype=None):
17761776
"""
17771777
A class method that returns a method that will correspond to an
17781778
operator for an ExtensionArray subclass, by dispatching to the

pandas/core/arrays/categorical.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ def _constructor(self) -> type[Categorical]:
493493
return Categorical
494494

495495
@classmethod
496-
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
496+
def _from_sequence(
497+
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False
498+
) -> Categorical:
497499
return Categorical(scalars, dtype=dtype, copy=copy)
498500

499501
@overload
@@ -783,7 +785,7 @@ def codes(self) -> np.ndarray:
783785
v.flags.writeable = False
784786
return v
785787

786-
def _set_categories(self, categories, fastpath=False):
788+
def _set_categories(self, categories, fastpath: bool = False) -> None:
787789
"""
788790
Sets new categories inplace
789791
@@ -951,7 +953,7 @@ def as_unordered(
951953
return self.set_ordered(False, inplace=inplace)
952954

953955
def set_categories(
954-
self, new_categories, ordered=None, rename=False, inplace=no_default
956+
self, new_categories, ordered=None, rename: bool = False, inplace=no_default
955957
):
956958
"""
957959
Set the categories to the specified new_categories.
@@ -1821,8 +1823,11 @@ def check_for_ordered(self, op) -> None:
18211823
"Categorical to an ordered one\n"
18221824
)
18231825

1826+
# error: Signature of "argsort" incompatible with supertype "ExtensionArray"
18241827
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
1825-
def argsort(self, ascending=True, kind="quicksort", **kwargs):
1828+
def argsort( # type: ignore[override]
1829+
self, ascending: bool = True, kind="quicksort", **kwargs
1830+
):
18261831
"""
18271832
Return the indices that would sort the Categorical.
18281833
@@ -2291,7 +2296,7 @@ def _reverse_indexer(self) -> dict[Hashable, npt.NDArray[np.intp]]:
22912296
# Reductions
22922297

22932298
@deprecate_kwarg(old_arg_name="numeric_only", new_arg_name="skipna")
2294-
def min(self, *, skipna=True, **kwargs):
2299+
def min(self, *, skipna: bool = True, **kwargs):
22952300
"""
22962301
The minimum value of the object.
22972302
@@ -2328,7 +2333,7 @@ def min(self, *, skipna=True, **kwargs):
23282333
return self._wrap_reduction_result(None, pointer)
23292334

23302335
@deprecate_kwarg(old_arg_name="numeric_only", new_arg_name="skipna")
2331-
def max(self, *, skipna=True, **kwargs):
2336+
def max(self, *, skipna: bool = True, **kwargs):
23322337
"""
23332338
The maximum value of the object.
23342339

pandas/core/arrays/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
184184
def _can_hold_na(self) -> bool:
185185
return True
186186

187-
def __init__(self, data, dtype: Dtype | None = None, freq=None, copy=False) -> None:
187+
def __init__(
188+
self, data, dtype: Dtype | None = None, freq=None, copy: bool = False
189+
) -> None:
188190
raise AbstractMethodError(self)
189191

190192
@property

pandas/core/arrays/datetimes.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,21 @@ def _from_sequence_not_strict(
333333

334334
return result
335335

336+
# error: Signature of "_generate_range" incompatible with supertype
337+
# "DatetimeLikeArrayMixin"
336338
@classmethod
337-
def _generate_range(
339+
def _generate_range( # type: ignore[override]
338340
cls,
339341
start,
340342
end,
341343
periods,
342344
freq,
343345
tz=None,
344-
normalize=False,
346+
normalize: bool = False,
345347
ambiguous="raise",
346348
nonexistent="raise",
347349
inclusive="both",
348-
):
350+
) -> DatetimeArray:
349351

350352
periods = dtl.validate_periods(periods)
351353
if freq is None and any(x is None for x in [periods, start, end]):
@@ -2133,7 +2135,7 @@ def objects_to_datetime64ns(
21332135
data: np.ndarray,
21342136
dayfirst,
21352137
yearfirst,
2136-
utc=False,
2138+
utc: bool = False,
21372139
errors="raise",
21382140
require_iso8601: bool = False,
21392141
allow_object: bool = False,

pandas/core/arrays/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ def __arrow_array__(self, type=None):
15171517
@Appender(
15181518
_interval_shared_docs["to_tuples"] % {"return_type": "ndarray", "examples": ""}
15191519
)
1520-
def to_tuples(self, na_tuple=True) -> np.ndarray:
1520+
def to_tuples(self, na_tuple: bool = True) -> np.ndarray:
15211521
tuples = com.asarray_tuplesafe(zip(self._left, self._right))
15221522
if not na_tuple:
15231523
# GH 18756

0 commit comments

Comments
 (0)