Skip to content

Commit 0bc4bdc

Browse files
committed
refactor trying to clean the code and add comments where conditions on instances of walrus operator
1 parent 52f818d commit 0bc4bdc

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
assertstate_key = StashKey["AssertionState"]()
5454

55-
5655
# pytest caches rewritten pycs in pycache dirs
5756
PYTEST_TAG = f"{sys.implementation.cache_tag}-pytest-{version}"
5857
PYC_EXT = ".py" + (__debug__ and "c" or "o")
@@ -945,7 +944,8 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]:
945944

946945
def visit_NamedExpr(self, name: namedExpr) -> Tuple[namedExpr, str]:
947946
# This method handles the 'walrus operator' repr of the target
948-
# name if it's a local variable or _should_repr_global_name() thinks it's acceptable.
947+
# name if it's a local variable or _should_repr_global_name()
948+
# thinks it's acceptable.
949949
locs = ast.Call(self.builtin("locals"), [], [])
950950
target_id = name.target.id # type: ignore[attr-defined]
951951
inlocs = ast.Compare(ast.Str(target_id), [ast.In()], [locs])
@@ -981,25 +981,23 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> Tuple[ast.Name, str]:
981981
# cond is set in a prior loop iteration below
982982
self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa
983983
self.expl_stmts = fail_inner
984-
if isinstance(v, ast.Compare):
985-
if isinstance(v.left, namedExpr) and (
984+
# Check if the left operand is a namedExpr and the value has already been visited
985+
if (
986+
isinstance(v, ast.Compare)
987+
and isinstance(v.left, namedExpr)
988+
and (
986989
v.left.target.id
987990
in [
988991
ast_expr.id
989992
for ast_expr in boolop.values[:i]
990993
if hasattr(ast_expr, "id")
991994
]
992995
or v.left.target.id == pytest_temp
993-
):
994-
pytest_temp = f"pytest_{v.left.target.id}_temp"
995-
self.variables_overwrite[v.left.target.id] = pytest_temp
996-
v.left.target.id = pytest_temp
997-
998-
elif isinstance(v.left, ast.Name) and (
999-
pytest_temp is not None
1000-
and v.left.id == pytest_temp.lstrip("pytest_").rstrip("_temp")
1001-
):
1002-
v.left.id = pytest_temp
996+
)
997+
):
998+
pytest_temp = util.compose_temp_variable(v.left.target.id)
999+
self.variables_overwrite[v.left.target.id] = pytest_temp
1000+
v.left.target.id = pytest_temp
10031001
self.push_format_context()
10041002
res, expl = self.visit(v)
10051003
body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))
@@ -1075,6 +1073,7 @@ def visit_Attribute(self, attr: ast.Attribute) -> Tuple[ast.Name, str]:
10751073

10761074
def visit_Compare(self, comp: ast.Compare) -> Tuple[ast.expr, str]:
10771075
self.push_format_context()
1076+
# We first check if we have overwritten a variable in the previous assert
10781077
if isinstance(comp.left, ast.Name) and comp.left.id in self.variables_overwrite:
10791078
comp.left.id = self.variables_overwrite[comp.left.id]
10801079
left_res, left_expl = self.visit(comp.left)
@@ -1093,7 +1092,7 @@ def visit_Compare(self, comp: ast.Compare) -> Tuple[ast.expr, str]:
10931092
and isinstance(left_res, ast.Name)
10941093
and next_operand.target.id == left_res.id
10951094
):
1096-
next_operand.target.id = f"pytest_{left_res.id}_temp"
1095+
next_operand.target.id = util.compose_temp_variable(left_res.id)
10971096
self.variables_overwrite[left_res.id] = next_operand.target.id
10981097
next_res, next_expl = self.visit(next_operand)
10991098
if isinstance(next_operand, (ast.Compare, ast.BoolOp)):

src/_pytest/assertion/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,7 @@ def running_on_ci() -> bool:
520520
"""Check if we're currently running on a CI system."""
521521
env_vars = ["CI", "BUILD_NUMBER"]
522522
return any(var in os.environ for var in env_vars)
523+
524+
525+
def compose_temp_variable(original_variable: str) -> str:
526+
return f"pytest_{original_variable}_temp"

0 commit comments

Comments
 (0)