[ty] Respect dict-compatible fallbacks in TypedDict unions#25242
Merged
Conversation
Memory usage reportMemory usage unchanged ✅ |
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134. |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-key |
0 | 4 | 0 |
missing-typed-dict-key |
0 | 2 | 0 |
unused-type-ignore-comment |
1 | 0 | 0 |
| Total | 1 | 6 | 0 |
Raw diff:
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- tests/annotations/declarations.py:366:59 error[invalid-key] Unknown key "a" for TypedDict `EmptyDict`
- tests/annotations/declarations.py:366:72 error[invalid-key] Unknown key "a" for TypedDict `EmptyDict`
+ tests/annotations/declarations.py:405:32 warning[unused-type-ignore-comment] Unused blanket `type: ignore` directive
pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- tests/frame/test_frame.py:3293:34 error[missing-typed-dict-key] Missing required key 'formats' in TypedDict `_DTypeDict` constructor
- tests/frame/test_frame.py:3293:34 error[missing-typed-dict-key] Missing required key 'names' in TypedDict `_DTypeDict` constructor
- tests/frame/test_frame.py:3293:35 error[invalid-key] Unknown key "col1" for TypedDict `_DTypeDict`
- tests/frame/test_frame.py:3293:52 error[invalid-key] Unknown key "col2" for TypedDict `_DTypeDict`6f2963f to
180889c
Compare
dict-compatible fallbacks in TypedDict unions
180889c to
0920b3a
Compare
AlexWaygood
reviewed
May 20, 2026
| has_dict_compatible_fallback = true; | ||
| } else if !has_dict_compatible_fallback | ||
| && dict_fallback.is_assignable_to(self.db(), element) | ||
| && element.is_assignable_to(self.db(), mapping_fallback) |
Member
There was a problem hiding this comment.
it's not clear to me why we're imposing the restriction here that the element needs to be assignable to Mapping[Unknown, Unknown] in order for it to be a valid dict-compatible fallback. On your branch, we still emit a false-positive error here (it's a false positive because dict[int, int] is a subtype of Iterable[int]):
from typing import TypedDict, Iterable
class Foo(TypedDict):
x: int
def f(obj: Foo | Iterable[int]): ...
f({42: 42})
Member
Author
There was a problem hiding this comment.
This is clearly an improvement but not sufficiently general, lemme think.
Member
Author
There was a problem hiding this comment.
Ah, ok, I think the speculative inference piece on its own is sufficient now.
70ec716 to
f9f3ae3
Compare
thejchap
pushed a commit
to thejchap/ruff
that referenced
this pull request
May 23, 2026
…sh#25242) ## Summary Prior to this change, this snippet would produce a TypedDict-based `missing-key` diagnostic, because the RHS wasn't viewed as assignable to the `Mapping[int, float]` (since we were only gating on `dict`): ```py from typing import TypedDict, Mapping class Foo(TypedDict): some: str name: str mapping: Foo | Mapping[int, float] = {1: 5.2} ``` We now use speculative inference to determine whether the non-TypedDict union member `T` is a viable fallback type. Closes astral-sh/ty#3488.
anishgirianish
pushed a commit
to anishgirianish/ruff
that referenced
this pull request
May 28, 2026
…sh#25242) ## Summary Prior to this change, this snippet would produce a TypedDict-based `missing-key` diagnostic, because the RHS wasn't viewed as assignable to the `Mapping[int, float]` (since we were only gating on `dict`): ```py from typing import TypedDict, Mapping class Foo(TypedDict): some: str name: str mapping: Foo | Mapping[int, float] = {1: 5.2} ``` We now use speculative inference to determine whether the non-TypedDict union member `T` is a viable fallback type. Closes astral-sh/ty#3488.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prior to this change, this snippet would produce a TypedDict-based
missing-keydiagnostic, because the RHS wasn't viewed as assignable to theMapping[int, float](since we were only gating ondict):We now use speculative inference to determine whether the non-TypedDict union member
Tis a viable fallback type.Closes astral-sh/ty#3488.