Skip to content

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

Closed

Description

@kristiker

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.

Activity

  1. The-Compiler commented on May 22, 2023

    @The-Compiler
    Member

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

    cc @aless10

  2. aless10 commented on May 22, 2023

    @aless10
    Contributor

    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.

  3. The-Compiler commented on May 22, 2023

    @The-Compiler
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: rewriterelated to the assertion rewrite mechanismtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions