Skip to content

fix TypedDict tagged-union narrowing from a PEP 695 type alias #2645

@carljm

Description

@carljm

We fail to do TypedDict tagged-union narrowing on this example:

from typing import TypedDict, Literal

class Ok(TypedDict):
   result: Literal["ok"]
   payload: bytes

class Err(TypedDict):
   result: Literal["error"]
   message: str

type Response = Ok | Err

def read(res: Response) -> bytes:
   if res["result"] == "ok":
        return res["payload"]
   else:
       raise Exception(res["message"])

But we do narrow if res: Ok | Err, or if Response = Ok | Err (legacy type alias instead of PEP 695 type alias).

We aren't handling a PEP 695 type alias as a union in narrowing. We should fix this, and also look for other cases in narrowing where we special-case unions, and ensure a PEP 695 alias to a union is handled the same way.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions