Skip to content

Commit b0e7014

Browse files
authored
BUG: relocate to_offset and other pyrefly-inspired changes (#1878)
* pyrefly * bump ruff
1 parent db718c4 commit b0e7014

15 files changed

Lines changed: 50 additions & 31 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ ci:
33
autofix_prs: false
44
repos:
55
- repo: https://github.com/astral-sh/ruff-pre-commit
6-
rev: v0.15.22
6+
rev: v0.16.1
77
hooks:
88
- id: ruff-check
99
args: [--exit-non-zero-on-fix]
1010
- repo: https://github.com/codespell-project/codespell
11-
rev: v2.4.2
11+
rev: v2.4.3
1212
hooks:
1313
- id: codespell
1414
args: [-L, "THIRDPARTY"]

pandas-stubs/_libs/tslibs/offsets.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import numpy as np
1818
from pandas import Timestamp
1919

2020
from pandas._typing import (
21+
Frequency,
2122
ShapeT,
2223
np_ndarray_object,
2324
)
@@ -104,6 +105,11 @@ class BaseOffset:
104105

105106
class SingleConstructorOffset(BaseOffset): ...
106107

108+
@overload
109+
def to_offset(freq: None, is_period: bool = False) -> None: ...
110+
@overload
111+
def to_offset(freq: Frequency | timedelta, is_period: bool = False) -> BaseOffset: ...
112+
107113
class Tick(SingleConstructorOffset):
108114
def __init__(self, n: int = ..., normalize: bool = ...) -> None: ...
109115
@property

pandas-stubs/_libs/tslibs/timestamps.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ _Ambiguous: TypeAlias = bool | Literal["raise", "NaT"]
4848

4949
# Repeated from `_typing.pyi` so as to satisfy mixed strict / non-strict paths.
5050
# https://github.com/pandas-dev/pandas-stubs/pull/1151#issuecomment-2715130190
51-
TimeZones: TypeAlias = str | _tzinfo | None | int
51+
TimeZones: TypeAlias = str | _tzinfo | int | None
5252

5353
class Timestamp(datetime, SupportsIndex):
5454
min: ClassVar[Timestamp] # pyright: ignore[reportIncompatibleVariableOverride]

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ StorageOptions: TypeAlias = dict[str, Any] | None
703703
# compression keywords and compression
704704
CompressionDict: TypeAlias = dict[str, Any]
705705
CompressionOptions: TypeAlias = (
706-
None | Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"] | CompressionDict
706+
Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"] | CompressionDict | None
707707
)
708708
ParquetCompressionOptions: TypeAlias = (
709709
Literal["snappy", "gzip", "brotli", "lz4", "zstd"] | None
@@ -1203,7 +1203,7 @@ ExcelWriteEngine: TypeAlias = Literal["openpyxl", "odf", "xlsxwriter"]
12031203

12041204
# Repeated in `timestamps.pyi` so as to satisfy mixed strict / non-strict paths.
12051205
# https://github.com/pandas-dev/pandas-stubs/pull/1151#issuecomment-2715130190
1206-
TimeZones: TypeAlias = str | tzinfo | None | int
1206+
TimeZones: TypeAlias = str | tzinfo | int | None
12071207

12081208
ColumnValue: TypeAlias = AnyArrayLike | Scalar | Sequence[Scalar] | range | None
12091209
# Evaluates to a DataFrame column in DataFrame.assign context.

pandas-stubs/core/construction.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def array( # type: ignore[overload-overlap]
136136
@overload
137137
def array(
138138
data: (
139-
Sequence[IntervalT | None | float]
139+
Sequence[IntervalT | float | None]
140140
| IntervalArray
141141
| IntervalIndex
142142
| Series[Interval]

pandas-stubs/core/indexes/timedeltas.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def timedelta_range(
160160
freq: Frequency | Timedelta | timedelta | None = None,
161161
name: Hashable | None = None,
162162
closed: Literal["left", "right"] | None = None,
163-
unit: None | str = None,
163+
unit: str | None = None,
164164
) -> TimedeltaIndex: ...
165165
@overload
166166
def timedelta_range(
@@ -170,7 +170,7 @@ def timedelta_range(
170170
freq: Frequency | Timedelta | timedelta | None = None,
171171
name: Hashable | None = None,
172172
closed: Literal["left", "right"] | None = None,
173-
unit: None | str = None,
173+
unit: str | None = None,
174174
) -> TimedeltaIndex: ...
175175
@overload
176176
def timedelta_range(
@@ -180,7 +180,7 @@ def timedelta_range(
180180
freq: Frequency | Timedelta | timedelta | None = None,
181181
name: Hashable | None = None,
182182
closed: Literal["left", "right"] | None = None,
183-
unit: None | str = None,
183+
unit: str | None = None,
184184
) -> TimedeltaIndex: ...
185185
@overload
186186
def timedelta_range(
@@ -190,5 +190,5 @@ def timedelta_range(
190190
*,
191191
name: Hashable | None = None,
192192
closed: Literal["left", "right"] | None = None,
193-
unit: None | str = None,
193+
unit: str | None = None,
194194
) -> TimedeltaIndex: ...

pandas-stubs/core/series.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ from pandas.core.window.rolling import (
128128
Rolling,
129129
Window,
130130
)
131+
from typing_extensions import override
131132
import xarray as xr
132133

133134
from pandas._libs.interval import Interval
@@ -1874,7 +1875,8 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
18741875
@final
18751876
def last_valid_index(self) -> Scalar: ...
18761877
@overload
1877-
def value_counts( # pyrefly: ignore
1878+
@override
1879+
def value_counts(
18781880
self,
18791881
normalize: Literal[False] = False,
18801882
sort: _bool = ...,
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
from datetime import timedelta
2-
from typing import overload
1+
from pandas.core.indexes.datetimes import DatetimeIndex
2+
from pandas.core.indexes.timedeltas import TimedeltaIndex
3+
from pandas.core.series import Series
34

4-
from pandas import (
5-
DatetimeIndex,
6-
Series,
7-
TimedeltaIndex,
8-
)
5+
from pandas._libs.tslibs.offsets import to_offset as to_offset
96

10-
from pandas._typing import Frequency
11-
12-
from pandas.tseries.offsets import BaseOffset
13-
14-
@overload
15-
def to_offset(freq: None, is_period: bool = False) -> None: ...
16-
@overload
17-
def to_offset(freq: Frequency | timedelta, is_period: bool = False) -> BaseOffset: ...
187
def infer_freq(index: Series | DatetimeIndex | TimedeltaIndex) -> str | None: ...

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ extend-select = ["ALL"]
201201
ignore = [
202202
# The following rules are ignored permanently for good reasons
203203
"COM", # https://docs.astral.sh/ruff/rules/#flake8-commas-com
204+
"CPY001", # https://docs.astral.sh/ruff/rules/missing-copyright-notice/
204205
"D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
205206
"E501", # handled by black https://docs.astral.sh/ruff/rules/line-too-long/
206207
"FBT", # https://docs.astral.sh/ruff/rules/#flake8-boolean-trap-fbt
@@ -347,10 +348,12 @@ filterwarnings = [
347348
project-includes = ["pandas-stubs", "tests"]
348349
enabled-ignores = ["pyrefly"]
349350
python-version = "3.11"
351+
min-severity = "warn"
350352

351353
[tool.pyrefly.errors]
352354
explicit-any = false # We do use implicit Any
353355
implicit-any-type-argument = false # We do use implicit Any
356+
implicit-bool = false # We do use implicit bool
354357
untyped-import = false # Import discovery
355358

356359
[tool.ty.environment]

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ def check(
9191
value: Any
9292
if isinstance(actual, pd.Series):
9393
# cast is by design microsoft/pyright#11191
94+
# pyrefly: ignore[redundant-cast]
9495
value = cast(pd.Series, actual).iloc[index_to_check_for_type]
9596
elif isinstance(actual, pd.Index):
97+
# pyrefly: ignore[redundant-cast]
9698
# cast is by design microsoft/pyright#11191
9799
value = cast(pd.Index, actual)[index_to_check_for_type]
98100
elif isinstance(actual, BaseGroupBy):

0 commit comments

Comments
 (0)