Skip to content

[ty] Handle dynamic classes in string annotations - #27883

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/dynamic-classes-string-annotations
Aug 19, 2026
Merged

charliermarsh merged 2 commits into
mainfrom
charlie/dynamic-classes-string-annotations

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Previously, creating a dynamic class inside a string annotation could panic because the call's AST node belongs to a separately parsed annotation rather than the enclosing module:

from types import new_class
from typing import Annotated


def example(
    first: "Annotated[int, type('First', (), {})]",
    second: "Annotated[int, new_class('Second', ())]",
) -> None: ...

Dynamic classes normally retain a scope-relative AST node index as their identity. For calls parsed from string annotations, that index was missing or later resolved against the wrong AST.

We now anchor those calls to the enclosing string expression and record the call's relative source range. This preserves distinct class identities, points diagnostics at the actual call, and handles nested annotations without retaining absolute source positions.

Supersedes #27851.

@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 ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

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 review from a team as code owners August 19, 2026 14:43
@charliermarsh
charliermarsh merged commit b122061 into main Aug 19, 2026
66 checks passed
@astral-sh-bot
astral-sh-bot Bot requested a review from carljm August 19, 2026 14:43
@charliermarsh
charliermarsh deleted the charlie/dynamic-classes-string-annotations 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)
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.

2 participants