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

Method pop_format_context

src/_pytest/assertion/rewrite.py:819–837  ·  view source on GitHub ↗

Format the %-formatted string with current format context. The expl_expr should be an str ast.expr instance constructed from the %-placeholders created by .explanation_param(). This will add the required code to format said string to .expl_stmts and return the ast.N

(self, expl_expr: ast.expr)

Source from the content-addressed store, hash-verified

817 self.stack.append(self.explanation_specifiers)
818
819 def pop_format_context(self, expl_expr: ast.expr) -> ast.Name:
820 """Format the %-formatted string with current format context.
821
822 The expl_expr should be an str ast.expr instance constructed from
823 the %-placeholders created by .explanation_param(). This will
824 add the required code to format said string to .expl_stmts and
825 return the ast.Name instance of the formatted string.
826 """
827 current = self.stack.pop()
828 if self.stack:
829 self.explanation_specifiers = self.stack[-1]
830 keys: list[ast.expr | None] = [ast.Constant(key) for key in current.keys()]
831 format_dict = ast.Dict(keys, list(current.values()))
832 form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
833 name = "@py_format" + str(next(self.variable_counter))
834 if self.enable_assertion_pass_hook:
835 self.format_variables.append(name)
836 self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
837 return ast.Name(name, ast.Load())
838
839 def generic_visit(self, node: ast.AST) -> tuple[ast.Name, str]:
840 """Handle expressions we don't have custom code for."""

Callers 3

visit_AssertMethod · 0.95
visit_BoolOpMethod · 0.95
visit_CompareMethod · 0.95

Calls 2

popMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected