[ty] Avoid unsound TypedDict dictionary disjointness#26859
Draft
charliermarsh wants to merge 2 commits into
Draft
[ty] Avoid unsound TypedDict dictionary disjointness#26859charliermarsh wants to merge 2 commits into
charliermarsh wants to merge 2 commits into
Conversation
This was referenced Jul 15, 2026
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 94.70%. The percentage of expected errors that received a diagnostic held steady at 90.27%. The number of fully passing files held steady at 97/134. |
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
2 | 0 | 0 |
invalid-key |
2 | 0 | 0 |
invalid-assignment |
1 | 0 | 0 |
unresolved-attribute |
1 | 0 | 0 |
| Total | 6 | 0 | 0 |
Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.
Raw diff:
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/generic.py:3686:46 error[invalid-argument-type] Argument to bound method `TypedDictFallback.pop` is incorrect: Expected `Never`, found `Literal["__index__"]`
+ pandas/core/generic.py:3687:47 error[invalid-argument-type] Argument to bound method `TypedDictFallback.pop` is incorrect: Expected `Never`, found `Literal["__columns__"]`
+ pandas/core/generic.py:3693:27 error[invalid-assignment] Object of type `(Mapping[str | int, (...) -> Unknown] & Top[dict[Unknown, Unknown]]) | (Mapping[str | int, (...) -> Unknown] & <TypedDict with no items>)` is not assignable to `list[Unknown] | tuple[Unknown, ...] | dict[Unknown, Unknown] | ((...) -> Unknown) | None`
+ pandas/core/generic.py:3697:21 error[unresolved-attribute] Attribute `update` is not defined on `list[Unknown]`, `tuple[Unknown, ...]`, `(...) -> Unknown`, `None` in union `list[Unknown] | tuple[Unknown, ...] | dict[Unknown, Unknown] | ((...) -> Unknown) | None`
+ pandas/io/common.py:1401:23 error[invalid-key] TypedDict `<TypedDict with no items>` can only be subscripted with a string literal key, got key of type `Hashable | str`.
xarray (https://github.com/pydata/xarray)
+ xarray/core/dataset.py:4722:21 error[invalid-key] TypedDict `<TypedDict with no items>` can only be subscripted with a string literal key, got key of type `Hashable`.
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
This is a focused follow-up to #25851.
When comparing a
TypedDictwith another type, we currently approximate everyTypedDictasdict[str, Any]. Because dictionary key parameters are invariant, that can incorrectly prove aTypedDictdisjoint from dictionary types whose key domains admit its actual string-literal keys, such asdict[Literal["x"], object]ordict[object, object]. Narrowing can then eliminate a reachable branch, including anisinstance()check whose generic class-info can instantiate todict.The same issue affects identity narrowing against empty dictionaries. An optional
TypedDictcan be empty at runtime, so comparing it with adict[Never, Never]must not make the identity branch unreachable.We now use the gradual projection
dict[Any, Any]for this fallback. This deliberately refuses to prove disjointness unless we have stronger evidence, while preserving the fact that aTypedDictis disjoint from properdictsubclasses.This remains intentionally conservative. We do not derive a precise dictionary projection from required fields, so
TypedDictversusMapping[int, object]remains non-disjoint, and required field value types are not used to prove disjointness from types such asdict[str, str]. The change also does not add broader materialization or correlation-aware refinement for generic class-info types.