Skip to content
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

Assignment expression changes value of variable in unrelated test case #11239

Closed
cdce8p opened this issue Jul 22, 2023 · 0 comments · Fixed by #11414
Closed

Assignment expression changes value of variable in unrelated test case #11239

cdce8p opened this issue Jul 22, 2023 · 0 comments · Fixed by #11414
Labels
topic: rewrite related to the assertion rewrite mechanism type: regression indicates a problem that was introduced in a release which was working previously

Comments

@cdce8p
Copy link
Contributor

cdce8p commented Jul 22, 2023

The assert rewriting in test2 effects the result of test_1 just because both asserts are using the same identifier state.
Tested with pytest 7.3.2 and 7.4.0.

def test_1():
    state = {"x": 2}.get("x")
    assert state is not None

def test_2():
    db = {"x": 2}
    assert (state := db.get("x")) is not None
    def test_1():
        state = {"x": 2}.get("x")
>       assert state is not None
E       NameError: name 'db' is not defined

custom_tests/test_a.py:3: NameError

I bisected the issue to #11041. From what I can tell, the assertion rewriter doesn't track the current context. Thus state in test_1 is replaced with db.get("x") from test_2 even though they are in separate functions.

This behavior can be quite surprising as a user typically doesn't expect test cases with purely local variables and without side effects to influence one another.

As there is another regression (#11115) with this PR already, maybe it would be best to revert it?

/CC @aless10

@Zac-HD Zac-HD added type: regression indicates a problem that was introduced in a release which was working previously topic: rewrite related to the assertion rewrite mechanism labels Jul 24, 2023
nicoddemus pushed a commit to nicoddemus/pytest that referenced this issue Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: rewrite related to the assertion rewrite mechanism type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants