Skip to content

Uninitialised check wrong after walrus in if #913

@ndmitchell

Description

@ndmitchell

UPDATE 2025/10/8: The example in the original issue report (preserved below) has been fixed, but Pyrefly still generates some related false positives.

Example 1:

import pathlib

def f(mod: str, stubs_path: pathlib.Path):
    _, *submods = mod.split(".")
    if (path := stubs_path.joinpath(*submods, "__init__.pyi")).is_file():
        return path
    assert submods, path  # false positive: `path` is uninitialized

sandbox

Example 2:

def check():
    if (y := 2) <= 1:
        return
    print(y)  # false positive: `y` is uninitialized

check()

sandbox

--- original issue report ---

Describe the Bug

from typing import *

def f(v):
    x: int
    if (x := v) and v:
        print(x) # E: `x` may be uninitialized

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoBUAUEQCYCmwUwAFAG4CUAXEVG1AB5OYoyvtIqNDlCYBeKIygBDFKUkt2SqAhCoYwhkSA&version=3.12

(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