Skip to content

Commit 6787245

Browse files
authored
TYP: Update mypy and pyright (#56493)
* changes for mypy * changes for pyright * 1.1.400 would require too many difficult changes
1 parent 77db53d commit 6787245

36 files changed

+213
-99
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ repos:
132132
types: [python]
133133
stages: [manual]
134134
additional_dependencies: &pyright_dependencies
135-
135+
136136
- id: pyright
137137
# note: assumes python env is setup and activated
138138
name: pyright reportGeneralTypeIssues
139-
entry: pyright --skipunannotated -p pyright_reportGeneralTypeIssues.json --level warning
139+
entry: pyright -p pyright_reportGeneralTypeIssues.json --level warning
140140
language: node
141141
pass_filenames: false
142142
types: [python]

doc/source/whatsnew/v2.2.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ Optional libraries below the lowest tested version may still work, but are not c
346346
+-----------------+-----------------+---------+
347347
| Package | Minimum Version | Changed |
348348
+=================+=================+=========+
349+
| mypy (dev) | 1.7.1 | X |
350+
+-----------------+-----------------+---------+
349351
| | | X |
350352
+-----------------+-----------------+---------+
351353

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ dependencies:
7676

7777
# code checks
7878
- flake8=6.1.0 # run in subprocess over docstring examples
79-
- mypy=1.4.1 # pre-commit uses locally installed mypy
79+
- mypy=1.7.1 # pre-commit uses locally installed mypy
8080
- tokenize-rt # scripts/check_for_inconsistent_pandas_namespace.py
8181
- pre-commit>=3.6.0
8282

pandas/_config/config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ def get_default_val(pat: str):
220220
class DictWrapper:
221221
"""provide attribute-style access to a nested dict"""
222222

223+
d: dict[str, Any]
224+
223225
def __init__(self, d: dict[str, Any], prefix: str = "") -> None:
224226
object.__setattr__(self, "d", d)
225227
object.__setattr__(self, "prefix", prefix)
@@ -250,7 +252,7 @@ def __getattr__(self, key: str):
250252
else:
251253
return _get_option(prefix)
252254

253-
def __dir__(self) -> Iterable[str]:
255+
def __dir__(self) -> list[str]:
254256
return list(self.d.keys())
255257

256258

pandas/_libs/tslibs/nattype.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import typing
88
import numpy as np
99

1010
from pandas._libs.tslibs.period import Period
11+
from pandas._typing import Self
1112

1213
NaT: NaTType
1314
iNaT: int
@@ -132,4 +133,9 @@ class NaTType:
132133
__le__: _NatComparison
133134
__gt__: _NatComparison
134135
__ge__: _NatComparison
136+
def __sub__(self, other: Self | timedelta | datetime) -> Self: ...
137+
def __rsub__(self, other: Self | timedelta | datetime) -> Self: ...
138+
def __add__(self, other: Self | timedelta | datetime) -> Self: ...
139+
def __radd__(self, other: Self | timedelta | datetime) -> Self: ...
140+
def __hash__(self) -> int: ...
135141
def as_unit(self, unit: str, round_ok: bool = ...) -> NaTType: ...

pandas/_libs/tslibs/timestamps.pyi

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ from typing import (
1010
ClassVar,
1111
Literal,
1212
TypeAlias,
13-
TypeVar,
1413
overload,
1514
)
1615

@@ -28,7 +27,6 @@ from pandas._typing import (
2827
TimestampNonexistent,
2928
)
3029

31-
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
3230
_TimeZones: TypeAlias = str | _tzinfo | None | int
3331

3432
def integer_op_not_supported(obj: object) -> TypeError: ...
@@ -42,7 +40,7 @@ class Timestamp(datetime):
4240
_value: int # np.int64
4341
# error: "__new__" must return a class instance (got "Union[Timestamp, NaTType]")
4442
def __new__( # type: ignore[misc]
45-
cls: type[_DatetimeT],
43+
cls: type[Self],
4644
ts_input: np.integer | float | str | _date | datetime | np.datetime64 = ...,
4745
year: int | None = ...,
4846
month: int | None = ...,
@@ -57,7 +55,7 @@ class Timestamp(datetime):
5755
tz: _TimeZones = ...,
5856
unit: str | int | None = ...,
5957
fold: int | None = ...,
60-
) -> _DatetimeT | NaTType: ...
58+
) -> Self | NaTType: ...
6159
@classmethod
6260
def _from_value_and_reso(
6361
cls, value: int, reso: int, tz: _TimeZones

pandas/_testing/_hypothesis.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@
5454
DATETIME_NO_TZ = st.datetimes()
5555

5656
DATETIME_JAN_1_1900_OPTIONAL_TZ = st.datetimes(
57-
min_value=pd.Timestamp(1900, 1, 1).to_pydatetime(),
58-
max_value=pd.Timestamp(1900, 1, 1).to_pydatetime(),
57+
min_value=pd.Timestamp(
58+
1900, 1, 1
59+
).to_pydatetime(), # pyright: ignore[reportGeneralTypeIssues]
60+
max_value=pd.Timestamp(
61+
1900, 1, 1
62+
).to_pydatetime(), # pyright: ignore[reportGeneralTypeIssues]
5963
timezones=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()),
6064
)
6165

pandas/_typing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def __reversed__(self) -> Iterator[_T_co]:
234234

235235
# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
236236
AggFuncTypeBase = Union[Callable, str]
237-
AggFuncTypeDict = dict[Hashable, Union[AggFuncTypeBase, list[AggFuncTypeBase]]]
237+
AggFuncTypeDict = MutableMapping[
238+
Hashable, Union[AggFuncTypeBase, list[AggFuncTypeBase]]
239+
]
238240
AggFuncType = Union[
239241
AggFuncTypeBase,
240242
list[AggFuncTypeBase],

pandas/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ def fixed_now_ts() -> Timestamp:
13531353
"""
13541354
Fixture emits fixed Timestamp.now()
13551355
"""
1356-
return Timestamp(
1356+
return Timestamp( # pyright: ignore[reportGeneralTypeIssues]
13571357
year=2021, month=1, day=1, hour=12, minute=4, second=13, microsecond=22
13581358
)
13591359

pandas/core/_numba/kernels/var_.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def sliding_var(
116116
ssqdm_x,
117117
compensation_add,
118118
num_consecutive_same_value,
119-
prev_value, # pyright: ignore[reportGeneralTypeIssues]
119+
prev_value,
120120
)
121121
else:
122122
for j in range(start[i - 1], s):
@@ -141,7 +141,7 @@ def sliding_var(
141141
ssqdm_x,
142142
compensation_add,
143143
num_consecutive_same_value,
144-
prev_value, # pyright: ignore[reportGeneralTypeIssues]
144+
prev_value,
145145
)
146146

147147
if nobs >= min_periods and nobs > ddof:

pandas/core/apply.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
TYPE_CHECKING,
1010
Any,
1111
Callable,
12-
DefaultDict,
1312
Literal,
1413
cast,
1514
)
@@ -63,6 +62,7 @@
6362
Generator,
6463
Hashable,
6564
Iterable,
65+
MutableMapping,
6666
Sequence,
6767
)
6868

@@ -1642,7 +1642,7 @@ def transform(self):
16421642

16431643
def reconstruct_func(
16441644
func: AggFuncType | None, **kwargs
1645-
) -> tuple[bool, AggFuncType, list[str] | None, npt.NDArray[np.intp] | None]:
1645+
) -> tuple[bool, AggFuncType, tuple[str, ...] | None, npt.NDArray[np.intp] | None]:
16461646
"""
16471647
This is the internal function to reconstruct func given if there is relabeling
16481648
or not and also normalize the keyword to get new order of columns.
@@ -1668,7 +1668,7 @@ def reconstruct_func(
16681668
-------
16691669
relabelling: bool, if there is relabelling or not
16701670
func: normalized and mangled func
1671-
columns: list of column names
1671+
columns: tuple of column names
16721672
order: array of columns indices
16731673
16741674
Examples
@@ -1680,7 +1680,7 @@ def reconstruct_func(
16801680
(False, 'min', None, None)
16811681
"""
16821682
relabeling = func is None and is_multi_agg_with_relabel(**kwargs)
1683-
columns: list[str] | None = None
1683+
columns: tuple[str, ...] | None = None
16841684
order: npt.NDArray[np.intp] | None = None
16851685

16861686
if not relabeling:
@@ -1696,7 +1696,14 @@ def reconstruct_func(
16961696
raise TypeError("Must provide 'func' or tuples of '(column, aggfunc).")
16971697

16981698
if relabeling:
1699-
func, columns, order = normalize_keyword_aggregation(kwargs)
1699+
# error: Incompatible types in assignment (expression has type
1700+
# "MutableMapping[Hashable, list[Callable[..., Any] | str]]", variable has type
1701+
# "Callable[..., Any] | str | list[Callable[..., Any] | str] |
1702+
# MutableMapping[Hashable, Callable[..., Any] | str | list[Callable[..., Any] |
1703+
# str]] | None")
1704+
func, columns, order = normalize_keyword_aggregation( # type: ignore[assignment]
1705+
kwargs
1706+
)
17001707
assert func is not None
17011708

17021709
return relabeling, func, columns, order
@@ -1730,7 +1737,11 @@ def is_multi_agg_with_relabel(**kwargs) -> bool:
17301737

17311738
def normalize_keyword_aggregation(
17321739
kwargs: dict,
1733-
) -> tuple[dict, list[str], npt.NDArray[np.intp]]:
1740+
) -> tuple[
1741+
MutableMapping[Hashable, list[AggFuncTypeBase]],
1742+
tuple[str, ...],
1743+
npt.NDArray[np.intp],
1744+
]:
17341745
"""
17351746
Normalize user-provided "named aggregation" kwargs.
17361747
Transforms from the new ``Mapping[str, NamedAgg]`` style kwargs
@@ -1744,7 +1755,7 @@ def normalize_keyword_aggregation(
17441755
-------
17451756
aggspec : dict
17461757
The transformed kwargs.
1747-
columns : List[str]
1758+
columns : tuple[str, ...]
17481759
The user-provided keys.
17491760
col_idx_order : List[int]
17501761
List of columns indices.
@@ -1759,9 +1770,7 @@ def normalize_keyword_aggregation(
17591770
# Normalize the aggregation functions as Mapping[column, List[func]],
17601771
# process normally, then fixup the names.
17611772
# TODO: aggspec type: typing.Dict[str, List[AggScalar]]
1762-
# May be hitting https://github.com/python/mypy/issues/5958
1763-
# saying it doesn't have an attribute __name__
1764-
aggspec: DefaultDict = defaultdict(list)
1773+
aggspec = defaultdict(list)
17651774
order = []
17661775
columns, pairs = list(zip(*kwargs.items()))
17671776

pandas/core/arrays/_ranges.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ def generate_regular_range(
5757
b: int | np.int64 | np.uint64
5858
e: int | np.int64 | np.uint64
5959
try:
60-
td = td.as_unit( # pyright: ignore[reportGeneralTypeIssues]
61-
unit, round_ok=False
62-
)
60+
td = td.as_unit(unit, round_ok=False)
6361
except ValueError as err:
6462
raise ValueError(
6563
f"freq={freq} is incompatible with unit={unit}. "

pandas/core/arrays/categorical.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2894,9 +2894,7 @@ def _validate(data):
28942894
if not isinstance(data.dtype, CategoricalDtype):
28952895
raise AttributeError("Can only use .cat accessor with a 'category' dtype")
28962896

2897-
# error: Signature of "_delegate_property_get" incompatible with supertype
2898-
# "PandasDelegate"
2899-
def _delegate_property_get(self, name: str): # type: ignore[override]
2897+
def _delegate_property_get(self, name: str):
29002898
return getattr(self._parent, name)
29012899

29022900
# error: Signature of "_delegate_property_set" incompatible with supertype

pandas/core/arrays/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ def _validate_scalar(
600600
raise TypeError(msg)
601601

602602
elif isinstance(value, self._recognized_scalars):
603-
value = self._scalar_type(value)
603+
# error: Argument 1 to "Timestamp" has incompatible type "object"; expected
604+
# "integer[Any] | float | str | date | datetime | datetime64"
605+
value = self._scalar_type(value) # type: ignore[arg-type]
604606

605607
else:
606608
msg = self._validation_error_message(value, allow_listlike)

pandas/core/arrays/datetimes.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,8 @@ def _from_sequence_not_strict(
397397
result._maybe_pin_freq(freq, validate_kwds)
398398
return result
399399

400-
# error: Signature of "_generate_range" incompatible with supertype
401-
# "DatetimeLikeArrayMixin"
402400
@classmethod
403-
def _generate_range( # type: ignore[override]
401+
def _generate_range(
404402
cls,
405403
start,
406404
end,

pandas/core/arrays/timedeltas.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,8 @@ def _from_sequence_not_strict(
267267
result._maybe_pin_freq(freq, {})
268268
return result
269269

270-
# Signature of "_generate_range" incompatible with supertype
271-
# "DatetimeLikeArrayMixin"
272270
@classmethod
273-
def _generate_range( # type: ignore[override]
271+
def _generate_range(
274272
cls, start, end, periods, freq, closed=None, *, unit: str | None = None
275273
) -> Self:
276274
periods = dtl.validate_periods(periods)

pandas/core/base.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1310,12 +1310,10 @@ def factorize(
13101310
# This overload is needed so that the call to searchsorted in
13111311
# pandas.core.resample.TimeGrouper._get_period_bins picks the correct result
13121312

1313-
@overload
1314-
# The following ignore is also present in numpy/__init__.pyi
1315-
# Possibly a mypy bug??
13161313
# error: Overloaded function signatures 1 and 2 overlap with incompatible
1317-
# return types [misc]
1318-
def searchsorted( # type: ignore[misc]
1314+
# return types
1315+
@overload
1316+
def searchsorted( # type: ignore[overload-overlap]
13191317
self,
13201318
value: ScalarLike_co,
13211319
side: Literal["left", "right"] = ...,

pandas/core/frame.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -6896,14 +6896,7 @@ def f(vals) -> tuple[np.ndarray, int]:
68966896
vals = (col.values for name, col in self.items() if name in subset)
68976897
labels, shape = map(list, zip(*map(f, vals)))
68986898

6899-
ids = get_group_index(
6900-
labels,
6901-
# error: Argument 1 to "tuple" has incompatible type "List[_T]";
6902-
# expected "Iterable[int]"
6903-
tuple(shape), # type: ignore[arg-type]
6904-
sort=False,
6905-
xnull=False,
6906-
)
6899+
ids = get_group_index(labels, tuple(shape), sort=False, xnull=False)
69076900
result = self._constructor_sliced(duplicated(ids, keep), index=self.index)
69086901
return result.__finalize__(self, method="duplicated")
69096902

pandas/core/generic.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -8005,7 +8005,9 @@ def replace(
80058005
if items:
80068006
keys, values = zip(*items)
80078007
else:
8008-
keys, values = ([], [])
8008+
# error: Incompatible types in assignment (expression has type
8009+
# "list[Never]", variable has type "tuple[Any, ...]")
8010+
keys, values = ([], []) # type: ignore[assignment]
80098011

80108012
are_mappings = [is_dict_like(v) for v in values]
80118013

@@ -8020,7 +8022,12 @@ def replace(
80208022
value_dict = {}
80218023

80228024
for k, v in items:
8023-
keys, values = list(zip(*v.items())) or ([], [])
8025+
# error: Incompatible types in assignment (expression has type
8026+
# "list[Never]", variable has type "tuple[Any, ...]")
8027+
keys, values = list(zip(*v.items())) or ( # type: ignore[assignment]
8028+
[],
8029+
[],
8030+
)
80248031

80258032
to_rep_dict[k] = list(keys)
80268033
value_dict[k] = list(values)

pandas/core/indexes/accessors.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def _get_values(self):
8585
f"cannot convert an object of type {type(data)} to a datetimelike index"
8686
)
8787

88-
# error: Signature of "_delegate_property_get" incompatible with supertype
89-
# "PandasDelegate"
90-
def _delegate_property_get(self, name: str): # type: ignore[override]
88+
def _delegate_property_get(self, name: str):
9189
from pandas import Series
9290

9391
values = self._get_values()
@@ -175,7 +173,7 @@ def __init__(self, data: Series, orig) -> None:
175173
self._orig = orig
176174
self._freeze()
177175

178-
def _delegate_property_get(self, name: str): # type: ignore[override]
176+
def _delegate_property_get(self, name: str):
179177
if not hasattr(self._parent.array, f"_dt_{name}"):
180178
raise NotImplementedError(
181179
f"dt.{name} is not supported for {self._parent.dtype}"

0 commit comments

Comments
 (0)