Skip to content

Narrow a walrus target in a later operand of and/or - #5691

Open
TimonGaertner wants to merge 1 commit into
pyrefly-org:mainfrom
TimonGaertner:fix-walrus-narrowing-in-and
Open

TimonGaertner wants to merge 1 commit into
pyrefly-org:mainfrom
TimonGaertner:fix-walrus-narrowing-in-and

Conversation

@TimonGaertner

@TimonGaertner TimonGaertner commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A walrus in the first operand of a boolean test already narrows its target, but one in a later operand does not:

def f(u: str | os.PathLike[str]) -> str:
    if u and (u := str(u)):
        return u  # before: `u` is still `str | PathLike[str]`; mypy says `str`
    return ""

The binder forks the flow for each operand after the first, because later operands only run when the earlier ones pass, and merges the branches again after the operation. In the branch for (u := str(u)) the name is bound to str, but the merge joins it with the short-circuit branch, where u still has its declared type. The narrowing for the if body then applies IsTruthy to that merged type and keeps the union.

This adds a narrow op WalrusValue(idx) that narrows a name to the type of a given walrus binding. When NarrowOps::from_expr sees a walrus with a name target, it records And([WalrusValue(binding), IsTruthy]) for the name. Where the test is known to have passed, the name gets the assigned type back. A walrus assigns its value whether or not the test passes, so negation keeps the fact and only negates the conditions: not (u := str(u)) narrows to the falsy part of str, and if not u or not (u := str(u)): return leaves u: str after the if. Outside a test (x = u and (u := str(u))) nothing changes, because no narrowing is applied there and the merged flow is right.

The binding is looked up with promised_idx, so a target whose definition has not been bound (which should not happen, since the test is bound before its narrowing is computed) simply falls back to the previous behaviour.

Fixes #5581

  • I am a human, and I fully understand the code I am submitting.

Test Plan

New test test_walrus_in_later_bool_op_operand_narrows in flow_branching.rs: the issue's example, a walrus after an unrelated first operand (with the else branch keeping the union), the negated or form, while, a ternary, a plain assignment that must keep the union, and a third operand that uses the narrowed value.

cargo test -p pyrefly --lib passes (the test_boolean_or_result_falsy_narrow case in operators.rs caught the first version, which negated the walrus fact along with the conditions). The conformance suite is unchanged. The repro from the issue reports str with the fixed build.

A walrus in a later operand of a boolean operation runs only when the
earlier operands pass, so the binder forks the flow for it and merges
the branches again after the operation. In `if u and (u := str(u)):`
the body therefore narrowed the merged `str | PathLike[str]` by
truthiness and kept the union, while mypy reports `str`.

Record the assignment as part of the test's narrowing: a new
`WalrusValue` op narrows the name to the type of the walrus binding, and
`from_expr` emits `And([WalrusValue, IsTruthy])` for a walrus with a name
target. A walrus assigns its value whether or not the test passes, so
negation keeps the fact and only negates the condition.
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

✅ This PR meets all requirements from our pull request checklist.

Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour.

@meta-codesync

meta-codesync Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D133022249. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

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

============================================================
SUMMARY
============================================================
Total: +16 new errors, -11 fixed errors
By preset: +0/-5 (default), +1/-5 (strict)

Projects with changes (7):
  openlibrary: +3 -2
  rotki: +7 -3
  static-frame: +0 -1
  Tanjun: +0 -4
  sphinx: +3 -0
  core: +3 -0
  scrapy: +0 -1
============================================================

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

openlibrary (https://github.com/internetarchive/openlibrary)
- ERROR openlibrary/core/fulltext.py:348:29-32: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
+ ERROR openlibrary/core/fulltext.py:348:29-32: The type of this argument is unknown because it comes from an error [unknown-from-error]
+ ERROR openlibrary/core/fulltext.py:348:45-49: The type of `hit` is unknown because it comes from an error [unknown-from-error]
- ERROR openlibrary/core/fulltext.py:355:36-40: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
+ ERROR openlibrary/core/fulltext.py:355:36-40: The type of this argument is unknown because it comes from an error [unknown-from-error]

rotki (https://github.com/rotki/rotki)
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:36-94: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:41-61: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
- ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:41-69: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:41-69: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
- ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:62-67: The type of attribute `items` is an explicit `Any` [explicit-any-use]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:62-67: The type of attribute `items` is partially unknown; it contains an implicit `Any` [partially-unknown-type]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:280:67-69: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:281:25-39: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]
- ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:283:38-58: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR rotkehlchen/chain/ethereum/modules/eth2/eth2.py:283:38-58: The type of this argument is partially unknown; it contains an implicit `Any` [partially-unknown-type]

static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/util.py:732:27-36: Implicit conversion of `Sequence[bool] | bool` to `bool` is not allowed [implicit-bool]

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- ERROR tanjun/_internal/cache.py:74:16-23: Returned type `PartialChannel | PermissibleGuildChannel` is not assignable to declared return type `PermissibleGuildChannel` [bad-return]
- ERROR tanjun/_internal/cache.py:82:24-31: Returned type `PartialChannel | PermissibleGuildChannel` is not assignable to declared return type `PermissibleGuildChannel` [bad-return]
- ERROR tanjun/parsing.py:585:75-80: Argument `EllipsisType | str` is not assignable to parameter `value` with type `str` in function `Parameter.convert` [bad-argument-type]
- ERROR tanjun/parsing.py:588:92-97: Argument `str | None` is not assignable to parameter `value` with type `str` in function `Parameter.convert` [bad-argument-type]

sphinx (https://github.com/sphinx-doc/sphinx)
+ ERROR sphinx/ext/autodoc/_dynamic/_docstrings.py:264:47-56: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR sphinx/ext/autodoc/_legacy_class_based/_documenters.py:2493:51-60: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR sphinx/ext/autodoc/_legacy_class_based/_documenters.py:2493:63-99: Unused `# pyrefly: ignore` comment for code(s): bad-argument-type [unused-ignore]

core (https://github.com/home-assistant/core)
+ ERROR homeassistant/components/homekit/__init__.py:1095:13-39: The type of this argument is an explicit `Any` [explicit-any-use]
+ ERROR homeassistant/components/homekit/__init__.py:1095:29-39: The type of attribute `pairing_qr` is an explicit `Any` [explicit-any-use]
+ ERROR homeassistant/components/lg_thinq/climate.py:369:40-56: The type of this argument is an explicit `Any` [explicit-any-use]

scrapy (https://github.com/scrapy/scrapy)
- ERROR scrapy/pipelines/files.py:683:58-67: Argument `PathLike[str] | str` is not assignable to parameter `uri` with type `str` in function `FilesPipeline._get_store` [bad-argument-type]

This branch has not been deployed

No deployments
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.

Walrus in the right operand of and does not narrow the assigned name

1 participant