Created
November 12, 2025 21:19
-
-
Save mypy-play/6e17e7a90c857368cee3cf997eeb396e to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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
| from typing import TypeVar, Literal | |
| A = TypeVar("A") | |
| B = TypeVar("B") | |
| MutExResult = tuple[Literal["neither"], None, None] | tuple[Literal["both"], A, B] | tuple[Literal["left"], A, None] | tuple[Literal["right"], None, B] | |
| def check_mutually_exclusive(a: A | None, b: B | None) -> MutExResult[A, B]: | |
| if a is None and b is None: | |
| return "neither", None, None | |
| elif a is not None and b is not None: | |
| return "both", a, b | |
| elif a is not None: | |
| return "left", a, None | |
| else: | |
| assert b is not None | |
| return "right", None, b | |
| def usage(a1: int | None, b1: str | None): | |
| res, a, b = check_mutually_exclusive(a1, b1) | |
| reveal_type(res) | |
| reveal_type(a) | |
| reveal_type(b) | |
| if res == "both": | |
| reveal_type(a) | |
| reveal_type(b) | |
| elif res == "neither": | |
| reveal_type(a) | |
| reveal_type(b) | |
| elif res == "left": | |
| reveal_type(a) | |
| reveal_type(b) | |
| elif res == "right": | |
| reveal_type(a) | |
| reveal_type(b) | |
| def usage2(a1: int | None, b1: str | None): | |
| res = check_mutually_exclusive(a1, b1) | |
| reveal_type(res) | |
| if res[0] == "both": | |
| reveal_type(res[1]) | |
| reveal_type(res[2]) | |
| elif res[0] == "neither": | |
| reveal_type(res[1]) | |
| reveal_type(res[2]) | |
| elif res[0] == "left": | |
| reveal_type(res[1]) | |
| reveal_type(res[2]) | |
| elif res[0] == "right": | |
| reveal_type(res[1]) | |
| reveal_type(res[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment