Skip to content

Commit

Permalink
Fix assertion rewriting on Python 3.10
Browse files Browse the repository at this point in the history
Fixes pytest-dev#8539

This seems to have been the result of https://bugs.python.org/issue43798
  • Loading branch information
hauntsaninja authored and Taiju Yamada committed Aug 26, 2023
1 parent da7ca9e commit fef682a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -222,6 +222,7 @@ Samuele Pedroni
Sankt Petersbug
Segev Finer
Serhii Mozghovyi
Shantanu Jain
Simon Gomizelj
Skylar Downes
Srinivas Reddy Thatiparthy
Expand Down
1 change: 1 addition & 0 deletions changelog/8539.bugfix.rst
@@ -0,0 +1 @@
Fixed assertion rewriting on Python 3.10.
18 changes: 14 additions & 4 deletions src/_pytest/assertion/rewrite.py
Expand Up @@ -652,12 +652,9 @@ def run(self, mod):
if not mod.body:
# Nothing to do.
return

# Insert some special imports at the top of the module but after any
# docstrings and __future__ imports.
aliases = [
ast.alias(six.moves.builtins.__name__, "@py_builtins"),
ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
]
doc = getattr(mod, "docstring", None)
expect_docstring = doc is None
if doc is not None and self.is_rewrite_disabled(doc):
Expand All @@ -684,6 +681,19 @@ def run(self, mod):
pos += 1
else:
lineno = item.lineno
if sys.version_info >= (3, 10):
aliases = [
ast.alias(six.moves.builtins.__name__, "@py_builtins", lineno=lineno, col_offset=0),
ast.alias(
"_pytest.assertion.rewrite", "@pytest_ar",
lineno=lineno, col_offset=0
),
]
else:
aliases = [
ast.alias(six.moves.builtins.__name__, "@py_builtins"),
ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
]
imports = [
ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases
]
Expand Down

0 comments on commit fef682a

Please sign in to comment.