Skip to content

[Python 3.12] Assertion rewriting triggers DeprecationWarning #10977

Closed

Description

@seifertm

What's the problem this feature will solve?

As of pytest 7.3.1, the classes ast.Str and ast.NameConstant are used in assertion rewriting. Their use has been deprecated in Python 3.8. These classes will emit a DeprecationWarning starting with Python 3.12.

When a user runs pytest 7.3.1 on Python 3.12and pytest collects a test with an assert statement, the test will show deprecation warnings.

Describe the solution you'd like

Pytest does not trigger a DeprecationWarning when running tests on Python 3.12.

Tested with pytest 7.3.1 on Python 3.12 (commit 0aeda29793):

test_anything.py

def test_a():
    assert True
$ pytest
===== test session starts =====
platform linux -- Python 3.12.0a7+, pytest-7.3.1, pluggy-1.0.0
rootdir: /tmp/test-cpython-next
collected 1 item                                                                                                                                                                                                                                                             

test_anything.py .                                                                                                                                                                                                                                                     [100%]

===== warnings summary =====
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    assertmsg = ast.Str("")

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:929
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:929: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation))

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:817
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:817: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    keys = [ast.Str(key) for key in current.keys()]

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941: DeprecationWarning: ast.NameConstant is deprecated and will be removed in Python 3.14; use ast.Constant instead
    clear = ast.Assign(variables, ast.NameConstant(None))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===== 1 passed, 6 warnings in 0.01s ====

Activity

  1. added a commit that references this issue on May 10, 2023
    9fb0fce
  2. added
    type: enhancementnew feature or API change, should be merged into features branch
    topic: rewriterelated to the assertion rewrite mechanism
    on May 15, 2023
  3. added 4 commits that reference this issue on May 23, 2023
    bdb453d
    b1ba5ff
    e248255
    6536067
  4. added a commit that references this issue on Jun 7, 2023
    9335a0b
  5. added a commit that references this issue on Jun 9, 2023
  6. added a commit that references this issue on Dec 3, 2023
  7. added 2 commits that reference this issue on Apr 18, 2026
    cfe2d8d
    6571b88
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

    topic: rewriterelated to the assertion rewrite mechanismtype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions