Skip to content

Settle a walrus test by the value it assigns - #5497

Closed
kinto0 wants to merge 2 commits into
meta:mainfrom
kinto0:export-D129211786
Closed

kinto0 wants to merge 2 commits into
meta:mainfrom
kinto0:export-D129211786

Conversation

@kinto0

@kinto0 kinto0 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
Stack context: pyrefly does not report a suite as unreachable when its if test is a walrus (x := value) whose value is always false or always true.

  • Problem: if x := value: with value: Literal[False] does not report its body as unreachable, but if value: does. The same happens with a class whose __bool__ returns Literal[False], for example if genre := (author and author.genre):. declared_bool_value has no case for Expr::Named, so a walrus test never gets a fixed value.
  • Fix: a walrus test takes the declared value of the expression it assigns.

Differential Revision: D129211786

fixes #5499

Summary:
Stack context: pyrefly does not report a suite as unreachable when its `if` test is a walrus (`x := value`) whose value is always false or always true.

- What: add a passing test, marked `bug`, that shows the missing "This code is unreachable" errors for walrus tests.
- Why: the next diff fixes this and updates the expectations.

Differential Revision: D129211785
Summary:
Stack context: pyrefly does not report a suite as unreachable when its `if` test is a walrus (`x := value`) whose value is always false or always true.

- Problem: `if x := value:` with `value: Literal[False]` does not report its body as unreachable, but `if value:` does. The same happens with a class whose `__bool__` returns `Literal[False]`, for example `if genre := (author and author.genre):`. `declared_bool_value` has no case for `Expr::Named`, so a walrus test never gets a fixed value.
- Fix: a walrus test takes the declared value of the expression it assigns.

Differential Revision: D129211786
@meta-cla meta-cla Bot added the cla signed label Nov 3, 2026
@meta-codesync

meta-codesync Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@kinto0 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D129211786.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

============================================================
SUMMARY
============================================================
Total: +3 new errors, -0 fixed errors
By preset: +3/-0 (default), +3/-0 (strict)

Projects with changes (2):
  pytest-autoprofile: +2 -0
  core: +1 -0
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

pytest-autoprofile (https://gitlab.com/TTsangSC/pytest-autoprofile)
+ ERROR tests/test-modules-and-tests/test_subprocess/tests/test_func.py:29:5-21: This code is unreachable [unreachable]
+ ERROR tests/test_subprocess.py:40:5-41:21: This code is unreachable [unreachable]

core (https://github.com/home-assistant/core)
+ ERROR homeassistant/components/tuya/climate.py:367:13-64: This code is unreachable [unreachable]

@meta-codesync meta-codesync Bot closed this in b44689a Nov 3, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 3, 2026
@meta-codesync

meta-codesync Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in b44689a.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant