Skip to content

Commit 6de3717

Browse files
authored
TYP: misc annotations (#57606)
* TYP: misc changes * fix runtime * pre-commit * a few more * stubtest * NoReturn for <3.11
1 parent 4153f87 commit 6de3717

File tree

11 files changed

+137
-94
lines changed

11 files changed

+137
-94
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ repos:
127127
types: [python]
128128
stages: [manual]
129129
additional_dependencies: &pyright_dependencies
130-
130+
131131
- id: pyright
132132
# note: assumes python env is setup and activated
133133
name: pyright reportGeneralTypeIssues

pandas/_libs/tslibs/nattype.pyi

+40-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
from datetime import (
2+
date as date_,
23
datetime,
4+
time as time_,
35
timedelta,
46
tzinfo as _tzinfo,
57
)
6-
import typing
8+
from typing import (
9+
Literal,
10+
NoReturn,
11+
TypeAlias,
12+
)
713

814
import numpy as np
915

1016
from pandas._libs.tslibs.period import Period
11-
from pandas._typing import Self
17+
from pandas._typing import (
18+
Frequency,
19+
Self,
20+
TimestampNonexistent,
21+
)
1222

1323
NaT: NaTType
1424
iNaT: int
1525
nat_strings: set[str]
1626

17-
_NaTComparisonTypes: typing.TypeAlias = (
27+
_NaTComparisonTypes: TypeAlias = (
1828
datetime | timedelta | Period | np.datetime64 | np.timedelta64
1929
)
2030

@@ -61,18 +71,38 @@ class NaTType:
6171
def week(self) -> float: ...
6272
@property
6373
def weekofyear(self) -> float: ...
74+
@property
75+
def fold(self) -> int: ...
6476
def day_name(self) -> float: ...
6577
def month_name(self) -> float: ...
6678
def weekday(self) -> float: ...
6779
def isoweekday(self) -> float: ...
80+
def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...
81+
def strftime(self, format: str) -> NoReturn: ...
6882
def total_seconds(self) -> float: ...
6983
def today(self, *args, **kwargs) -> NaTType: ...
7084
def now(self, *args, **kwargs) -> NaTType: ...
7185
def to_pydatetime(self) -> NaTType: ...
7286
def date(self) -> NaTType: ...
73-
def round(self) -> NaTType: ...
74-
def floor(self) -> NaTType: ...
75-
def ceil(self) -> NaTType: ...
87+
def round(
88+
self,
89+
freq: Frequency,
90+
ambiguous: bool | Literal["raise"] | NaTType = ...,
91+
nonexistent: TimestampNonexistent = ...,
92+
) -> NaTType: ...
93+
def floor(
94+
self,
95+
freq: Frequency,
96+
ambiguous: bool | Literal["raise"] | NaTType = ...,
97+
nonexistent: TimestampNonexistent = ...,
98+
) -> NaTType: ...
99+
def ceil(
100+
self,
101+
freq: Frequency,
102+
ambiguous: bool | Literal["raise"] | NaTType = ...,
103+
nonexistent: TimestampNonexistent = ...,
104+
) -> NaTType: ...
105+
def combine(cls, date: date_, time: time_) -> NoReturn: ...
76106
@property
77107
def tzinfo(self) -> None: ...
78108
@property
@@ -81,8 +111,8 @@ class NaTType:
81111
def tz_localize(
82112
self,
83113
tz: _tzinfo | str | None,
84-
ambiguous: str = ...,
85-
nonexistent: str = ...,
114+
ambiguous: bool | Literal["raise"] | NaTType = ...,
115+
nonexistent: TimestampNonexistent = ...,
86116
) -> NaTType: ...
87117
def replace(
88118
self,
@@ -121,6 +151,8 @@ class NaTType:
121151
@property
122152
def days(self) -> float: ...
123153
@property
154+
def seconds(self) -> float: ...
155+
@property
124156
def microseconds(self) -> float: ...
125157
@property
126158
def nanoseconds(self) -> float: ...

pandas/_libs/tslibs/offsets.pyi

+4-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ class WeekOfMonth(WeekOfMonthMixin):
196196
self, n: int = ..., normalize: bool = ..., week: int = ..., weekday: int = ...
197197
) -> None: ...
198198

199-
class LastWeekOfMonth(WeekOfMonthMixin): ...
199+
class LastWeekOfMonth(WeekOfMonthMixin):
200+
def __init__(
201+
self, n: int = ..., normalize: bool = ..., weekday: int = ...
202+
) -> None: ...
200203

201204
class FY5253Mixin(SingleConstructorOffset):
202205
def __init__(

pandas/_typing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def __reversed__(self) -> Iterator[_T_co]:
207207
IndexLabel = Union[Hashable, Sequence[Hashable]]
208208
Level = Hashable
209209
Shape = tuple[int, ...]
210-
Suffixes = tuple[Optional[str], Optional[str]]
210+
Suffixes = Sequence[Optional[str]]
211211
Ordered = Optional[bool]
212212
JSONSerializable = Optional[Union[PythonScalar, list, dict]]
213213
Frequency = Union[str, "BaseOffset"]
@@ -226,7 +226,7 @@ def __reversed__(self) -> Iterator[_T_co]:
226226
Dtype = Union["ExtensionDtype", NpDtype]
227227
AstypeArg = Union["ExtensionDtype", "npt.DTypeLike"]
228228
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
229-
DtypeArg = Union[Dtype, dict[Hashable, Dtype]]
229+
DtypeArg = Union[Dtype, Mapping[Hashable, Dtype]]
230230
DtypeObj = Union[np.dtype, "ExtensionDtype"]
231231

232232
# converters

pandas/core/dtypes/missing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
if TYPE_CHECKING:
4646
from re import Pattern
4747

48+
from pandas._libs.missing import NAType
49+
from pandas._libs.tslibs import NaTType
4850
from pandas._typing import (
4951
ArrayLike,
5052
DtypeObj,
@@ -66,7 +68,7 @@
6668

6769

6870
@overload
69-
def isna(obj: Scalar | Pattern) -> bool:
71+
def isna(obj: Scalar | Pattern | NAType | NaTType) -> bool:
7072
...
7173

7274

@@ -283,7 +285,7 @@ def _isna_recarray_dtype(values: np.rec.recarray) -> npt.NDArray[np.bool_]:
283285

284286

285287
@overload
286-
def notna(obj: Scalar) -> bool:
288+
def notna(obj: Scalar | Pattern | NAType | NaTType) -> bool:
287289
...
288290

289291

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4799,7 +4799,7 @@ def insert(
47994799
self,
48004800
loc: int,
48014801
column: Hashable,
4802-
value: Scalar | AnyArrayLike,
4802+
value: object,
48034803
allow_duplicates: bool | lib.NoDefault = lib.no_default,
48044804
) -> None:
48054805
"""
@@ -6266,7 +6266,7 @@ def dropna(
62666266
axis: Axis = 0,
62676267
how: AnyAll | lib.NoDefault = lib.no_default,
62686268
thresh: int | lib.NoDefault = lib.no_default,
6269-
subset: IndexLabel | None = None,
6269+
subset: IndexLabel | AnyArrayLike | None = None,
62706270
inplace: bool = False,
62716271
ignore_index: bool = False,
62726272
) -> DataFrame | None:
@@ -6390,7 +6390,7 @@ def dropna(
63906390
if subset is not None:
63916391
# subset needs to be list
63926392
if not is_list_like(subset):
6393-
subset = [subset]
6393+
subset = [cast(Hashable, subset)]
63946394
ax = self._get_axis(agg_axis)
63956395
indices = ax.get_indexer_for(subset)
63966396
check = indices == -1

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ def hist(
13261326
xrot: float | None = None,
13271327
ylabelsize: int | None = None,
13281328
yrot: float | None = None,
1329-
figsize: tuple[int, int] | None = None,
1329+
figsize: tuple[float, float] | None = None,
13301330
bins: int | Sequence[int] = 10,
13311331
backend: str | None = None,
13321332
legend: bool = False,
@@ -2599,7 +2599,7 @@ def hist(
25992599
ax=None,
26002600
sharex: bool = False,
26012601
sharey: bool = False,
2602-
figsize: tuple[int, int] | None = None,
2602+
figsize: tuple[float, float] | None = None,
26032603
layout: tuple[int, int] | None = None,
26042604
bins: int | Sequence[int] = 10,
26052605
backend: str | None = None,

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def merge_asof(
445445
left_by=None,
446446
right_by=None,
447447
suffixes: Suffixes = ("_x", "_y"),
448-
tolerance: int | Timedelta | None = None,
448+
tolerance: int | datetime.timedelta | None = None,
449449
allow_exact_matches: bool = True,
450450
direction: str = "backward",
451451
) -> DataFrame:
@@ -494,7 +494,7 @@ def merge_asof(
494494
suffixes : 2-length sequence (tuple, list, ...)
495495
Suffix to apply to overlapping column names in the left and right
496496
side, respectively.
497-
tolerance : int or Timedelta, optional, default None
497+
tolerance : int or timedelta, optional, default None
498498
Select asof tolerance within this range; must be compatible
499499
with the merge index.
500500
allow_exact_matches : bool, default True

pandas/core/series.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,7 @@ def autocorr(self, lag: int = 1) -> float:
28912891
"""
28922892
return self.corr(cast(Series, self.shift(lag)))
28932893

2894-
def dot(self, other: AnyArrayLike) -> Series | np.ndarray:
2894+
def dot(self, other: AnyArrayLike | DataFrame) -> Series | np.ndarray:
28952895
"""
28962896
Compute the dot product between the Series and the columns of other.
28972897
@@ -6346,7 +6346,7 @@ def mean(
63466346
skipna: bool = True,
63476347
numeric_only: bool = False,
63486348
**kwargs,
6349-
):
6349+
) -> Any:
63506350
return NDFrame.mean(
63516351
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
63526352
)
@@ -6358,7 +6358,7 @@ def median(
63586358
skipna: bool = True,
63596359
numeric_only: bool = False,
63606360
**kwargs,
6361-
):
6361+
) -> Any:
63626362
return NDFrame.median(
63636363
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
63646364
)

pandas/io/excel/_base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
DtypeBackend,
7777
ExcelWriterIfSheetExists,
7878
FilePath,
79+
HashableT,
7980
IntStrT,
8081
ReadBuffer,
8182
Self,
@@ -382,7 +383,7 @@ def read_excel(
382383
| str
383384
| Sequence[int]
384385
| Sequence[str]
385-
| Callable[[str], bool]
386+
| Callable[[HashableT], bool]
386387
| None = ...,
387388
dtype: DtypeArg | None = ...,
388389
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = ...,
@@ -421,7 +422,7 @@ def read_excel(
421422
| str
422423
| Sequence[int]
423424
| Sequence[str]
424-
| Callable[[str], bool]
425+
| Callable[[HashableT], bool]
425426
| None = ...,
426427
dtype: DtypeArg | None = ...,
427428
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = ...,
@@ -460,7 +461,7 @@ def read_excel(
460461
| str
461462
| Sequence[int]
462463
| Sequence[str]
463-
| Callable[[str], bool]
464+
| Callable[[HashableT], bool]
464465
| None = None,
465466
dtype: DtypeArg | None = None,
466467
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = None,

0 commit comments

Comments
 (0)