Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created March 31, 2023 01:21
Show Gist options
  • Save mypy-play/2a8773ad5ba9253ac8933ad67b240010 to your computer and use it in GitHub Desktop.
Save mypy-play/2a8773ad5ba9253ac8933ad67b240010 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import TypeVar, Union
T = TypeVar("T")
Thing = Union[str, int]
MaybeTuple = Union[T, tuple[T]]
def untuple(item: MaybeTuple[T]) -> T:
if isinstance(item, tuple):
return item[0]
return item
def test_func(var: MaybeTuple[Thing]):
reveal_type(untuple(var)) # object in mypy, str | int (==Thing) in pyright
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment