Skip to content

Commit 6787245

Browse files
authored
TYP: Update mypy and pyright (pandas-dev#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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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:

0 commit comments

Comments
 (0)