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

UnboundLocalError: cannot access local variable 'x' where it is not associated with a value #11028

Closed
kristiker opened this issue May 21, 2023 · 3 comments · Fixed by #11041
Closed
Labels
topic: rewrite related to the assertion rewrite mechanism type: bug problem that needs to be addressed

Comments

@kristiker
Copy link

There seems to be a regression in pytest version 7.3.x when a walrus operator is used in an assert line.
Code:

import json
import pytest

def test_json_encoder():
  assert (object:="foo") in json.dumps(object)

Fails the test with error:

UnboundLocalError: cannot access local variable 'object' where it is not associated with a value

in pytest version 7.3.x, whereas with pytest version 7.2.x it passes successfully. My Python version is 3.11.

Looks like it has to do with PR #10758.

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed topic: rewrite related to the assertion rewrite mechanism labels May 21, 2023
@The-Compiler
Copy link
Member

Yep, I could indeed bisect this to #10758 / 6e478b0.

cc @aless10

@aless10
Copy link
Contributor

aless10 commented May 22, 2023

Yep, I could indeed bisect this to #10758 / 6e478b0.

cc @aless10

Hi, I think this is definitely something I worked on. It seems that I did not cover this case. @The-Compiler I can work on this one by the end of the week it's ok with you.

@The-Compiler
Copy link
Member

Sure thing, feel free and thanks for taking a look!

nicoddemus pushed a commit that referenced this issue May 30, 2023
In #10758 we introduced the support for the use of the walrus operator in the test cases. There was a case which was not handled that caused a bug report #11028. This PR aims to fix the issue and also to improve how the walrus operator is handled in the AssertionRewriter class.

Closes #11028
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: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants