Repository navigation
[Python 3.12] Assertion rewriting triggers DeprecationWarning #10977
Copy link
Copy link
Closed
Labels
topic: rewriterelated to the assertion rewrite mechanismrelated to the assertion rewrite mechanismtype: enhancementnew feature or API change, should be merged into features branchnew feature or API change, should be merged into features branch
Description
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 ====
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
topic: rewriterelated to the assertion rewrite mechanismrelated to the assertion rewrite mechanismtype: enhancementnew feature or API change, should be merged into features branchnew feature or API change, should be merged into features branch
Activity
Avoid ast deprecation warnings on Python 3.12
Avoid ast deprecation warnings on Python 3.12
Avoid ast deprecation warnings on Python 3.12
Avoid ast deprecation warnings on Python 3.12
Avoid ast deprecation warnings on Python 3.12
test(pytest): don't error on internal deprecation warnings on 3.12
Ignore depr warnings inside pytest on 3.12
Ignore depr warnings inside pytest on 3.12
Ignore depr warnings inside pytest on 3.12 (#483)
Ignore depr warnings inside pytest on 3.12
Ignore depr warnings inside pytest on 3.12
Avoid ast deprecation warnings on Python 3.12
py312: Ignore pytest ast warnings
remove warnings filters fixed in pytest 7.4.0.
Add support for python 3.12 (#1390)
Avoid ast deprecation warnings on Python 3.12
Avoid ast deprecation warnings on Python 3.12