pytest-dev / pytest-dev/pytest

Pytest reevaluates walrus operations within assert statements

Open
#11,115 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite type: regression
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Ubuntu 22.04, pytest 7.3.2, python 3.10.11

Package Version


exceptiongroup 1.1.1
iniconfig 2.0.0
packaging 23.1
pip 23.1.2
pluggy 1.0.0
pytest 7.3.2
setuptools 67.8.0
tomli 2.0.1
wheel 0.38.4

Pytest 7.3.2 re-evaluates walrus operator statements if they appear in assert statements.

def test_walrus():
    assert (x := 2) == 2
    x  =  3
    assert x == 3, "This test fails because (x := 2) is reevaluated by pytest"
    
def test_walrus_no_assert():
    (x := 2) == 2
    x = 3
    assert x == 3, "This test passes because (x := 2) is not reevaluated"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied walrus-in-assert example under the reported pytest and Python versions, then compare it with the non-assert example. Trace pytest's assert handling to identify where the expression is reevaluated, add a regression test for the demonstrated behavior, and confirm both tests pass without changing the intended assertion result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.