Skip to content

[ty] Treat assignments in string annotation metadata conservatively - #27913

Closed
charliermarsh wants to merge 2 commits into
mainfrom
charlie/fix-string-annotation-walrus
Closed

charliermarsh wants to merge 2 commits into
mainfrom
charlie/fix-string-annotation-walrus

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Stacked on #27914.

Assignment expressions are valid in Annotated metadata, but parsed string annotations do not have a use-def model. Inferring only the assigned value avoids a panic while still getting later references wrong:

from typing import Annotated

value: "Annotated[int, (metadata := 0), metadata]"

We now treat metadata as opaque when the parsed annotation contains an assignment expression. The policy applies to the whole annotation, including conditional assignments, lambda defaults, and sibling Annotated expressions, so we do not pretend to model bindings or their control-flow effects. We still check the annotated type, and metadata without assignments retains its existing diagnostics and inference.

This removes the broad missing-definition fallback from ordinary assignment-expression inference. Full inference of this metadata can be added when parsed annotations have a proper use-def model.

All 485 semantic mdtests and 339 selected crate unit tests pass. Clippy and the standalone panic and binding-regression reproducers also pass.

@charliermarsh charliermarsh added the ty The ty type checker label Aug 19, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.68%. The percentage of expected errors that received a diagnostic held steady at 93.44%. The number of fully passing files held steady at 109/136.

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh
charliermarsh force-pushed the charlie/fix-string-annotation-walrus branch from 4cd6857 to cdc2d1e Compare August 20, 2026 00:32
@charliermarsh charliermarsh changed the title [ty] Handle assignment expressions in string annotations [ty] Treat assignments in string annotation metadata conservatively Aug 20, 2026
@charliermarsh
charliermarsh changed the base branch from main to charlie/fix-string-annotation-lambda-defaults August 20, 2026 00:32
Base automatically changed from charlie/fix-string-annotation-lambda-defaults to main August 20, 2026 01:59
@charliermarsh
charliermarsh force-pushed the charlie/fix-string-annotation-walrus branch from cdc2d1e to bba48ab Compare August 20, 2026 02:16
charliermarsh added a commit that referenced this pull request Aug 20, 2026
## Summary

Follow-up to #27882.

Previously, the non-generic-class and `type[...]` fallback paths could
evaluate invalid subscript arguments inside string annotations, reaching
assignment expressions that are absent from the semantic index:

```python
value: "int[(other := 0)]"
another: "type[(other := 0)]"
```

We now skip expression inference in these error-recovery paths when the
arguments come from a string annotation. Non-generic classes retain
their existing `invalid-type-form` diagnostic, unsupported `type[...]`
forms retain their existing fallback type, and evaluated annotations
retain their runtime diagnostics. We do not add support for additional
`type[...]` forms.

This fix is independent of #27914 and #27913.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant