Skip to content

[ty] Fix type[T] inference for TypedDict protocol bounds#24423

Open
charliermarsh wants to merge 1 commit intomainfrom
charlie/typed-dict-generic
Open

[ty] Fix type[T] inference for TypedDict protocol bounds#24423
charliermarsh wants to merge 1 commit intomainfrom
charlie/typed-dict-generic

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

@charliermarsh charliermarsh commented Apr 5, 2026

Summary

My understanding after reasoning through this for a while (with limited prior experience around this part of the codebase): typically, when solving for type[T], given a class, we convert to the instance of that class, then infer T from that instance type.

This fails specifically for cases like:

class TypedDictLikeV1(Protocol):
    __required_keys__: ClassVar[frozenset[str]]
    __optional_keys__: ClassVar[frozenset[str]]

Because __required_keys__ exists on the TypedDict class, but not on instances, apparently:

>>> from typing import TypedDict
>>> class Foo(TypedDict):
...     bar: int
...
>>> Foo.__required_keys__
frozenset({'bar'})
>>> Foo(bar=1).__required_keys__
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    Foo(bar=1).__required_keys__
AttributeError: 'dict' object has no attribute '__required_keys__'

This PR adds a special case to the specialization code for TypedDict such that if the instance doesn't satisfy the bound, but the class object does, we allow it to satisfy type[T].

This does not change protocol matching for instances, because if we did make the same change there, we'd be misrepresenting whether __required_keys__ is available on TypedDict instances.

Closes #24423.

@astral-sh-bot astral-sh-bot bot added the ty Multi-file analysis & type inference label Apr 5, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.72%. The percentage of expected errors that received a diagnostic held steady at 82.85%. The number of fully passing files held steady at 74/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 5, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh charliermarsh force-pushed the charlie/typed-dict-generic branch from 576287b to 1c2877a Compare April 6, 2026 00:11
@charliermarsh charliermarsh marked this pull request as ready for review April 6, 2026 01:45
@astral-sh-bot astral-sh-bot bot requested a review from oconnor663 April 6, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants