Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Expressions in assert statements have the wrong line number #3463

Description

@alexmojaki

In the below example, the normal Python traceback correctly points to line 5, where the actual division by zero happens. pytest points to line 4.

$ cat test.py
def test_thing():
    x = 0
    y = 3
    assert [1 / y,
            1 / x]

if __name__ == '__main__':
    test_thing()

$ python3.6 test.py
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    test_thing()
  File "test.py", line 5, in test_thing
    1 / x]
ZeroDivisionError: division by zero

$ python3.6 -m pytest test.py
================================================================================ test session starts ================================================================================
platform darwin -- Python 3.6.3, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /Users/alexhall, inifile:
collected 1 item                                                                                                                                                                    

test.py F                                                                                                                                                                     [100%]

===================================================================================== FAILURES ======================================================================================
____________________________________________________________________________________ test_thing _____________________________________________________________________________________

    def test_thing():
        x = 0
        y = 3
>       assert [1 / y,
                1 / x]
E       ZeroDivisionError: division by zero

test.py:4: ZeroDivisionError
============================================================================= 1 failed in 0.13 seconds ==============================================================================

Theoretically this certainly seems like a serious issue, but for the practical question of how often this is a problem, don't count me as a datapoint. I only noticed this problem while playing with some dark magic code that uses the line numbers of frames. The above example is completely contrived.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: reportingrelated to terminal output and user-facing messages and errorstopic: rewriterelated to the assertion rewrite mechanismtype: bugproblem that needs to be addressed

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions