Skip to content

Replace use of utcnow() with now() for Python 3.13 compatibility, Fixes #1643 #1644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions alembic/script/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ def _generate_create_date(self) -> datetime.datetime:
"Can't locate timezone: %s" % self.timezone
) from None
create_date = (
datetime.datetime.utcnow()
.replace(tzinfo=datetime.timezone.utc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test fails without this replace. i dont remember the reason for the replace, and it perhaps is local to the fact that the test is using mocks. not sure yet

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I'm just going to restore that replace, I know if I had more mental capacity right now I could change the mock on the other end but I just need to get this merged and I dont want to screw it up right now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm being dumb, but doesn't datetime.datetime.now(tzinfo) achieve the desired result (the current datetime in the prescribed timezone)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't get the mock in the tests to match it. i dont have time to understand why the mocks were set up the way they were and dont want to change them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/alembic/script/base.py b/alembic/script/base.py
index 972a9bd..552dc05 100644
--- a/alembic/script/base.py
+++ b/alembic/script/base.py
@@ -605,7 +605,6 @@ class ScriptDirectory:
 
             create_date = (
                 datetime.datetime.now(tz=datetime.timezone.utc)
-                .replace(tzinfo=datetime.timezone.utc)
                 .astimezone(tzinfo)
             )
         else:
diff --git a/tests/test_script_production.py b/tests/test_script_production.py
index 4263c1e..1049357 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(now=lambda tz=None: given),
+                datetime=mock.Mock(
+                    now=lambda tz=None: (
+                        given.replace(tzinfo=datetime.timezone.utc)
+                        if timezone_arg
+                        else given
+                    )
+                ),
                 timezone=datetime.timezone,
             ),
         ):

datetime.datetime.now(tz=datetime.timezone.utc)
.astimezone(tzinfo)
)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_script_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _test_tz(self, timezone_arg, given, expected):
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),
timezone=datetime.timezone,
),
):
Expand Down