Skip to content

unexpected unbound-name error on second walrus conditional assignment for same variable #1397

@andrew-meter

Description

@andrew-meter

Describe the Bug

I have a code sequence that's doing walrus assignments of regex matches within if statements, using the same variable name for the walrus assignment, and then using the walrus-assigned variable within the body of the if statement.

Due to it being part of the if condition itself, once inside the if statement body it is my understanding, there is no way that the walrus-assigned variable could ever be unbound.

Yet, this is an outcome I see with a specific code sequence.

Interestingly I'm doing two walrus assignments, if I remove the first once, the error goes away.
And if I keep the first one but slightly tweak the if condition of the second one, it also goes away.

Full details are below.

Python code in question:

from re import compile

interface_re = compile(r"^foo")
ipv4_re = compile(r"bar$")
line = str()

if match := interface_re.match(line):  # unbound-name error gone if this is removed
    pass

# if match := ipv4_re.search(line):
if line and (match := ipv4_re.search(line)):  # unbound-name error also gone if above alternate if is used instead
    print(match)

Relevant software versions:

# pyrefly --version
pyrefly 0.39.1

# python3 --version
Python 3.14.0

Pyrefly error:

# pyrefly check walrus.py
 WARN PYTHONPATH environment variable is set to `/myapp`. Checks in other environments may not include these paths.
ERROR `match` may be uninitialized [unbound-name]
  --> walrus.py:12:11
   |
12 |     print(match)
   |           ^^^^^
   |
 INFO 1 error

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions