[ty] Model walrus bindings from comprehensions#26466
Draft
charliermarsh wants to merge 34 commits into
Draft
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 94.47%. The percentage of expected errors that received a diagnostic held steady at 89.19%. The number of fully passing files held steady at 95/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unresolved-reference |
0 | 8 | 0 |
| Total | 0 | 8 | 0 |
Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.
Raw diff:
core (https://github.com/home-assistant/core)
- homeassistant/components/ista_ecotrend/sensor.py:277:47 error[unresolved-reference] Name `statistics_sum` used when not defined
- homeassistant/components/recorder/history/__init__.py:909:53 error[unresolved-reference] Name `prev_state` used when not defined
- homeassistant/components/recorder/history/__init__.py:925:49 error[unresolved-reference] Name `prev_state` used when not defined
mkosi (https://github.com/systemd/mkosi)
- mkosi/__init__.py:5249:80 error[unresolved-reference] Name `c` used when not defined
- mkosi/__init__.py:5254:79 error[unresolved-reference] Name `c` used when not defined
- mkosi/config.py:4776:41 error[unresolved-reference] Name `missing` used when not defined
steam.py (https://github.com/Gobot1234/steam.py)
- steam/_gc/client.py:56:90 error[unresolved-reference] Name `previous_state` used when not defined
xarray (https://github.com/pydata/xarray)
- xarray/core/extension_array.py:154:16 error[unresolved-reference] Name `ea_type` used when not defined0c783d5 to
40e790a
Compare
cdb9361 to
1ce0f53
Compare
charliermarsh
added a commit
that referenced
this pull request
Jun 30, 2026
## Summary This separates constructing and registering a `Definition` from recording it in the current scope's use-def state. `push_additional_definition` continues to create and associate AST-backed definitions, then delegates the existing binding, declaration, capture, and try-context bookkeeping to `record_definition`. This is a behavior-neutral refactor split from #26466 so synthesized definitions can reuse the recording path without mixing the mechanical extraction into the comprehension-walrus change. The ty core and semantic suites and repository hooks pass.
1ce0f53 to
d5e0dbf
Compare
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
PEP 572 specifies that an assignment-expression target inside a comprehension binds in the scope containing the outermost comprehension. Before this change, we left the target inside the comprehension scope, so it was unresolved when referenced afterward. For example, these two cases raised false positives on
main:This PR makes the walrus target visible in the containing scope while keeping its value expression in the comprehension scope, where it can reference iteration variables.
What's included vs. excluded
I went through several iterations of this change over the past few months, and each time they ballooned in scope. Here, I'm trying to intentionally limit the scope, so this section articulates cases that came up in Codex review but were explicitly rejected as out-of-scope.
We do not model the zero-iteration path or whether a generator expression has been consumed:
We also conservatively promote exported values because later iterations can change them. We do not try to retain single-iteration literal precision:
We do not compute a fixed point for a walrus whose value changes type across repeated iterations. We currently infer the exported type from the first modeled iteration:
(This requires treating the comprehension body as a loop and iterating its use-def state to a fixed point.)
We also defer all the IDE-specific navigation, references, rename, and unused-binding behavior to a separate PR (#26476).
Closes astral-sh/ty#162.