Repository navigation
Narrow a walrus target in a later operand of and/or - #5691
Open
TimonGaertner wants to merge 1 commit into
Open
TimonGaertner wants to merge 1 commit into
TimonGaertner wants to merge 1 commit into
Conversation
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.
|
✅ 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. |
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.) |
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A walrus in the first operand of a boolean test already narrows its target, but one in a later operand does not:
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 tostr, but the merge joins it with the short-circuit branch, whereustill has its declared type. The narrowing for theifbody then appliesIsTruthyto 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. WhenNarrowOps::from_exprsees a walrus with a name target, it recordsAnd([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 ofstr, andif not u or not (u := str(u)): returnleavesu: strafter theif. 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
Test Plan
New test
test_walrus_in_later_bool_op_operand_narrowsinflow_branching.rs: the issue's example, a walrus after an unrelated first operand (with theelsebranch keeping the union), the negatedorform,while, a ternary, a plain assignment that must keep the union, and a third operand that uses the narrowed value.cargo test -p pyrefly --libpasses (thetest_boolean_or_result_falsy_narrowcase inoperators.rscaught the first version, which negated the walrus fact along with the conditions). The conformance suite is unchanged. The repro from the issue reportsstrwith the fixed build.