Skip to content

Commit

Permalink
Switch to Python 3.12's recommended way of getting UTC time (TZ-aware)
Browse files Browse the repository at this point in the history
The old TZ-naive way is deprecated and soon will be removed — this warning causes exceptions in strict-mode tests.

Signed-off-by: Sergey Vasilyev <[email protected]>
  • Loading branch information
nolar committed Oct 9, 2023
1 parent 44df8dd commit f831ddc
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ is no need to retry over time, as it will not become better::
@kopf.on.create('kopfexamples')
def create_fn(spec, **_):
valid_until = datetime.datetime.fromisoformat(spec['validUntil'])
if valid_until <= datetime.datetime.utcnow():
if valid_until <= datetime.datetime.now(datetime.timezone.utc):
raise kopf.PermanentError("The object is not valid anymore.")

See also: :ref:`never-again-filters` to prevent handlers from being invoked
Expand Down
4 changes: 2 additions & 2 deletions docs/probing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ probing handlers:
@kopf.on.probe(id='now')
def get_current_timestamp(**kwargs):
return datetime.datetime.utcnow().isoformat()
return datetime.datetime.now(datetime.timezone.utc).isoformat()
@kopf.on.probe(id='random')
def get_random_value(**kwargs):
Expand All @@ -91,7 +91,7 @@ The handler results will be reported as the content of the liveness response:
.. code-block:: console
$ curl http://localhost:8080/healthz
{"now": "2019-11-07T18:03:52.513803", "random": 765846}
{"now": "2019-11-07T18:03:52.513803+00:00", "random": 765846}
.. note::
The liveness status report is simplistic and minimalistic at the moment.
Expand Down
2 changes: 1 addition & 1 deletion examples/13-hooks/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def login_fn(**kwargs):
certificate_path=cert.filename() if cert else None, # can be a temporary file
private_key_path=pkey.filename() if pkey else None, # can be a temporary file
default_namespace=config.namespace,
expiration=datetime.datetime.utcnow() + datetime.timedelta(seconds=30),
expiration=datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=30),
)


Expand Down
8 changes: 4 additions & 4 deletions kopf/_cogs/clients/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def post_event(
suffix = message[-MAX_MESSAGE_LENGTH // 2 + (len(infix) - len(infix) // 2):]
message = f'{prefix}{infix}{suffix}'

now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
body = {
'metadata': {
'namespace': namespace,
Expand All @@ -67,9 +67,9 @@ async def post_event(

'involvedObject': full_ref,

'firstTimestamp': now.isoformat() + 'Z', # '2019-01-28T18:25:03.000000Z' -- seen in `kubectl describe ...`
'lastTimestamp': now.isoformat() + 'Z', # '2019-01-28T18:25:03.000000Z' - seen in `kubectl get events`
'eventTime': now.isoformat() + 'Z', # '2019-01-28T18:25:03.000000Z'
'firstTimestamp': now.isoformat(), # '2019-01-28T18:25:03.000000+00:00' -- seen in `kubectl describe ...`
'lastTimestamp': now.isoformat(), # '2019-01-28T18:25:03.000000+00:00' - seen in `kubectl get events`
'eventTime': now.isoformat(), # '2019-01-28T18:25:03.000000+00:00'
}

try:
Expand Down
6 changes: 4 additions & 2 deletions kopf/_cogs/structs/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ async def expire(self) -> None:
Unlike invalidation, the expired credentials are not remembered
and not blocked from reappearing.
"""
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
if self._next_expiration.tzinfo is None:
now = now.replace(tzinfo=None) # for comparability
if now >= self._next_expiration: # quick & lockless for speed: it is done on every API call
async with self._lock:
for key, item in list(self._current.items()):
Expand Down Expand Up @@ -315,7 +317,7 @@ async def populate(
await self._ready.turn_to(True)

def is_empty(self) -> bool:
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
return all(
item.info.expiration is not None and now >= item.info.expiration # i.e. expired
for key, item in self._current.items()
Expand Down
2 changes: 1 addition & 1 deletion kopf/_core/actions/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def apply(
logger.debug(f"Sleeping was interrupted by new changes, {unslept_delay} seconds left.")
else:
# Any unique always-changing value will work; not necessary a timestamp.
value = datetime.datetime.utcnow().isoformat()
value = datetime.datetime.now(datetime.timezone.utc).isoformat()
touch = patches.Patch()
settings.persistence.progress_storage.touch(body=body, patch=touch, value=value)
await patch_and_check(
Expand Down
6 changes: 3 additions & 3 deletions kopf/_core/actions/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def finished(self) -> bool:
@property
def sleeping(self) -> bool:
ts = self.delayed
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
return not self.finished and ts is not None and ts > now

@property
Expand All @@ -122,7 +122,7 @@ def awakened(self) -> bool:

@property
def runtime(self) -> datetime.timedelta:
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
return now - (self.started if self.started else now)


Expand Down Expand Up @@ -277,7 +277,7 @@ async def execute_handler_once(
handler=handler,
cause=cause,
retry=state.retries,
started=state.started or datetime.datetime.utcnow(), # "or" is for type-checking.
started=state.started or datetime.datetime.now(datetime.timezone.utc), # "or" is for type-checking.
runtime=state.runtime,
settings=settings,
lifecycle=lifecycle, # just a default for the sub-handlers, not used directly.
Expand Down
13 changes: 8 additions & 5 deletions kopf/_core/actions/progression.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ class HandlerState(execution.HandlerState):
def from_scratch(cls, *, purpose: Optional[str] = None) -> "HandlerState":
return cls(
active=True,
started=datetime.datetime.utcnow(),
started=datetime.datetime.now(datetime.timezone.utc),
purpose=purpose,
)

@classmethod
def from_storage(cls, __d: progress.ProgressRecord) -> "HandlerState":
return cls(
active=False,
started=_datetime_fromisoformat(__d.get('started')) or datetime.datetime.utcnow(),
started=_datetime_fromisoformat(__d.get('started')) or datetime.datetime.now(datetime.timezone.utc),
stopped=_datetime_fromisoformat(__d.get('stopped')),
delayed=_datetime_fromisoformat(__d.get('delayed')),
purpose=__d.get('purpose') if __d.get('purpose') else None,
Expand Down Expand Up @@ -104,7 +104,7 @@ def with_outcome(
self,
outcome: execution.Outcome,
) -> "HandlerState":
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
cls = type(self)
return cls(
active=self.active,
Expand Down Expand Up @@ -313,7 +313,7 @@ def delays(self) -> Collection[float]:
processing routine, based on all delays of different origin:
e.g. postponed daemons, stopping daemons, temporarily failed handlers.
"""
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
return [
max(0, (handler_state.delayed - now).total_seconds()) if handler_state.delayed else 0
for handler_state in self._states.values()
Expand Down Expand Up @@ -381,4 +381,7 @@ def _datetime_fromisoformat(val: Optional[str]) -> Optional[datetime.datetime]:
if val is None:
return None
else:
return datetime.datetime.fromisoformat(val)
dt = datetime.datetime.fromisoformat(val)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=datetime.timezone.utc)
return dt
8 changes: 4 additions & 4 deletions kopf/_core/engines/peering.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def __init__(
self.priority = priority
self.lifetime = datetime.timedelta(seconds=int(lifetime))
self.lastseen = (iso8601.parse_date(lastseen) if lastseen is not None else
datetime.datetime.utcnow())
datetime.datetime.now(datetime.timezone.utc))
self.lastseen = self.lastseen.replace(tzinfo=None) # only the naive utc -- for comparison
self.deadline = self.lastseen + self.lifetime
self.is_dead = self.deadline <= datetime.datetime.utcnow()
self.is_dead = self.deadline <= datetime.datetime.now(datetime.timezone.utc)

def __repr__(self) -> str:
clsname = self.__class__.__name__
Expand Down Expand Up @@ -149,7 +149,7 @@ async def process_peering_event(
# are expected to expire, and force the immediate re-evaluation by a certain change of self.
# This incurs an extra PATCH request besides usual keepalives, but in the complete silence
# from other peers that existed a moment earlier, this should not be a problem.
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
delays = [(peer.deadline - now).total_seconds() for peer in same_peers + prio_peers]
unslept = await aiotime.sleep(delays, wakeup=stream_pressure)
if unslept is None and delays:
Expand Down Expand Up @@ -279,7 +279,7 @@ def detect_own_id(*, manual: bool) -> Identity:

user = getpass.getuser()
host = hostnames.get_descriptive_hostname()
now = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%d%H%M%S")
rnd = ''.join(random.choices('abcdefhijklmnopqrstuvwxyz0123456789', k=3))
return Identity(f'{user}@{host}' if manual else f'{user}@{host}/{now}/{rnd}')

Expand Down
6 changes: 3 additions & 3 deletions kopf/_core/engines/probing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ async def get_health(

# Recollect the data on-demand, and only if is is older that a reasonable caching period.
# Protect against multiple parallel requests performing the same heavy activity.
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
if probing_timestamp is None or now - probing_timestamp >= probing_max_age:
async with probing_lock:
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
if probing_timestamp is None or now - probing_timestamp >= probing_max_age:

activity_results = await activities.run_activity(
Expand All @@ -64,7 +64,7 @@ async def get_health(
)
probing_container.clear()
probing_container.update(activity_results)
probing_timestamp = datetime.datetime.utcnow()
probing_timestamp = datetime.datetime.now(datetime.timezone.utc)

return aiohttp.web.json_response(probing_container)

Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def pytest_configure(config):
# TODO: Remove when fixed in https://github.com/pytest-dev/pytest-asyncio/issues/460:
config.addinivalue_line('filterwarnings', 'ignore:There is no current event loop:DeprecationWarning:pytest_asyncio')

# Python 3.12 transitional period:
config.addinivalue_line('filterwarnings', 'ignore:datetime.datetime.utcfromtimestamp.*:DeprecationWarning:dateutil')
config.addinivalue_line('filterwarnings', 'ignore:datetime.datetime.utcfromtimestamp.*:DeprecationWarning:freezegun')


def pytest_addoption(parser):
parser.addoption("--only-e2e", action="store_true", help="Execute end-to-end tests only.")
Expand Down
4 changes: 2 additions & 2 deletions tests/handling/test_timing_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def move_to_tsB(*_, **__):
# Simulate the call as if the event has just arrived on the watch-stream.
# Another way (the same effect): process_changing_cause() and its result.
with freezegun.freeze_time(tsA_triggered) as frozen_dt:
assert datetime.datetime.utcnow() < ts0 # extra precaution
assert datetime.datetime.now(datetime.timezone.utc) < ts0 # extra precaution
await process_resource_event(
lifecycle=kopf.lifecycles.all_at_once,
registry=registry,
Expand All @@ -68,7 +68,7 @@ def move_to_tsB(*_, **__):
raw_event={'type': 'ADDED', 'object': body},
event_queue=asyncio.Queue(),
)
assert datetime.datetime.utcnow() > ts0 # extra precaution
assert datetime.datetime.now(datetime.timezone.utc) > ts0 # extra precaution

assert state_store.called

Expand Down

0 comments on commit f831ddc

Please sign in to comment.