MCPcopy
hub / github.com/pytest-dev/pytest / visit_BoolOp

Method visit_BoolOp

src/_pytest/assertion/rewrite.py:988–1032  ·  src/_pytest/assertion/rewrite.py::AssertionRewriter.visit_BoolOp
(self, boolop: ast.BoolOp)

Source from the content-addressed store, hash-verified

986 return name, self.explanation_param(expr)
987
988 def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
989 res_var = self.variable()
990 expl_list = self.assign(ast.List([], ast.Load()))
991 app = ast.Attribute(expl_list, class="st">"append", ast.Load())
992 is_or = int(isinstance(boolop.op, ast.Or))
993 body = save = self.statements
994 fail_save = self.expl_stmts
995 levels = len(boolop.values) - 1
996 self.push_format_context()
997 class="cm"># Process each operand, short-circuiting if needed.
998 for i, v in enumerate(boolop.values):
999 if i:
1000 fail_inner: list[ast.stmt] = []
1001 class="cm"># cond is set in a prior loop iteration below
1002 self.expl_stmts.append(ast.If(cond, fail_inner, [])) class="cm"># noqa: F821
1003 self.expl_stmts = fail_inner
1004 match v:
1005 class="cm"># Check if the left operand is an ast.NamedExpr and the value has already been visited
1006 case ast.Compare(
1007 left=ast.NamedExpr(target=ast.Name(id=target_id))
1008 ) if target_id in [
1009 e.id for e in boolop.values[:i] if hasattr(e, class="st">"id")
1010 ]:
1011 pytest_temp = self.variable()
1012 self.variables_overwrite[self.scope][target_id] = v.left class="cm"># type:ignore[assignment]
1013 class="cm"># mypyclass="st">'s false positive, we're checking that the class="st">'target' attribute exists.
1014 v.left.target.id = pytest_temp class="cm"># type:ignore[attr-defined]
1015 self.push_format_context()
1016 res, expl = self.visit(v)
1017 body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))
1018 expl_format = self.pop_format_context(ast.Constant(expl))
1019 call = ast.Call(app, [expl_format], [])
1020 self.expl_stmts.append(ast.Expr(call))
1021 if i < levels:
1022 cond: ast.expr = res
1023 if is_or:
1024 cond = ast.UnaryOp(ast.Not(), cond)
1025 inner: list[ast.stmt] = []
1026 self.statements.append(ast.If(cond, inner, []))
1027 self.statements = body = inner
1028 self.statements = save
1029 self.expl_stmts = fail_save
1030 expl_template = self.helper(class="st">"_format_boolop", expl_list, ast.Constant(is_or))
1031 expl = self.pop_format_context(expl_template)
1032 return ast.Name(res_var, ast.Load()), self.explanation_param(expl)
1033
1034 def visit_UnaryOp(self, unary: ast.UnaryOp) -> tuple[ast.Name, str]:
1035 pattern = UNARY_MAP[unary.op.__class__]

Callers

nothing calls this directly

Calls 9

variableMethod · 0.95
assignMethod · 0.95
push_format_contextMethod · 0.95
pop_format_contextMethod · 0.95
helperMethod · 0.95
explanation_paramMethod · 0.95
intClass · 0.85
appendMethod · 0.80
visitMethod · 0.80

Tested by

no test coverage detected