From f9461d474dc304d1bb7edaaa29deb8a4e753fcf5 Mon Sep 17 00:00:00 2001 From: Gerhard Weis Date: Wed, 9 Oct 2024 11:45:13 +1000 Subject: [PATCH] update line-length fix duration __repr__ and __str__ format fix duration parser (broken in previous commit) --- .pre-commit-config.yaml | 2 +- pyproject.toml | 2 +- src/isodate/duration.py | 8 ++++---- src/isodate/isodatetime.py | 3 +-- src/isodate/isoduration.py | 26 ++++++++++++++------------ src/isodate/isostrf.py | 6 +++--- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 02a2647..45c3000 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/pyproject.toml b/pyproject.toml index ef323a7..d3e9122 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,7 +57,7 @@ source_pkgs = ["isodate"] omit = ["tests/"] [tool.black] -line-length = 88 +line-length = 100 [tool.isort] profile = "black" diff --git a/src/isodate/duration.py b/src/isodate/duration.py index 0e5af4f..85241e4 100644 --- a/src/isodate/duration.py +++ b/src/isodate/duration.py @@ -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, diff --git a/src/isodate/isodatetime.py b/src/isodate/isodatetime.py index bafd708..c69a46a 100644 --- a/src/isodate/isodatetime.py +++ b/src/isodate/isodatetime.py @@ -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 @@ -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. diff --git a/src/isodate/isoduration.py b/src/isodate/isoduration.py index 6b396b4..eacb15b 100644 --- a/src/isodate/isoduration.py +++ b/src/isodate/isoduration.py @@ -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 diff --git a/src/isodate/isostrf.py b/src/isodate/isostrf.py index e77fec9..f80b6c2 100644 --- a/src/isodate/isostrf.py +++ b/src/isodate/isostrf.py @@ -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] @@ -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: "%",