Skip to content

[Python 3.12] Assertion rewriting triggers DeprecationWarning #10977

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

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