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

Method visit_Compare

src/_pytest/assertion/rewrite.py:1101–1154  ·  src/_pytest/assertion/rewrite.py::AssertionRewriter.visit_Compare
(self, comp: ast.Compare)

Source from the content-addressed store, hash-verified

1099 return res, expl
1100
1101 def visit_Compare(self, comp: ast.Compare) -> tuple[ast.expr, str]:
1102 self.push_format_context()
1103 class="cm"># We first check if we have overwritten a variable in the previous assert
1104 match comp.left:
1105 case ast.Name(id=name_id) if name_id in self.variables_overwrite.get(
1106 self.scope, {}
1107 ):
1108 comp.left = self.variables_overwrite[self.scope][name_id] class="cm"># type: ignore[assignment]
1109 case ast.NamedExpr(target=ast.Name(id=target_id)):
1110 self.variables_overwrite[self.scope][target_id] = comp.left class="cm"># type: ignore[assignment]
1111 left_res, left_expl = self.visit(comp.left)
1112 if isinstance(comp.left, ast.Compare | ast.BoolOp):
1113 left_expl = fclass="st">"({left_expl})"
1114 res_variables = [self.variable() for i in range(len(comp.ops))]
1115 load_names: list[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables]
1116 store_names = [ast.Name(v, ast.Store()) for v in res_variables]
1117 it = zip(range(len(comp.ops)), comp.ops, comp.comparators, strict=True)
1118 expls: list[ast.expr] = []
1119 syms: list[ast.expr] = []
1120 results = [left_res]
1121 for i, op, next_operand in it:
1122 match (next_operand, left_res):
1123 case (
1124 ast.NamedExpr(target=ast.Name(id=target_id)),
1125 ast.Name(id=name_id),
1126 ) if target_id == name_id:
1127 next_operand.target.id = self.variable()
1128 self.variables_overwrite[self.scope][name_id] = next_operand class="cm"># type: ignore[assignment]
1129
1130 next_res, next_expl = self.visit(next_operand)
1131 if isinstance(next_operand, ast.Compare | ast.BoolOp):
1132 next_expl = fclass="st">"({next_expl})"
1133 results.append(next_res)
1134 sym = BINOP_MAP[op.__class__]
1135 syms.append(ast.Constant(sym))
1136 expl = fclass="st">"{left_expl} {sym} {next_expl}"
1137 expls.append(ast.Constant(expl))
1138 res_expr = ast.copy_location(ast.Compare(left_res, [op], [next_res]), comp)
1139 self.statements.append(ast.Assign([store_names[i]], res_expr))
1140 left_res, left_expl = next_res, next_expl
1141 class="cm"># Use pytest.assertion.util._reprcompare if that's available.
1142 expl_call = self.helper(
1143 class="st">"_call_reprcompare",
1144 ast.Tuple(syms, ast.Load()),
1145 ast.Tuple(load_names, ast.Load()),
1146 ast.Tuple(expls, ast.Load()),
1147 ast.Tuple(results, ast.Load()),
1148 )
1149 if len(comp.ops) > 1:
1150 res: ast.expr = ast.BoolOp(ast.And(), load_names)
1151 else:
1152 res = load_names[0]
1153
1154 return res, self.explanation_param(self.pop_format_context(expl_call))
1155
1156
1157def try_makedirs(cache_dir: Path) -> bool:

Callers

nothing calls this directly

Calls 8

push_format_contextMethod · 0.95
variableMethod · 0.95
helperMethod · 0.95
explanation_paramMethod · 0.95
pop_format_contextMethod · 0.95
visitMethod · 0.80
appendMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected