Skip to content

Commit 80753e2

Browse files
authored
TYP: enable reportGeneralTypeIssues for more files (#54423)
* TYP: enable reportGeneralTypeIssues for more files * fixes * Added some time-related annotations based on the tests from pandas-stubs * remove python version from second pyright config file
1 parent 78c79b9 commit 80753e2

File tree

22 files changed

+127
-84
lines changed

22 files changed

+127
-84
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ repos:
138138
types: [python]
139139
stages: [manual]
140140
additional_dependencies: &pyright_dependencies
141-
- pyright@1.1.305
141+
- pyright@1.1.318
142142
- id: pyright
143143
# note: assumes python env is setup and activated
144144
name: pyright reportGeneralTypeIssues

pandas/_libs/tslibs/offsets.pyi

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import (
22
datetime,
3+
time,
34
timedelta,
45
)
56
from typing import (
@@ -14,6 +15,7 @@ import numpy as np
1415

1516
from pandas._libs.tslibs.nattype import NaTType
1617
from pandas._typing import (
18+
OffsetCalendar,
1719
Self,
1820
npt,
1921
)
@@ -141,8 +143,8 @@ class BusinessHour(BusinessMixin):
141143
self,
142144
n: int = ...,
143145
normalize: bool = ...,
144-
start: str | Collection[str] = ...,
145-
end: str | Collection[str] = ...,
146+
start: str | time | Collection[str | time] = ...,
147+
end: str | time | Collection[str | time] = ...,
146148
offset: timedelta = ...,
147149
) -> None: ...
148150

@@ -228,7 +230,7 @@ class _CustomBusinessMonth(BusinessMixin):
228230
normalize: bool = ...,
229231
weekmask: str = ...,
230232
holidays: list | None = ...,
231-
calendar: np.busdaycalendar | None = ...,
233+
calendar: OffsetCalendar | None = ...,
232234
offset: timedelta = ...,
233235
) -> None: ...
234236

@@ -239,7 +241,7 @@ class CustomBusinessDay(BusinessDay):
239241
normalize: bool = ...,
240242
weekmask: str = ...,
241243
holidays: list | None = ...,
242-
calendar: np.busdaycalendar | None = ...,
244+
calendar: OffsetCalendar | None = ...,
243245
offset: timedelta = ...,
244246
) -> None: ...
245247

@@ -250,9 +252,9 @@ class CustomBusinessHour(BusinessHour):
250252
normalize: bool = ...,
251253
weekmask: str = ...,
252254
holidays: list | None = ...,
253-
calendar: np.busdaycalendar | None = ...,
254-
start: str = ...,
255-
end: str = ...,
255+
calendar: OffsetCalendar | None = ...,
256+
start: str | time | Collection[str | time] = ...,
257+
end: str | time | Collection[str | time] = ...,
256258
offset: timedelta = ...,
257259
) -> None: ...
258260

pandas/_libs/tslibs/timestamps.pyi

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ from pandas._libs.tslibs import (
2121
Tick,
2222
Timedelta,
2323
)
24-
from pandas._typing import Self
24+
from pandas._typing import (
25+
Self,
26+
TimestampNonexistent,
27+
)
2528

2629
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
2730

@@ -187,18 +190,27 @@ class Timestamp(datetime):
187190
self,
188191
tz: _tzinfo | str | None,
189192
ambiguous: str = ...,
190-
nonexistent: str = ...,
193+
nonexistent: TimestampNonexistent = ...,
191194
) -> Self: ...
192195
def normalize(self) -> Self: ...
193196
# TODO: round/floor/ceil could return NaT?
194197
def round(
195-
self, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
198+
self,
199+
freq: str,
200+
ambiguous: bool | str = ...,
201+
nonexistent: TimestampNonexistent = ...,
196202
) -> Self: ...
197203
def floor(
198-
self, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
204+
self,
205+
freq: str,
206+
ambiguous: bool | str = ...,
207+
nonexistent: TimestampNonexistent = ...,
199208
) -> Self: ...
200209
def ceil(
201-
self, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
210+
self,
211+
freq: str,
212+
ambiguous: bool | str = ...,
213+
nonexistent: TimestampNonexistent = ...,
202214
) -> Self: ...
203215
def day_name(self, locale: str | None = ...) -> str: ...
204216
def month_name(self, locale: str | None = ...) -> str: ...

pandas/_typing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Sequence,
88
)
99
from datetime import (
10+
date,
1011
datetime,
1112
timedelta,
1213
tzinfo,
@@ -68,6 +69,7 @@
6869
from pandas.core.window.rolling import BaseWindow
6970

7071
from pandas.io.formats.format import EngFormatter
72+
from pandas.tseries.holiday import AbstractHolidayCalendar
7173

7274
ScalarLike_co = Union[
7375
int,
@@ -89,7 +91,7 @@
8991
from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport]
9092

9193
if sys.version_info >= (3, 11):
92-
from typing import Self
94+
from typing import Self # pyright: ignore[reportUnusedImport]
9395
else:
9496
from typing_extensions import Self # pyright: ignore[reportUnusedImport]
9597
else:
@@ -117,14 +119,17 @@
117119
PythonScalar = Union[str, float, bool]
118120
DatetimeLikeScalar = Union["Period", "Timestamp", "Timedelta"]
119121
PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]
120-
Scalar = Union[PythonScalar, PandasScalar, np.datetime64, np.timedelta64, datetime]
122+
Scalar = Union[PythonScalar, PandasScalar, np.datetime64, np.timedelta64, date]
121123
IntStrT = TypeVar("IntStrT", int, str)
122124

123125

124126
# timestamp and timedelta convertible types
125127

126128
TimestampConvertibleTypes = Union[
127-
"Timestamp", datetime, np.datetime64, np.int64, float, str
129+
"Timestamp", date, np.datetime64, np.int64, float, str
130+
]
131+
TimestampNonexistent = Union[
132+
Literal["shift_forward", "shift_backward", "NaT", "raise"], timedelta
128133
]
129134
TimedeltaConvertibleTypes = Union[
130135
"Timedelta", timedelta, np.timedelta64, np.int64, float, str
@@ -466,3 +471,6 @@ def closed(self) -> bool:
466471

467472
# ExcelWriter
468473
ExcelWriterIfSheetExists = Literal["error", "new", "replace", "overlay"]
474+
475+
# Offsets
476+
OffsetCalendar = Union[np.busdaycalendar, "AbstractHolidayCalendar"]

pandas/core/arrays/period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def raise_on_incompatible(left, right):
990990

991991
def period_array(
992992
data: Sequence[Period | str | None] | AnyArrayLike,
993-
freq: str | Tick | None = None,
993+
freq: str | Tick | BaseOffset | None = None,
994994
copy: bool = False,
995995
) -> PeriodArray:
996996
"""

pandas/core/indexes/datetimes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ def bdate_range(
10241024
start=None,
10251025
end=None,
10261026
periods: int | None = None,
1027-
freq: Frequency = "B",
1027+
freq: Frequency | dt.timedelta = "B",
10281028
tz=None,
10291029
normalize: bool = True,
10301030
name: Hashable | None = None,

pandas/core/reshape/concat.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,22 @@ def _clean_keys_and_objs(
497497
if isinstance(objs, abc.Mapping):
498498
if keys is None:
499499
keys = list(objs.keys())
500-
objs = [objs[k] for k in keys]
500+
objs_list = [objs[k] for k in keys]
501501
else:
502-
objs = list(objs)
502+
objs_list = list(objs)
503503

504-
if len(objs) == 0:
504+
if len(objs_list) == 0:
505505
raise ValueError("No objects to concatenate")
506506

507507
if keys is None:
508-
objs = list(com.not_none(*objs))
508+
objs_list = list(com.not_none(*objs_list))
509509
else:
510510
# GH#1649
511511
clean_keys = []
512512
clean_objs = []
513513
if is_iterator(keys):
514514
keys = list(keys)
515-
if is_iterator(objs):
516-
objs = list(objs)
517-
if len(keys) != len(objs):
515+
if len(keys) != len(objs_list):
518516
# GH#43485
519517
warnings.warn(
520518
"The behavior of pd.concat with len(keys) != len(objs) is "
@@ -523,12 +521,12 @@ def _clean_keys_and_objs(
523521
FutureWarning,
524522
stacklevel=find_stack_level(),
525523
)
526-
for k, v in zip(keys, objs):
524+
for k, v in zip(keys, objs_list):
527525
if v is None:
528526
continue
529527
clean_keys.append(k)
530528
clean_objs.append(v)
531-
objs = clean_objs
529+
objs_list = clean_objs
532530

533531
if isinstance(keys, MultiIndex):
534532
# TODO: retain levels?
@@ -537,10 +535,10 @@ def _clean_keys_and_objs(
537535
name = getattr(keys, "name", None)
538536
keys = Index(clean_keys, name=name, dtype=getattr(keys, "dtype", None))
539537

540-
if len(objs) == 0:
538+
if len(objs_list) == 0:
541539
raise ValueError("All objects passed were None")
542540

543-
return objs, keys
541+
return objs_list, keys
544542

545543
def _get_sample_object(
546544
self,

pandas/core/tools/datetimes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from collections import abc
4-
from datetime import datetime
4+
from datetime import date
55
from functools import partial
66
from itertools import islice
77
from typing import (
@@ -98,7 +98,7 @@
9898

9999
ArrayConvertible = Union[list, tuple, AnyArrayLike]
100100
Scalar = Union[float, str]
101-
DatetimeScalar = Union[Scalar, datetime]
101+
DatetimeScalar = Union[Scalar, date, np.datetime64]
102102

103103
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]
104104

pandas/core/tools/times.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
def to_time(
2424
arg,
25-
format=None,
25+
format: str | None = None,
2626
infer_time_format: bool = False,
2727
errors: DateTimeErrorChoices = "raise",
2828
):

pandas/io/common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,10 +1023,12 @@ def __init__(
10231023
self.archive_name = archive_name
10241024

10251025
kwargs.setdefault("compression", zipfile.ZIP_DEFLATED)
1026-
# error: Argument 1 to "ZipFile" has incompatible type "Union[
1027-
# Union[str, PathLike[str]], ReadBuffer[bytes], WriteBuffer[bytes]]";
1028-
# expected "Union[Union[str, PathLike[str]], IO[bytes]]"
1029-
self.buffer = zipfile.ZipFile(file, mode, **kwargs) # type: ignore[arg-type]
1026+
# error: No overload variant of "ZipFile" matches argument types "str |
1027+
# PathLike[str] | ReadBuffer[bytes] | WriteBuffer[bytes]", "str", "dict[str,
1028+
# Any]"
1029+
self.buffer = zipfile.ZipFile( # type: ignore[call-overload]
1030+
file, mode, **kwargs
1031+
)
10301032

10311033
def infer_filename(self) -> str | None:
10321034
"""

0 commit comments

Comments
 (0)