From ed1d1b1bba602fca3058a8eacfcd049589d75926 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 18 Nov 2019 20:26:31 +0100 Subject: [PATCH] visit_Assert: minor cleanup - import format_explanation as-is - move setting of `negation` out of branches --- src/_pytest/assertion/rewrite.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 2f9ca6de0b3..f05d4cbcfb5 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -22,9 +22,7 @@ from _pytest._io.saferepr import saferepr from _pytest._version import version from _pytest.assertion import util -from _pytest.assertion.util import ( # noqa: F401 - format_explanation as _format_explanation, -) +from _pytest.assertion.util import format_explanation # noqa: F401 from _pytest.compat import fspath from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import Path @@ -807,8 +805,9 @@ def visit_Assert(self, assert_): ) ) + negation = ast.UnaryOp(ast.Not(), top_condition) + if self.enable_assertion_pass_hook: # Experimental pytest_assertion_pass hook - negation = ast.UnaryOp(ast.Not(), top_condition) msg = self.pop_format_context(ast.Str(explanation)) # Failed @@ -821,7 +820,7 @@ def visit_Assert(self, assert_): err_explanation = ast.BinOp(ast.Str(gluestr), ast.Add(), msg) err_msg = ast.BinOp(assertmsg, ast.Add(), err_explanation) err_name = ast.Name("AssertionError", ast.Load()) - fmt = self.helper("_format_explanation", err_msg) + fmt = self.helper("format_explanation", err_msg) exc = ast.Call(err_name, [fmt], []) raise_ = ast.Raise(exc, None) statements_fail = [] @@ -829,7 +828,7 @@ def visit_Assert(self, assert_): statements_fail.append(raise_) # Passed - fmt_pass = self.helper("_format_explanation", msg) + fmt_pass = self.helper("format_explanation", msg) orig = self._assert_expr_to_lineno()[assert_.lineno] hook_call_pass = ast.Expr( self.helper( @@ -860,7 +859,6 @@ def visit_Assert(self, assert_): else: # Original assertion rewriting # Create failure message. body = self.expl_stmts - negation = ast.UnaryOp(ast.Not(), top_condition) self.statements.append(ast.If(negation, body, [])) if assert_.msg: assertmsg = self.helper("_format_assertmsg", assert_.msg) @@ -870,7 +868,7 @@ def visit_Assert(self, assert_): explanation = "assert " + explanation template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation)) msg = self.pop_format_context(template) - fmt = self.helper("_format_explanation", msg) + fmt = self.helper("format_explanation", msg) err_name = ast.Name("AssertionError", ast.Load()) exc = ast.Call(err_name, [fmt], []) raise_ = ast.Raise(exc, None)