Skip to content

Commit e2f21c3

Browse files
zzzeekGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Replace use of utcnow() with now() for Python 3.13 compatibility, Fixes #1643" into main
2 parents 6857ba2 + 97edb1a commit e2f21c3

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

alembic/script/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,10 @@ def _generate_create_date(self) -> datetime.datetime:
602602
raise util.CommandError(
603603
"Can't locate timezone: %s" % self.timezone
604604
) from None
605-
create_date = (
606-
datetime.datetime.utcnow()
607-
.replace(tzinfo=datetime.timezone.utc)
608-
.astimezone(tzinfo)
609-
)
605+
606+
create_date = datetime.datetime.now(
607+
tz=datetime.timezone.utc
608+
).astimezone(tzinfo)
610609
else:
611610
create_date = datetime.datetime.now()
612611
return create_date

docs/build/unreleased/1643.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: bug, environment
3+
:tickets: 1643
4+
5+
Fixed issue where use of deprecated ``utcnow()`` function would generate
6+
warnings. Has been replaced with ``now(UTC)``. Pull request courtesy
7+
Jens Tröger.

tests/test_script_production.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,13 @@ def _test_tz(self, timezone_arg, given, expected):
235235
with mock.patch(
236236
"alembic.script.base.datetime",
237237
mock.Mock(
238-
datetime=mock.Mock(utcnow=lambda: given, now=lambda: given),
238+
datetime=mock.Mock(
239+
now=lambda tz=None: (
240+
given.replace(tzinfo=datetime.timezone.utc)
241+
if timezone_arg
242+
else given
243+
)
244+
),
239245
timezone=datetime.timezone,
240246
),
241247
):
@@ -276,6 +282,19 @@ def test_custom_tzdata_tz(self):
276282
),
277283
)
278284

285+
def test_generates_a_date(self):
286+
"""test #1643"""
287+
script = ScriptDirectory(
288+
_get_staging_directory(),
289+
file_template="%(rev)s_%(slug)s_"
290+
"%(year)s_%(month)s_"
291+
"%(day)s_%(hour)s_"
292+
"%(minute)s_%(second)s",
293+
timezone="utc",
294+
)
295+
# generates a date with no warnings
296+
eq_(script._generate_create_date(), mock.ANY)
297+
279298
def test_default_tz(self):
280299
self._test_tz(
281300
None,

0 commit comments

Comments
 (0)