Skip to content

[ty] Skip invalid subscript operands in string annotations - #27882

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/skip-invalid-string-annotation-operands
Aug 19, 2026
Merged

charliermarsh merged 1 commit into
mainfrom
charlie/skip-invalid-string-annotation-operands

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

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:

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.

@astral-sh-bot astral-sh-bot Bot added the ty The ty type checker label Aug 19, 2026
@charliermarsh charliermarsh added the bug An issue describing something that isn't working, or a PR that fixes a bug 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 marked this pull request as ready for review August 19, 2026 14:43
@charliermarsh
charliermarsh requested a review from a team as a code owner August 19, 2026 14:43
@astral-sh-bot
astral-sh-bot Bot requested a review from dcreager August 19, 2026 14:43
@charliermarsh
charliermarsh merged commit 4c03719 into main Aug 19, 2026
73 checks passed
@charliermarsh
charliermarsh deleted the charlie/skip-invalid-string-annotation-operands branch August 19, 2026 14:43
carljm added a commit that referenced this pull request Aug 19, 2026
…ype-form

* origin/main:
  [ty] Fix dynamic-class diagnostic snapshot in string annotations (#27890)
  Lock `wrangler` for playground deployments (#27838)
  [ty] Dogfood standalone scripts with inline configuration (#27885)
  Annotate intentional release secret inheritance (#27887)
  [ty] Skip invalid subscript operands in string annotations (#27882)
  [ty] Handle dynamic classes in string annotations (#27883)
  Disable `npm` install scripts by default (#27837)
  Require wheels for Python tooling dependencies (#27861)
  Update dependency pytest to v9 (#27881)
charliermarsh added a commit that referenced this pull request Aug 20, 2026
…icts (#27914)

## Summary

Follow-up to #27882.

Previously, changing deferred lookup modes while inferring `Annotated`
metadata could discard the enclosing string-annotation context, causing
parsed expressions to be looked up in the module's semantic index and
panic:

```python
from typing_extensions import Annotated, TypedDict

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

We now preserve `InStringAnnotation` while inferring lambda defaults.
For `TypedDict.extra_items`, we pass through the existing state and rely
on annotation inference to defer stub annotations without losing their
enclosing string context. This also covers nested lambdas, dynamic
classes in defaults, and positional-only and keyword-only stub defaults.

All 838 semantic tests pass.
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

bug An issue describing something that isn't working, or a PR that fixes a bug ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant