Skip to content

pytest_assertion_pass strange results #5510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nicoddemus opened this issue Jun 27, 2019 · 3 comments · Fixed by #5512
Closed

pytest_assertion_pass strange results #5510

nicoddemus opened this issue Jun 27, 2019 · 3 comments · Fixed by #5512
Labels
type: bug problem that needs to be addressed

Comments

@nicoddemus
Copy link
Member

(From #5509 (comment))

It seems this patch fixes the problem:

diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py
index 8810c156c..28091c115 100644
--- a/src/_pytest/assertion/rewrite.py
+++ b/src/_pytest/assertion/rewrite.py
@@ -750,7 +750,7 @@ class AssertionRewriter(ast.NodeVisitor):

             # Passed
             fmt_pass = self.helper("_format_explanation", msg)
-            orig = astor.to_source(assert_.test).rstrip("\n").lstrip("(").rstrip(")")
+            orig = astor.to_source(assert_.test).strip()
             hook_call_pass = ast.Expr(
                 self.helper(
                     "_call_assertion_pass",
item = <Function test>
lineno = 5
orig = f()
expl = 1
 +  where 1 = f()

The test we have for it fails, but just because it expects the parenthesis to be stripped:

E           and: '>       raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))'
E           and: 'E       Exception: Assertion Passed: (a + b == c + d) (1 + 2) == (3 + 0) at line 7'
...
E       remains unmatched: '*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*'

So I think we should avoid trying to strip the results ourselves. I will open a PR.

cc @Sup3rGeo @asottile

@nicoddemus nicoddemus added the type: bug problem that needs to be addressed label Jun 27, 2019
@asottile
Copy link
Member

Even that doesn't work well though:

def test():
    assert 1. + 2.
(Pdb) args
item = <Function test>
lineno = 5
orig = '1.0 + 2.0'
expl = '(1.0 + 2.0)'

we shouldn't be roundtripping through the ast for this -- I'm working on a fix which uses the tokenization and not astor

@nicoddemus
Copy link
Member Author

Oh OK, thanks! I was tidying up the tests, I will push a PR anyway to see if you want to borrow something from there. 👍

@nicoddemus
Copy link
Member Author

#5511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants