Skip to content

Commit 8be1628

Browse files
author
hauntsaninja
committed
Fix assertion rewriting on Python 3.10
Fixes #8539 This seems to have been the result of https://bugs.python.org/issue43798
1 parent 9653a0e commit 8be1628

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Sankt Petersbug
277277
Segev Finer
278278
Serhii Mozghovyi
279279
Seth Junot
280+
Shantanu Jain
280281
Shubham Adep
281282
Simon Gomizelj
282283
Simon Kerr

changelog/8539.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed assertion rewriting on Python 3.10.

src/_pytest/assertion/rewrite.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,9 @@ def run(self, mod: ast.Module) -> None:
684684
if not mod.body:
685685
# Nothing to do.
686686
return
687+
687688
# Insert some special imports at the top of the module but after any
688689
# docstrings and __future__ imports.
689-
aliases = [
690-
ast.alias("builtins", "@py_builtins"),
691-
ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
692-
]
693690
doc = getattr(mod, "docstring", None)
694691
expect_docstring = doc is None
695692
if doc is not None and self.is_rewrite_disabled(doc):
@@ -721,6 +718,19 @@ def run(self, mod: ast.Module) -> None:
721718
lineno = item.decorator_list[0].lineno
722719
else:
723720
lineno = item.lineno
721+
if sys.version_info >= (3, 10):
722+
aliases = [
723+
ast.alias("builtins", "@py_builtins", lineno=lineno, col_offset=0),
724+
ast.alias(
725+
"_pytest.assertion.rewrite", "@pytest_ar",
726+
lineno=lineno, col_offset=0
727+
),
728+
]
729+
else:
730+
aliases = [
731+
ast.alias("builtins", "@py_builtins"),
732+
ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
733+
]
724734
imports = [
725735
ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases
726736
]

0 commit comments

Comments
 (0)