Skip to content

[ty] Preserve string annotation context during inference - #27852

Closed
zaniebot wants to merge 1 commit into
astral-sh:mainfrom
zaniebot:agent/preserve-string-annotation-context
Closed

zaniebot wants to merge 1 commit into
astral-sh:mainfrom
zaniebot:agent/preserve-string-annotation-context

Conversation

@zaniebot

Copy link
Copy Markdown
Contributor

Invalid string annotations containing a lambda default, or a functional TypedDict with extra_items in a stub, can panic during name lookup. These paths overwrite InStringAnnotation while selecting how to resolve names, so inference expects annotation-only nodes to exist in the module's semantic index.

Preserve the string-annotation state while inferring lambda defaults, and let the existing annotation-expression helper choose the appropriate state for extra_items.

@astral-sh-bot astral-sh-bot Bot added the ty The ty type checker label Aug 18, 2026
@charliermarsh charliermarsh self-assigned this Aug 19, 2026
charliermarsh added a commit that referenced this pull request Aug 19, 2026
## Summary

Previously, we inferred an invalid subscript's value before checking
whether it was a valid type-expression operand. Inside a string
annotation, this could evaluate expressions that are absent from the
semantic index, panic on assignment expressions, or emit cascading
unresolved-reference diagnostics:

```python
value: "(lambda default=(name := int): None)[int]"
other: "TypedDict('T', {}, extra_items=missing)[int]"
```

We now infer invalid subscript operands only when annotations execute at
runtime. String annotations instead report the existing
`invalid-type-form` diagnostic without evaluating their invalid
children, matching existing error-recovery behavior in both Python and
stub files.

Supersedes #27852.
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.

2 participants