Skip to content

Commit 5e925f3

Browse files
committed
Update ruff config, use ruff format instead of black
1 parent 3205e97 commit 5e925f3

20 files changed

+180
-198
lines changed

.pre-commit-config.yaml

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ repos:
1111
exclude: ^tests/.*/fixtures/.*
1212
- id: debug-statements
1313

14-
- repo: https://github.com/psf/black
15-
rev: 23.7.0
16-
hooks:
17-
- id: black
18-
1914
- repo: https://github.com/astral-sh/ruff-pre-commit
20-
rev: v0.0.291
15+
rev: v0.11.2
2116
hooks:
22-
- id: ruff
17+
- id: ruff
18+
- id: ruff-format
2319

2420
- repo: local
2521
hooks:

clock

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ translations = {{}}
196196
v = repr(v)
197197

198198
s.append(f"{' ' * tab}{k!r}: {v},\n")
199-
s.append(f'{" " * (tab - 1)}}}')
199+
s.append(f"{' ' * (tab - 1)}}}")
200200

201201
return "".join(s)
202202

pyproject.toml

+27-21
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,50 @@ module-name = "pendulum._pendulum"
6868

6969
[tool.ruff]
7070
fix = true
71-
unfixable = [
72-
"ERA", # do not autoremove commented out code
73-
]
74-
target-version = "py39"
7571
line-length = 88
72+
target-version = "py39"
73+
extend-exclude = [
74+
# External to the project's coding standards:
75+
"docs/*",
76+
# Machine-generated, too many false-positives
77+
"src/pendulum/locales/*",
78+
# ruff disagrees with black when it comes to formatting
79+
"*.pyi",
80+
]
81+
82+
[tool.ruff.lint]
7683
extend-select = [
77-
"B", # flake8-bugbear
78-
"C4", # flake8-comprehensions
84+
"B", # flake8-bugbear
85+
"C4", # flake8-comprehensions
7986
"ERA", # flake8-eradicate/eradicate
80-
"I", # isort
81-
"N", # pep8-naming
87+
"I", # isort
88+
"N", # pep8-naming
8289
"PIE", # flake8-pie
8390
"PGH", # pygrep
8491
"RUF", # ruff checks
8592
"SIM", # flake8-simplify
93+
"T20", # flake8-print
8694
"TCH", # flake8-type-checking
8795
"TID", # flake8-tidy-imports
88-
"UP", # pyupgrade
96+
"UP", # pyupgrade
8997
]
9098
ignore = [
9199
"B904", # use 'raise ... from err'
92100
"B905", # use explicit 'strict=' parameter with 'zip()'
93-
"N818", # Exception name should be named with an Error suffix
94-
"RUF001",
101+
"N818",
102+
"RUF001"
95103
]
96-
extend-exclude = [
97-
# External to the project's coding standards:
98-
"docs/*",
99-
# Machine-generated, too many false-positives
100-
"src/pendulum/locales/*",
101-
# ruff disagrees with black when it comes to formatting
102-
"*.pyi",
104+
extend-safe-fixes = [
105+
"TCH", # move import from and to TYPE_CHECKING blocks
106+
]
107+
unfixable = [
108+
"ERA", # do not autoremove commented out code
103109
]
104110

105-
[tool.ruff.flake8-tidy-imports]
111+
[tool.ruff.lint.flake8-tidy-imports]
106112
ban-relative-imports = "all"
107113

108-
[tool.ruff.isort]
114+
[tool.ruff.lint.isort]
109115
force-single-line = true
110116
lines-between-types = 1
111117
lines-after-imports = 2
@@ -119,7 +125,7 @@ known-third-party = [
119125
]
120126
required-imports = ["from __future__ import annotations"]
121127

122-
[tool.ruff.extend-per-file-ignores]
128+
[tool.ruff.lint.extend-per-file-ignores]
123129
"build.py" = ["I002"]
124130
"clock" = ["RUF012"]
125131

src/pendulum/__init__.py

+17-23
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@
5959

6060

6161
@overload
62-
def timezone(name: int) -> FixedTimezone:
63-
...
62+
def timezone(name: int) -> FixedTimezone: ...
6463

6564

6665
@overload
67-
def timezone(name: str) -> Timezone:
68-
...
66+
def timezone(name: str) -> Timezone: ...
6967

7068

7169
@overload
72-
def timezone(name: str | int) -> Timezone | FixedTimezone:
73-
...
70+
def timezone(name: str | int) -> Timezone | FixedTimezone: ...
7471

7572

7673
def timezone(name: str | int) -> Timezone | FixedTimezone:
@@ -119,7 +116,7 @@ def _safe_timezone(
119116

120117
obj = int(offset.total_seconds())
121118

122-
obj = cast(Union[str, int], obj)
119+
obj = cast("Union[str, int]", obj)
123120

124121
return timezone(obj)
125122

@@ -205,24 +202,21 @@ def time(hour: int, minute: int = 0, second: int = 0, microsecond: int = 0) -> T
205202
def instance(
206203
obj: _datetime.datetime,
207204
tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
208-
) -> DateTime:
209-
...
205+
) -> DateTime: ...
210206

211207

212208
@overload
213209
def instance(
214210
obj: _datetime.date,
215211
tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
216-
) -> Date:
217-
...
212+
) -> Date: ...
218213

219214

220215
@overload
221216
def instance(
222217
obj: _datetime.time,
223218
tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
224-
) -> Time:
225-
...
219+
) -> Time: ...
226220

227221

228222
def instance(
@@ -350,22 +344,27 @@ def interval(
350344
travel_back = _traveller.travel_back
351345

352346
__all__ = [
353-
"__version__",
354347
"DAYS_PER_WEEK",
355348
"HOURS_PER_DAY",
356349
"MINUTES_PER_HOUR",
357350
"MONTHS_PER_YEAR",
358351
"SECONDS_PER_DAY",
359352
"SECONDS_PER_HOUR",
360353
"SECONDS_PER_MINUTE",
354+
"UTC",
361355
"WEEKS_PER_YEAR",
362356
"YEARS_PER_CENTURY",
363357
"YEARS_PER_DECADE",
364358
"Date",
365359
"DateTime",
366360
"Duration",
361+
"FixedTimezone",
367362
"Formatter",
363+
"Interval",
364+
"Time",
365+
"Timezone",
368366
"WeekDay",
367+
"__version__",
369368
"date",
370369
"datetime",
371370
"duration",
@@ -377,18 +376,13 @@ def interval(
377376
"instance",
378377
"interval",
379378
"local",
379+
"local_timezone",
380380
"locale",
381381
"naive",
382382
"now",
383-
"set_locale",
384-
"week_ends_at",
385-
"week_starts_at",
386383
"parse",
387-
"Interval",
388-
"Time",
389-
"UTC",
390-
"local_timezone",
391384
"set_local_timezone",
385+
"set_locale",
392386
"test_local_timezone",
393387
"time",
394388
"timezone",
@@ -398,7 +392,7 @@ def interval(
398392
"travel",
399393
"travel_back",
400394
"travel_to",
401-
"FixedTimezone",
402-
"Timezone",
395+
"week_ends_at",
396+
"week_starts_at",
403397
"yesterday",
404398
]

src/pendulum/_helpers.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import datetime
44
import math
5-
import zoneinfo
65

6+
from typing import TYPE_CHECKING
77
from typing import NamedTuple
88
from typing import cast
99

@@ -22,7 +22,12 @@
2222
from pendulum.constants import SECS_PER_YEAR
2323
from pendulum.constants import TM_DECEMBER
2424
from pendulum.constants import TM_JANUARY
25-
from pendulum.tz.timezone import Timezone
25+
26+
27+
if TYPE_CHECKING:
28+
import zoneinfo
29+
30+
from pendulum.tz.timezone import Timezone
2631

2732

2833
class PreciseDiff(NamedTuple):
@@ -174,11 +179,8 @@ def precise_diff(
174179
d2.tzinfo if isinstance(d2, datetime.datetime) else None
175180
)
176181

177-
if (
178-
tzinfo1 is None
179-
and tzinfo2 is not None
180-
or tzinfo2 is None
181-
and tzinfo1 is not None
182+
if (tzinfo1 is None and tzinfo2 is not None) or (
183+
tzinfo2 is None and tzinfo1 is not None
182184
):
183185
raise ValueError(
184186
"Comparison between naive and aware datetimes is not supported"
@@ -324,10 +326,10 @@ def _get_tzinfo_name(tzinfo: datetime.tzinfo | None) -> str | None:
324326

325327
if hasattr(tzinfo, "key"):
326328
# zoneinfo timezone
327-
return cast(zoneinfo.ZoneInfo, tzinfo).key
329+
return cast("zoneinfo.ZoneInfo", tzinfo).key
328330
elif hasattr(tzinfo, "name"):
329331
# Pendulum timezone
330-
return cast(Timezone, tzinfo).name
332+
return cast("Timezone", tzinfo).name
331333
elif hasattr(tzinfo, "zone"):
332334
# pytz timezone
333335
return tzinfo.zone # type: ignore[no-any-return]

src/pendulum/date.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,13 @@ def __add__(self, other: timedelta) -> Self:
257257
return self._add_timedelta(other)
258258

259259
@overload # type: ignore[override] # this is only needed because of Python 3.7
260-
def __sub__(self, __delta: timedelta) -> Self:
261-
...
260+
def __sub__(self, __delta: timedelta) -> Self: ...
262261

263262
@overload
264-
def __sub__(self, __dt: datetime) -> NoReturn:
265-
...
263+
def __sub__(self, __dt: datetime) -> NoReturn: ...
266264

267265
@overload
268-
def __sub__(self, __dt: Self) -> Interval[Date]:
269-
...
266+
def __sub__(self, __dt: Self) -> Interval[Date]: ...
270267

271268
def __sub__(self, other: timedelta | date) -> Self | Interval[Date]:
272269
if isinstance(other, timedelta):

src/pendulum/datetime.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,11 @@ def instance(
146146

147147
@overload
148148
@classmethod
149-
def now(cls, tz: datetime.tzinfo | None = None) -> Self:
150-
...
149+
def now(cls, tz: datetime.tzinfo | None = None) -> Self: ...
151150

152151
@overload
153152
@classmethod
154-
def now(cls, tz: str | Timezone | FixedTimezone | None = None) -> Self:
155-
...
153+
def now(cls, tz: str | Timezone | FixedTimezone | None = None) -> Self: ...
156154

157155
@classmethod
158156
def now(
@@ -474,7 +472,7 @@ def __repr__(self) -> str:
474472
if self.microsecond:
475473
us = f", {self.microsecond}"
476474

477-
repr_ = "{klass}(" "{year}, {month}, {day}, " "{hour}, {minute}, {second}{us}"
475+
repr_ = "{klass}({year}, {month}, {day}, {hour}, {minute}, {second}{us}"
478476

479477
if self.tzinfo is not None:
480478
repr_ += ", tzinfo={tzinfo}"
@@ -1008,7 +1006,7 @@ def nth_of(self, unit: str, nth: int, day_of_week: WeekDay) -> Self:
10081006
if unit not in ["month", "quarter", "year"]:
10091007
raise ValueError(f'Invalid unit "{unit}" for first_of()')
10101008

1011-
dt = cast(Optional["Self"], getattr(self, f"_nth_of_{unit}")(nth, day_of_week))
1009+
dt = cast("Optional[Self]", getattr(self, f"_nth_of_{unit}")(nth, day_of_week))
10121010
if not dt:
10131011
raise PendulumException(
10141012
f"Unable to find occurrence {nth}"
@@ -1186,12 +1184,10 @@ def average( # type: ignore[override]
11861184
)
11871185

11881186
@overload # type: ignore[override]
1189-
def __sub__(self, other: datetime.timedelta) -> Self:
1190-
...
1187+
def __sub__(self, other: datetime.timedelta) -> Self: ...
11911188

11921189
@overload
1193-
def __sub__(self, other: DateTime) -> Interval[datetime.datetime]:
1194-
...
1190+
def __sub__(self, other: DateTime) -> Interval[datetime.datetime]: ...
11951191

11961192
def __sub__(
11971193
self, other: datetime.datetime | datetime.timedelta

0 commit comments

Comments
 (0)