Skip to content

A walrus if test is never settled, so its unreachable suite is not reported #5499

Closed

Description

@kinto0

Describe the Bug

Describe the bug

Pyrefly reports a suite as unreachable when its if test is always false (or the else suite when the test is always true). It does not do this when the test is a walrus (x := value).

from typing import Literal

def falsy(value: Literal[False]) -> None:
    if value:
        print(1)  # This code is unreachable
    if x := value:
        print(2)  # no error

def truthy(value: Literal[True]) -> None:
    if x := value:
        print(3)
    else:
        print(4)  # no error

class AlwaysFalse:
    def __bool__(self) -> Literal[False]:
        return False

def user_defined(a: AlwaysFalse | None) -> None:
    if a and (b := a):
        print(5)  # This code is unreachable
    if b := (a and a):
        print(6)  # no error

Expected behavior

print(2), print(4) and print(6) are reported as unreachable.

Cause

declared_bool_value (pyrefly/lib/alt/solve.rs) computes the declared truth value of a branch test. It has no case for Expr::Named, so a walrus test falls to _ => None.

Proposed fix

A walrus has the value it assigns:

Expr::Named(named) => self.declared_bool_value(&named.value),

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Activity

  1. self-assigned this
    on Nov 3, 2026
  2. added theissue type on Nov 3, 2026
  3. rchen152 commented on Nov 3, 2026

    @rchen152
    Contributor

    Fixed by #5497

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions