Pytest/Warning/How To/Resolve AST-related Deprecation Warnings on Python 3.13
Properties
- tags
- Diataxis/How To
Pytest AST-related Deprecation Warnings on Python 3.13 Pytest/v/7/3 and under
TL;DR
- Running pytest < 7.4 under Python 3.13 (or 3.12) triggers a flood of warnings like
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead- because pytest's assertion-rewriter still used the legacy
ast.Str/ast.NameConstantnodes. - Upgrade to pytest ≥ 7.4.0 (or any 8.x release) to eliminate the warnings; those versions switch to the new
ast.ConstantAPI. (Avoid ast deprecation warnings on Python 3.12)
What you see
- Example warning (truncated):
_pytest/assertion/rewrite.py:958: DeprecationWarning: ast.Str is deprecated… - Where it comes from: pytest's assertion-rewriting pass instruments every
assertin your code to produce richer failure messages. - If your project promotes warnings to errors (
-W error,python -X dev, orpytest --strict-config) the test session may even fail.
- Example warning (truncated):
Why it happens
- Python 3.8 unified several constant node classes (
ast.Str,ast.Num,ast.NameConstant, etc.) into a singleast.Constant. The old node types stayed around but became officially deprecated, and Python 3.13 now emits aDeprecationWarningeach time they are referenced. - pytest versions prior to 7.4 rewrote
assertstatements by constructing those legacy nodes, so merely importing pytest on Python 3.13 raises the warnings.
- Python 3.8 unified several constant node classes (
How to fix it (root-cause, not suppression)
1 Upgrade pytest
# Poetry poetry add --group dev "pytest>=7.4" # pip python -m pip install -U "pytest>=7.4"- 7.4.0 includes commit 9335a0b ("Avoid ast deprecation warnings on Python 3.12").
- This commit aliases the old names to
ast.Constantat runtime, eliminating the warnings. (Avoid ast deprecation warnings on Python 3.12) - Side-benefit: Newer pytest releases bring many bug-fixes and features; upgrading is the canonical solution.
2 Pinned-Python fallback (not recommended long-term)
3 Temporary warning filter (stop-gap only)
References
- Python docs – Deprecated AST node types (Pending removal in 3.14 section for
ast.Str/ast.NameConstant) - Assertion rewriting triggers DeprecationWarning (pytest issue #10977)
- pytest commit 9335a0b (part of 7.4.0) – fixes the problem. (Avoid ast deprecation warnings on Python 3.12)
- Python docs – Deprecated AST node types (Pending removal in 3.14 section for