Skip to content

Replace utcnow() which is deprecated on Python 3.13 #1643

Description

@jenstroeger

Describe the bug

This use of utcnow()

create_date = (
datetime.datetime.utcnow()
.replace(tzinfo=datetime.timezone.utc)
.astimezone(tzinfo)
)
fails Alembic:

Traceback (most recent call last):
  File "/path/to/.venv/bin/alembic", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/config.py", line 636, in main
    CommandLine(prog=prog).main(argv=argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/config.py", line 626, in main
    self.run_cmd(cfg, options)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/config.py", line 603, in run_cmd
    fn(
    ~~^
        config,
        ^^^^^^^
        *[getattr(options, k, None) for k in positional],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        **{k: getattr(options, k, None) for k in kwarg},
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/command.py", line 243, in revision
    scripts = [script for script in revision_context.generate_scripts()]
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/autogenerate/api.py", line 650, in generate_scripts
    yield self._to_script(generated_revision)
          ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/autogenerate/api.py", line 555, in _to_script
    return self.script_directory.generate_revision(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        migration_script.rev_id,
        ^^^^^^^^^^^^^^^^^^^^^^^^
    ...<7 lines>...
        **template_args,
        ^^^^^^^^^^^^^^^^
    )
    ^
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/script/base.py", line 693, in generate_revision
    create_date = self._generate_create_date()
  File "/path/to/.venv/lib/python3.13/site-packages/alembic/script/base.py", line 632, in _generate_create_date
    datetime.datetime.utcnow()
    ~~~~~~~~~~~~~~~~~~~~~~~~^^
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).

Expected behavior

Alembic should succeed.

To Reproduce

I ran

alembic -c test.ini revision --autogenerate -m "Add column"

in a local Python 3.13 environment.

Error

See above.

Versions.

  • OS: macOS 13.7.5 (22H527)
  • Python: Python 3.13.2 (main, Feb 8 2025, 23:18:32) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
  • Alembic: 1.15.2
  • SQLAlchemy: 2.0.39
  • Database: PG 14.17
  • DBAPI: psycopg-binary 3.2.6

Additional context

I’ve not modified a warnings filter, so I’m a little surprised that the deprecation warning fails Alembic.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpy3k

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions