Skip to content

Commit

Permalink
update line-length
Browse files Browse the repository at this point in the history
fix duration __repr__ and __str__ format
fix duration parser (broken in previous commit)
  • Loading branch information
gweis committed Oct 9, 2024
1 parent aa3a84d commit f9461d4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
hooks:
- id: flake8
args:
- "--max-line-length=88"
- "--max-line-length=100"

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ source_pkgs = ["isodate"]
omit = ["tests/"]

[tool.black]
line-length = 88
line-length = 100

[tool.isort]
profile = "black"
8 changes: 4 additions & 4 deletions src/isodate/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ def __str__(self):
"""Return a string representation of this duration similar to timedelta."""
params: list[str] = []
if self.years:
params.append("%d years" % self.years)
params.append("%s years" % self.years)
if self.months:
fmt = "%d months"
fmt = "%s months"
if self.months <= 1:
fmt = "%d month"
fmt = "%s month"
params.append(fmt % self.months)
params.append(str(self.tdelta))
return ", ".join(params)

def __repr__(self):
"""Return a string suitable for repr(x) calls."""
return "%s.%s(%d, %d, %d, years=%d, months=%d)" % (
return "{}.{}({}, {}, {}, years={}, months={})".format(
self.__class__.__module__,
self.__class__.__name__,
self.tdelta.days,
Expand Down
3 changes: 1 addition & 2 deletions src/isodate/isodatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

from datetime import date, datetime, time, timedelta
from typing import Union

import isodate
from isodate.isodates import parse_date
Expand Down Expand Up @@ -36,7 +35,7 @@ def parse_datetime(datetimestring: str) -> datetime:


def datetime_isoformat(
tdt: Union[timedelta, isodate.isoduration.Duration, time, date],
tdt: timedelta | isodate.isoduration.Duration | time | date,
format: str = DATE_EXT_COMPLETE + "T" + TIME_EXT_COMPLETE + TZ_EXT,
) -> str:
"""Format datetime strings.
Expand Down
26 changes: 14 additions & 12 deletions src/isodate/isoduration.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,25 @@ def parse_duration(
groups[key] = float(groups[key][:-1].replace(",", "."))
if as_timedelta_if_possible and groups["years"] == 0 and groups["months"] == 0:
ret = timedelta(
days=int(groups["days"]),
hours=int(groups["hours"]),
minutes=int(groups["minutes"]),
seconds=int(groups["seconds"]),
weeks=int(groups["weeks"]),
# values have been converted to float or Decimal
days=groups["days"], # type: ignore [arg-type]
hours=groups["hours"], # type: ignore [arg-type]
minutes=groups["minutes"], # type: ignore [arg-type]
seconds=groups["seconds"], # type: ignore [arg-type]
weeks=groups["weeks"], # type: ignore [arg-type]
)
if groups["sign"] == "-":
ret = timedelta(0) - ret
else:
ret = Duration(
years=int(groups["years"]),
months=int(groups["months"]),
days=int(groups["days"]),
hours=int(groups["hours"]),
minutes=int(groups["minutes"]),
seconds=int(groups["seconds"]),
weeks=int(groups["weeks"]),
# values have been converted to float or Decimal
years=groups["years"], # type: ignore [arg-type]
months=groups["months"], # type: ignore [arg-type]
days=groups["days"], # type: ignore [arg-type]
hours=groups["hours"], # type: ignore [arg-type]
minutes=groups["minutes"], # type: ignore [arg-type]
seconds=groups["seconds"], # type: ignore [arg-type]
weeks=groups["weeks"], # type: ignore [arg-type]
)
if groups["sign"] == "-":
ret = Duration(0) - ret
Expand Down
6 changes: 3 additions & 3 deletions src/isodate/isostrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
"%d": lambda tdt, yds: "%02d" % tdt.day, # type: ignore [union-attr]
"%f": lambda tdt, yds: "%06d" % tdt.microsecond, # type: ignore [union-attr]
"%H": lambda tdt, yds: "%02d" % tdt.hour, # type: ignore [union-attr]
"%j": lambda tdt, yds: "%03d" % (tdt.toordinal() - date(tdt.year, 1, 1).toordinal() + 1), # type: ignore [union-attr, operator]
"%j": lambda tdt, yds: "%03d" % (tdt.toordinal() - date(tdt.year, 1, 1).toordinal() + 1), # type: ignore [union-attr, operator] # noqa: E501
"%m": lambda tdt, yds: "%02d" % tdt.month, # type: ignore [union-attr]
"%M": lambda tdt, yds: "%02d" % tdt.minute, # type: ignore [union-attr]
"%S": lambda tdt, yds: "%02d" % tdt.second, # type: ignore [union-attr]
"%w": lambda tdt, yds: "%1d" % tdt.isoweekday(), # type: ignore [union-attr]
"%W": lambda tdt, yds: "%02d" % tdt.isocalendar()[1], # type: ignore [union-attr]
"%Y": lambda tdt, yds: (((yds != 4) and "+") or "") + (("%%0%dd" % yds) % tdt.year), # type: ignore [union-attr]
"%Y": lambda tdt, yds: (((yds != 4) and "+") or "") + (("%%0%dd" % yds) % tdt.year), # type: ignore [union-attr] # noqa: E501
"%C": lambda tdt, yds: (((yds != 4) and "+") or "") # type: ignore [union-attr]
+ (("%%0%dd" % (yds - 2)) % (tdt.year / 100)), # type: ignore [union-attr]
"%h": lambda tdt, yds: tz_isoformat(tdt, "%h"), # type: ignore [arg-type]
Expand All @@ -83,7 +83,7 @@
"%M": lambda tdt, yds: "%02d" % ((tdt.seconds / 60) % 60),
"%S": lambda tdt, yds: "%02d" % (tdt.seconds % 60),
"%W": lambda tdt, yds: "%02d" % (abs(tdt.days / 7)),
"%Y": lambda tdt, yds: (((yds != 4) and "+") or "") + (("%%0%dd" % yds) % tdt.years), # type: ignore [union-attr]
"%Y": lambda tdt, yds: (((yds != 4) and "+") or "") + (("%%0%dd" % yds) % tdt.years), # type: ignore [union-attr] # noqa: E501
"%C": lambda tdt, yds: (((yds != 4) and "+") or "")
+ (("%%0%dd" % (yds - 2)) % (tdt.years / 100)), # type: ignore [union-attr]
"%%": lambda tdt, yds: "%",
Expand Down

0 comments on commit f9461d4

Please sign in to comment.