File fix-assertion-rewriting-on-python3.10.patch of Package saltbundlepy-pytest
From 8be16280423eafbc3c604a81419f40a8c1681fba Mon Sep 17 00:00:00 2001
From: hauntsaninja <>
Date: Sun, 11 Apr 2021 15:38:32 -0700
Subject: [PATCH 1/3] Fix assertion rewriting on Python 3.10
Fixes https://github.com/pytest-dev/pytest/issues/8539
This seems to have been the result of https://bugs.python.org/issue43798
---
AUTHORS | 1 +
changelog/8539.bugfix.rst | 1 +
src/_pytest/assertion/rewrite.py | 18 ++++++++++++++----
3 files changed, 16 insertions(+), 4 deletions(-)
create mode 100644 changelog/8539.bugfix.rst
diff --git a/AUTHORS b/AUTHORS
--- a/AUTHORS
+++ b/AUTHORS
@@ -244,6 +244,7 @@
Segev Finer
Serhii Mozghovyi
Seth Junot
+Shantanu Jain
Simon Gomizelj
Skylar Downes
Srinivas Reddy Thatiparthy
diff --git a/changelog/8539.bugfix.rst b/changelog/8539.bugfix.rst
--- /dev/null
+++ b/changelog/8539.bugfix.rst
@@ -0,0 +1 @@
+Fixed assertion rewriting on Python 3.10.
diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py
--- a/src/_pytest/assertion/rewrite.py
+++ b/src/_pytest/assertion/rewrite.py
@@ -630,12 +630,9 @@
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("builtins", "@py_builtins"),
- ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
- ]
+
+ # We'll insert some special imports at the top of the module, but after any
+ # docstrings and __future__ imports, so first figure out where that is.
doc = getattr(mod, "docstring", None)
expect_docstring = doc is None
if doc is not None and self.is_rewrite_disabled(doc):
@@ -662,10 +659,27 @@
pos += 1
else:
lineno = item.lineno
+ # Now actually insert the special imports.
+ if sys.version_info >= (3, 10):
+ aliases = [
+ ast.alias("builtins", "@py_builtins", lineno=lineno, col_offset=0),
+ ast.alias(
+ "_pytest.assertion.rewrite",
+ "@pytest_ar",
+ lineno=lineno,
+ col_offset=0,
+ ),
+ ]
+ else:
+ aliases = [
+ ast.alias("builtins", "@py_builtins"),
+ ast.alias("_pytest.assertion.rewrite", "@pytest_ar"),
+ ]
imports = [
ast.Import([alias], lineno=lineno, col_offset=0) for alias in aliases
]
mod.body[pos:pos] = imports
+
# Collect asserts.
nodes = [mod] # type: List[ast.AST]
while nodes: