From a6e59c6373468f373742e547f1fb87f60665cdc9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Brigitta=20Sip=C5=91cz?= Date: Fri, 6 Sep 2024 02:44:53 -0400 Subject: [PATCH] MAINT: cleanup the lasts of datetime.utcnow() ### Description I'm chasing some loose datetime.datetime.utcnow() deprecation warning in some test suites, and one of these was seemingly coming from sqlalchemy. It wasn't, but nevertheless these minor cleanup changes may still be found useful. ### Checklist This pull request is: - [x] A documentation / typographical / small typing error fix - Good to go, no issue or tests are needed **Have a nice day!** Closes: #11736 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11736 Pull-request-sha: 9bee8af8d1082c3cde5f64c78f1e565ef4ab14cd Change-Id: Ib1b85fa3d66b665165d908e7c8394482b714c57f (cherry picked from commit 6fefae897a576bce9ec74101e3a5ebcda0557c00) --- examples/extending_query/temporal_range.py | 5 ++++- lib/sqlalchemy/orm/events.py | 4 +++- test/orm/test_relationship_criteria.py | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/extending_query/temporal_range.py b/examples/extending_query/temporal_range.py index 50cbb66459..29ea119362 100644 --- a/examples/extending_query/temporal_range.py +++ b/examples/extending_query/temporal_range.py @@ -5,6 +5,7 @@ to selected entities. """ import datetime +from functools import partial from sqlalchemy import Column from sqlalchemy import create_engine @@ -23,7 +24,9 @@ class HasTemporal: """Mixin that identifies a class as having a timestamp column""" timestamp = Column( - DateTime, default=datetime.datetime.utcnow, nullable=False + DateTime, + default=partial(datetime.datetime.now, datetime.timezone.utc), + nullable=False, ) diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py index 1cd51bfd01..f2eae852b3 100644 --- a/lib/sqlalchemy/orm/events.py +++ b/lib/sqlalchemy/orm/events.py @@ -3155,7 +3155,9 @@ class QueryEvents(event.Events[Query[Any]]): entity = desc['entity'] query = query.filter(entity.deleted == False) - update_context.values['timestamp'] = datetime.utcnow() + update_context.values['timestamp'] = ( + datetime.datetime.now(datetime.UTC) + ) return query The ``.values`` dictionary of the "update context" object can also diff --git a/test/orm/test_relationship_criteria.py b/test/orm/test_relationship_criteria.py index 96c178e5e2..29720f7dc8 100644 --- a/test/orm/test_relationship_criteria.py +++ b/test/orm/test_relationship_criteria.py @@ -1,6 +1,7 @@ from __future__ import annotations import datetime +from functools import partial import random from typing import List @@ -1661,7 +1662,9 @@ class TemporalFixtureTest(testing.fixtures.DeclarativeMappedTest): """Mixin that identifies a class as having a timestamp column""" timestamp = Column( - DateTime, default=datetime.datetime.utcnow, nullable=False + DateTime, + default=partial(datetime.datetime.now, datetime.timezone.utc), + nullable=False, ) cls.HasTemporal = HasTemporal -- 2.47.3