Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created April 24, 2024 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mypy-play/2d9be2c95f6cc3bb9d35ae6a27f6234b to your computer and use it in GitHub Desktop.
Save mypy-play/2d9be2c95f6cc3bb9d35ae6a27f6234b to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import TypeVar
from typing_extensions import TypeIs
_T = TypeVar('_T', str, int)
def is_str(val: str | int) -> TypeIs[str]:
return isinstance(val, str)
def is_int(val: str | int) -> TypeIs[int]:
return isinstance(val, int)
def process_str_int(data: _T) -> _T:
if is_str(data):
reveal_type(data) # str
elif is_int(data):
reveal_type(data) # int
return data
process_str_int("hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment