-
Notifications
You must be signed in to change notification settings - Fork 265
Closed
Description
Describe the Bug
Pyrefly currently doesn't seem to support Discriminated Unions / Tagged Unions
This feature is widely used, including OpneAI SDK (or output of codegen tools like Stainless)
Here is the simple example from mypy (the same as sandbox link).
The below code passes in mypy, pyright, and etc.
from typing import Literal, TypedDict, Union
class NewJobEvent(TypedDict):
tag: Literal["new-job"]
job_name: str
config_file_path: str
class CancelJobEvent(TypedDict):
tag: Literal["cancel-job"]
job_id: int
Event = Union[NewJobEvent, CancelJobEvent]
def process_event(event: Event) -> None:
# Since we made sure both TypedDicts have a key named 'tag', it's
# safe to do 'event["tag"]'. This expression normally has the type
# Literal["new-job", "cancel-job"], but the check below will narrow
# the type to either Literal["new-job"] or Literal["cancel-job"].
#
# This in turns narrows the type of 'event' to either NewJobEvent
# or CancelJobEvent.
if event["tag"] == "new-job":
print(event["job_name"])
else:
print(event["job_id"])We got error like:
ERROR 27:21-31: TypedDict `CancelJobEvent` does not have key `job_name` [[typed-dict-key-error](https://pyrefly.org/en/docs/error-kinds/#typed-dict-key-error)]
ERROR 29:21-29: TypedDict `NewJobEvent` does not have key `job_id` [[typed-dict-key-error](https://pyrefly.org/en/docs/error-kinds/#typed-dict-key-error)]
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels