← All challenges
easypytest-dev/pytest · v7.0

Error message prints extra code line when using assert in python3.9

Try this bug with AI

Fix it in the original repo with an AI agent and get feedback on how you worked.

2 free rounds each week

Round mode
  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to135 XP
Sign up or sign in to start

Read the sample report

From a simulated Django round

You changed code before reproducing the bug.

Run the failing test before the first edit, so you know the failure you are fixing and can tell when it is gone.

Problem statement

<!--
Thanks for submitting an issue!

Quick check-list while reporting bugs:
-->

  • [x] a detailed description of the bug or problem you are having
  • [x] output of pip list from the virtual environment you are using
  • [x] pytest and operating system versions
  • [ ] minimal example if possible

Description

I have a test like this:

from pytest import fixture


def t(foo):
    return foo


@fixture
def foo():
    return 1


def test_right_statement(foo):
    assert foo == (3 + 2) * (6 + 9)

    @t
    def inner():
        return 2

    assert 2 == inner


@t
def outer():
    return 2

The test "test_right_statement" fails at the first assertion,but print extra code (the "t" decorator) in error details, like this:

 ============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- 
cachedir: .pytest_cache
rootdir: 
plugins: allure-pytest-2.9.45
collecting ... collected 1 item

test_statement.py::test_right_statement FAILED                           [100%]

================================== FAILURES ===================================
____________________________ test_right_statement _____________________________

foo = 1

    def test_right_statement(foo):
>       assert foo == (3 + 2) * (6 + 9)
    
        @t
E       assert 1 == 75
E         +1
E         -75

test_statement.py:14: AssertionError
=========================== short test summary info ===========================
FAILED test_statement.py::test_right_statement - assert 1 == 75
============================== 1 failed in 0.12s ==============================

And the same thing did not happen when using python3.7.10:

============================= test session starts =============================
platform win32 -- Python 3.7.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- 
cachedir: .pytest_cache
rootdir: 
collecting ... collected 1 item

test_statement.py::test_right_statement FAILED                           [100%]

================================== FAILURES ===================================
____________________________ test_right_statement _____________________________

foo = 1

    def test_right_statement(foo):
>       assert foo == (3 + 2) * (6 + 9)
E       assert 1 == 75
E         +1
E         -75

test_statement.py:14: AssertionError
=========================== short test summary info ===========================
FAILED test_statement.py::test_right_statement - assert 1 == 75
============================== 1 failed in 0.03s ==============================

Is there some problems when calculate the statement lineno?

pip list

$ pip list
Package            Version
------------------ -------
atomicwrites       1.4.0
attrs              21.2.0
colorama           0.4.4
importlib-metadata 4.8.2
iniconfig          1.1.1
packaging          21.3
pip                21.3.1
pluggy             1.0.0
py                 1.11.0
pyparsing          3.0.6
pytest             6.2.5
setuptools         59.4.0
toml               0.10.2
typing_extensions  4.0.0
zipp               3.6.0

pytest and operating system versions

pytest 6.2.5
Windows 10
Seems to happen in python 3.9,not 3.7

The environment starts at commit e2ee3144ed6e (pytest-dev/pytest 7.0), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.