forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider field value types when disambiguating a union of TypedDicts
Fixes python#8533. Previously, given a union of TypedDicts, e.g. `A|B` in ```py from typing import TypedDict, Literal, Union class A(TypedDict): tag: Literal['A'] extra_a: str class B(TypedDict): tag: Literal['B'] extra_b: str ``` when needing to disambiguate the union, e.g. ``` td: A|B = { 'tag': 'A', 'extra_a': 'foo', } ``` mypy would only consider the *keys* of the dict expression and TypedDict, e.g. 'tag' and 'extra_a'. But if multiple members of the union have the same shape, only distinguished by a value type, the disambiguation fails, e.g. ```py class A(TypedDict): tag: Literal['A'] class B(TypedDict): tag: Literal['B'] td: A|B = { # E: Type of TypedDict is ambiguous, could be any of ("A", "B") 'tag': 'A', } ``` To allow this, also consider the types of the dict expression's values when narrowing the candidates from the union.
- Loading branch information
Showing
2 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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