Skip to content

unbound name when using walrus operator within a ternary expression #2382

@jakevdp

Description

@jakevdp

Short reproduction of the issue:

# test.py
def f(i):
  return a if (a := round(i)) - 1 else a + 1
print(f"result is {f(1.0)}") 

This executes correctly:

$ python test.py
result is 2

But pyrefly raises an error (using v0.52.0):

$ pyrefly check test.py
ERROR `a` is uninitialized [unbound-name]
 --> test.py:3:40
  |
3 |   return a if (a := round(i)) - 1 else a + 1
  |                                        ^
  |
 INFO 1 error

Sandbox Link

sandbox link

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothscoping-control-flowissues related to scoping and control flow

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions