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 <stephenfin@redhat.com>
Change-Id: I0e079ed98dc6deedbc4e48a00a7ededdd8d46429
This commit is contained in:
Stephen Finucane 2024-08-12 11:23:04 +01:00
parent 8d2e02e9c0
commit f117b0d31b
4 changed files with 48 additions and 15 deletions

View File

@ -43,6 +43,7 @@ repos:
- types-PyYAML
- types-requests
- types-simplejson
- iso8601
# keep this in-sync with '[mypy] exclude' in 'setup.cfg'
exclude: |
(?x)(

View File

@ -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

View File

@ -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

View File

@ -128,3 +128,6 @@ disallow_untyped_defs = true
[mypy-keystoneauth1._fair_semaphore]
disallow_untyped_defs = true
[mypy-keystoneauth1._utils]
disallow_untyped_defs = true