missing evaluated value in report when asserting a boolean attribute (using --assert=rewrite) #1503

Closed
bagerard opened this Issue Apr 4, 2016 · 2 comments

Projects

None yet

3 participants

@bagerard
bagerard commented Apr 4, 2016

Hi there,

First of all, thank you for the amazing work you've done on pytest! It is really cool!
When asserting a boolean attribute of an object, the actual value isn't printed in the output as it would be with other types (int, str, etc). I'm using latest pytest 2.9.1

def test_a():
    class AA():
        pass
    a = AA()
    a.t = False
    assert a.t is True

Provides the following output (using with ---assert=rewrite):

...

  assert a.t is True

E assert <test.AA instance at 0x7fb9399169e0>.t is True

And if run with ---assert=reinterp:

...

  assert a.t is True

E assert False is True
E + where False = <test.AA instance at 0x7f24de4740e0>.t

Thanks

@tomviner
Contributor

This is caused by the _collapse_false function, which aims to trim away trivial explanations.

For example this test:

def test_false_value():
    x = 0
    assert bool(x)

Full explanation, with _collapse_false patched out:

    def test_false_value():
        x = 0
>       assert bool(x)
E       assert False
E        +  where False = bool(0)

Trimmed explanation, with _collapse_false back in place:

    def test_false_value():
        x = 0
>       assert bool(x)
E       assert bool(0)

I'm having trouble recreating an example where useful information is trimmed away.

@bagerard

Hi tomviner, Thanks for your reply. I wasn't aware of such behaviour and in my case I wasn't sure if my asserted value was a boolean or not. In fact if you take the following test:

def test_foo2():
    mo = Foo(a=False)
#>       assert mo.a
#E       assert <test.Foo instance at 0x7f28a5ff0dd0>.a

The output isn't telling me if mo.a is False or if it's anything else that evalues to False (0 for instance). It turns out that the output is different if it is 0 but without knowing this, you can't tell...
I understand that you want to trim away trivial explanations but personally I find it confusing for newcomers.
This ticket can be switched to "proposal", I suggest to trim away the _collapse_false method :)

@tomviner tomviner added a commit to tomviner/pytest that referenced this issue Jun 24, 2016
@tomviner tomviner Fixes #1503 no longer collapse false explanations a064c3f
@tomviner tomviner added a commit to tomviner/pytest that referenced this issue Jun 24, 2016
@tomviner tomviner Fixes #1503 no longer collapse false explanations 61c87dc
@tomviner tomviner added a commit to tomviner/pytest that referenced this issue Jun 24, 2016
@tomviner tomviner Fixes #1503 no longer collapse false explanations e30ec79
@tomviner tomviner added a commit to tomviner/pytest that referenced this issue Jun 24, 2016
@tomviner tomviner Fixes #1503 no longer collapse false explanations 77689eb
@nicoddemus nicoddemus closed this in #1654 Jun 24, 2016
This was referenced Aug 19, 2016
This was referenced Nov 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment