[ty] improve TypedDict narrowing logic#22786
Conversation
Typing conformance resultsNo changes detected ✅ |
|
|
This looks correct (good catch), but I'm not sure the solution scales well. I think we have the same problem for narrowing tagged unions of tuples, e.g.: from typing import Literal
type Tuple1 = tuple[int, Literal["tag1"]]
type Tuple2 = tuple[str, Literal["tag2"]]
def g(x: tuple[int, Literal["tag1"]] | tuple[str, Literal["tag2"]]):
if x[1] == "tag1":
reveal_type(x[0]) # narrowed to int
def f(x: Tuple1 | Tuple2):
if x[1] == "tag1":
reveal_type(x[0]) # not narrowed: still int | str |
|
A bigger problem is that I'd like to start working on whether this idea works. |
Basically it works but implicit recursive type aliases cause panics, which means we should merge #22238 first (and lazy evaluate more implicit type aliases). |
|
closed in favor of #22894. |
Summary
While working on #22238, I noticed that the following narrowing wasn't working:
The problem was that the narrowing logic for
TypedDictdidn't take into account type aliases that point toTypedDict.I fixed that and made the handling cleaner and less error-prone (I think).
Test Plan
mdtest updated