From: Jens Troeger Date: Tue, 20 May 2025 21:01:45 +0000 (-0400) Subject: Replace use of utcnow() with now() for Python 3.13 compatibility, Fixes #1643 X-Git-Tag: rel_1_16_0~7^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=97edb1a192aa71a0bbb3961d45b0a696fccd1616;p=thirdparty%2Fsqlalchemy%2Falembic.git Replace use of utcnow() with now() for Python 3.13 compatibility, Fixes #1643 Fixed issue where use of deprecated ``utcnow()`` function would generate warnings. Has been replaced with ``now(UTC)``. Pull request courtesy Jens Tröger. Fixes: #1643 Closes: #1644 Pull-request: https://github.com/sqlalchemy/alembic/pull/1644 Pull-request-sha: 1ddb3caa4a8bc7487d070d39c24d5e5e609e0688 Change-Id: Ide0f830d33529c5d4d9540b5e043954f440f9016 --- diff --git a/alembic/script/base.py b/alembic/script/base.py index 34dd461f..8c5d763f 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -602,11 +602,10 @@ class ScriptDirectory: raise util.CommandError( "Can't locate timezone: %s" % self.timezone ) from None - create_date = ( - datetime.datetime.utcnow() - .replace(tzinfo=datetime.timezone.utc) - .astimezone(tzinfo) - ) + + create_date = datetime.datetime.now( + tz=datetime.timezone.utc + ).astimezone(tzinfo) else: create_date = datetime.datetime.now() return create_date diff --git a/docs/build/unreleased/1643.rst b/docs/build/unreleased/1643.rst new file mode 100644 index 00000000..1a9b9b2c --- /dev/null +++ b/docs/build/unreleased/1643.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, environment + :tickets: 1643 + + Fixed issue where use of deprecated ``utcnow()`` function would generate + warnings. Has been replaced with ``now(UTC)``. Pull request courtesy + Jens Tröger. diff --git a/tests/test_script_production.py b/tests/test_script_production.py index 60e1998f..10493579 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -235,7 +235,13 @@ class ScriptNamingTest(TestBase): with mock.patch( "alembic.script.base.datetime", mock.Mock( - datetime=mock.Mock(utcnow=lambda: given, now=lambda: given), + datetime=mock.Mock( + now=lambda tz=None: ( + given.replace(tzinfo=datetime.timezone.utc) + if timezone_arg + else given + ) + ), timezone=datetime.timezone, ), ): @@ -276,6 +282,19 @@ class ScriptNamingTest(TestBase): ), ) + def test_generates_a_date(self): + """test #1643""" + script = ScriptDirectory( + _get_staging_directory(), + file_template="%(rev)s_%(slug)s_" + "%(year)s_%(month)s_" + "%(day)s_%(hour)s_" + "%(minute)s_%(second)s", + timezone="utc", + ) + # generates a date with no warnings + eq_(script._generate_create_date(), mock.ANY) + def test_default_tz(self): self._test_tz( None,