Skip to content

Update ruff config, use ruff format instead of black #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ repos:
exclude: ^tests/.*/fixtures/.*
- id: debug-statements

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
rev: v0.11.2
hooks:
- id: ruff
- id: ruff
- id: ruff-format

- repo: local
hooks:
Expand Down
2 changes: 1 addition & 1 deletion clock
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ translations = {{}}
v = repr(v)

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

return "".join(s)

Expand Down
48 changes: 27 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,50 @@ module-name = "pendulum._pendulum"

[tool.ruff]
fix = true
unfixable = [
"ERA", # do not autoremove commented out code
]
target-version = "py39"
line-length = 88
target-version = "py39"
extend-exclude = [
# External to the project's coding standards:
"docs/*",
# Machine-generated, too many false-positives
"src/pendulum/locales/*",
# ruff disagrees with black when it comes to formatting
"*.pyi",
]

[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"ERA", # flake8-eradicate/eradicate
"I", # isort
"N", # pep8-naming
"I", # isort
"N", # pep8-naming
"PIE", # flake8-pie
"PGH", # pygrep
"RUF", # ruff checks
"SIM", # flake8-simplify
"T20", # flake8-print
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"UP", # pyupgrade
]
ignore = [
"B904", # use 'raise ... from err'
"B905", # use explicit 'strict=' parameter with 'zip()'
"N818", # Exception name should be named with an Error suffix
"RUF001",
"N818",
"RUF001"
]
extend-exclude = [
# External to the project's coding standards:
"docs/*",
# Machine-generated, too many false-positives
"src/pendulum/locales/*",
# ruff disagrees with black when it comes to formatting
"*.pyi",
extend-safe-fixes = [
"TCH", # move import from and to TYPE_CHECKING blocks
]
unfixable = [
"ERA", # do not autoremove commented out code
]

[tool.ruff.flake8-tidy-imports]
[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.ruff.isort]
[tool.ruff.lint.isort]
force-single-line = true
lines-between-types = 1
lines-after-imports = 2
Expand All @@ -119,7 +125,7 @@ known-third-party = [
]
required-imports = ["from __future__ import annotations"]

[tool.ruff.extend-per-file-ignores]
[tool.ruff.lint.extend-per-file-ignores]
"build.py" = ["I002"]
"clock" = ["RUF012"]

Expand Down
40 changes: 17 additions & 23 deletions src/pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,15 @@


@overload
def timezone(name: int) -> FixedTimezone:
...
def timezone(name: int) -> FixedTimezone: ...


@overload
def timezone(name: str) -> Timezone:
...
def timezone(name: str) -> Timezone: ...


@overload
def timezone(name: str | int) -> Timezone | FixedTimezone:
...
def timezone(name: str | int) -> Timezone | FixedTimezone: ...


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

obj = int(offset.total_seconds())

obj = cast(Union[str, int], obj)
obj = cast("Union[str, int]", obj)

return timezone(obj)

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


@overload
def instance(
obj: _datetime.date,
tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
) -> Date:
...
) -> Date: ...


@overload
def instance(
obj: _datetime.time,
tz: str | Timezone | FixedTimezone | _datetime.tzinfo | None = UTC,
) -> Time:
...
) -> Time: ...


def instance(
Expand Down Expand Up @@ -350,22 +344,27 @@ def interval(
travel_back = _traveller.travel_back

__all__ = [
"__version__",
"DAYS_PER_WEEK",
"HOURS_PER_DAY",
"MINUTES_PER_HOUR",
"MONTHS_PER_YEAR",
"SECONDS_PER_DAY",
"SECONDS_PER_HOUR",
"SECONDS_PER_MINUTE",
"UTC",
"WEEKS_PER_YEAR",
"YEARS_PER_CENTURY",
"YEARS_PER_DECADE",
"Date",
"DateTime",
"Duration",
"FixedTimezone",
"Formatter",
"Interval",
"Time",
"Timezone",
"WeekDay",
"__version__",
"date",
"datetime",
"duration",
Expand All @@ -377,18 +376,13 @@ def interval(
"instance",
"interval",
"local",
"local_timezone",
"locale",
"naive",
"now",
"set_locale",
"week_ends_at",
"week_starts_at",
"parse",
"Interval",
"Time",
"UTC",
"local_timezone",
"set_local_timezone",
"set_locale",
"test_local_timezone",
"time",
"timezone",
Expand All @@ -398,7 +392,7 @@ def interval(
"travel",
"travel_back",
"travel_to",
"FixedTimezone",
"Timezone",
"week_ends_at",
"week_starts_at",
"yesterday",
]
20 changes: 11 additions & 9 deletions src/pendulum/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import datetime
import math
import zoneinfo

from typing import TYPE_CHECKING
from typing import NamedTuple
from typing import cast

Expand All @@ -22,7 +22,12 @@
from pendulum.constants import SECS_PER_YEAR
from pendulum.constants import TM_DECEMBER
from pendulum.constants import TM_JANUARY
from pendulum.tz.timezone import Timezone


if TYPE_CHECKING:
import zoneinfo

from pendulum.tz.timezone import Timezone


class PreciseDiff(NamedTuple):
Expand Down Expand Up @@ -174,11 +179,8 @@ def precise_diff(
d2.tzinfo if isinstance(d2, datetime.datetime) else None
)

if (
tzinfo1 is None
and tzinfo2 is not None
or tzinfo2 is None
and tzinfo1 is not None
if (tzinfo1 is None and tzinfo2 is not None) or (
tzinfo2 is None and tzinfo1 is not None
):
raise ValueError(
"Comparison between naive and aware datetimes is not supported"
Expand Down Expand Up @@ -324,10 +326,10 @@ def _get_tzinfo_name(tzinfo: datetime.tzinfo | None) -> str | None:

if hasattr(tzinfo, "key"):
# zoneinfo timezone
return cast(zoneinfo.ZoneInfo, tzinfo).key
return cast("zoneinfo.ZoneInfo", tzinfo).key
elif hasattr(tzinfo, "name"):
# Pendulum timezone
return cast(Timezone, tzinfo).name
return cast("Timezone", tzinfo).name
elif hasattr(tzinfo, "zone"):
# pytz timezone
return tzinfo.zone # type: ignore[no-any-return]
Expand Down
9 changes: 3 additions & 6 deletions src/pendulum/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,13 @@ def __add__(self, other: timedelta) -> Self:
return self._add_timedelta(other)

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

@overload
def __sub__(self, __dt: datetime) -> NoReturn:
...
def __sub__(self, __dt: datetime) -> NoReturn: ...

@overload
def __sub__(self, __dt: Self) -> Interval[Date]:
...
def __sub__(self, __dt: Self) -> Interval[Date]: ...

def __sub__(self, other: timedelta | date) -> Self | Interval[Date]:
if isinstance(other, timedelta):
Expand Down
16 changes: 6 additions & 10 deletions src/pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,11 @@ def instance(

@overload
@classmethod
def now(cls, tz: datetime.tzinfo | None = None) -> Self:
...
def now(cls, tz: datetime.tzinfo | None = None) -> Self: ...

@overload
@classmethod
def now(cls, tz: str | Timezone | FixedTimezone | None = None) -> Self:
...
def now(cls, tz: str | Timezone | FixedTimezone | None = None) -> Self: ...

@classmethod
def now(
Expand Down Expand Up @@ -474,7 +472,7 @@ def __repr__(self) -> str:
if self.microsecond:
us = f", {self.microsecond}"

repr_ = "{klass}(" "{year}, {month}, {day}, " "{hour}, {minute}, {second}{us}"
repr_ = "{klass}({year}, {month}, {day}, {hour}, {minute}, {second}{us}"

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

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

@overload # type: ignore[override]
def __sub__(self, other: datetime.timedelta) -> Self:
...
def __sub__(self, other: datetime.timedelta) -> Self: ...

@overload
def __sub__(self, other: DateTime) -> Interval[datetime.datetime]:
...
def __sub__(self, other: DateTime) -> Interval[datetime.datetime]: ...

def __sub__(
self, other: datetime.datetime | datetime.timedelta
Expand Down
Loading
Loading