From f117b0d31bfdd1b768aa3ba33c6e03d6c711912b Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 12 Aug 2024 11:23:04 +0100 Subject: [PATCH] typing: Annotate keystoneauth1._utils We bump the version of iso8601 to a major version which we know includes typing information. Signed-off-by: Stephen Finucane Change-Id: I0e079ed98dc6deedbc4e48a00a7ededdd8d46429 --- .pre-commit-config.yaml | 1 + keystoneauth1/_utils.py | 57 +++++++++++++++++++++++++++++++---------- requirements.txt | 2 +- setup.cfg | 3 +++ 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ae795be..7d863541 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,6 +43,7 @@ repos: - types-PyYAML - types-requests - types-simplejson + - iso8601 # keep this in-sync with '[mypy] exclude' in 'setup.cfg' exclude: | (?x)( diff --git a/keystoneauth1/_utils.py b/keystoneauth1/_utils.py index bd0c7446..4a7c13cd 100644 --- a/keystoneauth1/_utils.py +++ b/keystoneauth1/_utils.py @@ -12,11 +12,12 @@ import datetime import logging +import typing as ty import iso8601 -def get_logger(name): +def get_logger(name: str) -> logging.Logger: name = name.replace(__name__.split('.')[0], 'keystoneauth') return logging.getLogger(name) @@ -24,7 +25,7 @@ def get_logger(name): logger = get_logger(__name__) -def normalize_time(timestamp): +def normalize_time(timestamp: datetime.datetime) -> datetime.datetime: """Normalize time in arbitrary timezone to UTC naive object.""" offset = timestamp.utcoffset() if offset is None: @@ -32,7 +33,7 @@ def normalize_time(timestamp): return timestamp.replace(tzinfo=None) - offset -def parse_isotime(timestr): +def parse_isotime(timestr: str) -> datetime.datetime: """Parse time from ISO 8601 format.""" try: return iso8601.parse_date(timestr) @@ -42,12 +43,24 @@ def parse_isotime(timestr): raise ValueError(str(e)) -def from_utcnow(**timedelta_kwargs): - r"""Calculate the time in the future from utcnow. +def from_utcnow( + days: ty.Union[int, float] = 0, + seconds: ty.Union[int, float] = 0, + microseconds: ty.Union[int, float] = 0, + milliseconds: ty.Union[int, float] = 0, + minutes: ty.Union[int, float] = 0, + hours: ty.Union[int, float] = 0, + weeks: ty.Union[int, float] = 0, +) -> datetime.datetime: + """Calculate the time in the future from utcnow. - :param \*\*timedelta_kwargs: - Passed directly to :class:`datetime.timedelta` to add to the current - time in UTC. + :param days: Days to add to timestamp. + :param seconds: Seconds to add to timestamp. + :param microseconds: Microseconds to add to timestamp. + :param milliseconds: Milliseconds to add to timestamp. + :param minutes: Minutes to add to timestamp. + :param hours: Hours to add to timestamp. + :param weeks: Weeks to add to timestamp. :returns: The time in the future based on ``timedelta_kwargs`` and in TZ-naive format. @@ -55,16 +68,30 @@ def from_utcnow(**timedelta_kwargs): datetime.datetime """ now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - delta = datetime.timedelta(**timedelta_kwargs) + delta = datetime.timedelta( + days, seconds, microseconds, milliseconds, minutes, hours, weeks + ) return now + delta -def before_utcnow(**timedelta_kwargs): +def before_utcnow( + days: ty.Union[int, float] = 0, + seconds: ty.Union[int, float] = 0, + microseconds: ty.Union[int, float] = 0, + milliseconds: ty.Union[int, float] = 0, + minutes: ty.Union[int, float] = 0, + hours: ty.Union[int, float] = 0, + weeks: ty.Union[int, float] = 0, +) -> datetime.datetime: r"""Calculate the time in the past from utcnow. - :param \*\*timedelta_kwargs: - Passed directly to :class:`datetime.timedelta` to subtract from the - current time in UTC. + :param days: Days to remove from timestamp. + :param seconds: Seconds to remove from timestamp. + :param microseconds: Microseconds to remove from timestamp. + :param milliseconds: Milliseconds to remove from timestamp. + :param minutes: Minutes to remove from timestamp. + :param hours: Hours to remove from timestamp. + :param weeks: Weeks to remove from timestamp. :returns: The time in the past based on ``timedelta_kwargs`` and in TZ-naive format. @@ -72,7 +99,9 @@ def before_utcnow(**timedelta_kwargs): datetime.datetime """ now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - delta = datetime.timedelta(**timedelta_kwargs) + delta = datetime.timedelta( + days, seconds, microseconds, milliseconds, minutes, hours, weeks + ) return now - delta diff --git a/requirements.txt b/requirements.txt index b62322c1..1abd03fd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ # where oslo and associated transient dependencies are not desired. pbr>=2.0.0 # Apache-2.0 -iso8601>=0.1.11 # MIT +iso8601>=2.0.0 # MIT requests>=2.14.2 # Apache-2.0 stevedore>=1.20.0 # Apache-2.0 os-service-types>=1.2.0 # Apache-2.0 diff --git a/setup.cfg b/setup.cfg index 5a5dbb46..8f0ceb44 100644 --- a/setup.cfg +++ b/setup.cfg @@ -128,3 +128,6 @@ disallow_untyped_defs = true [mypy-keystoneauth1._fair_semaphore] disallow_untyped_defs = true + +[mypy-keystoneauth1._utils] +disallow_untyped_defs = true