Skip to content

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

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

Open
jenstroeger opened this issue Apr 10, 2025 · 1 comment · May be fixed by #1644
Open

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

jenstroeger opened this issue Apr 10, 2025 · 1 comment · May be fixed by #1644
Labels
bug Something isn't working py3k

Comments

@jenstroeger
Copy link

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.

@jenstroeger jenstroeger added the requires triage New issue that requires categorization label Apr 10, 2025
@CaselIT CaselIT added bug Something isn't working py3k and removed requires triage New issue that requires categorization labels Apr 10, 2025
@CaselIT
Copy link
Member

CaselIT commented Apr 10, 2025

Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working py3k
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants