Created
April 24, 2024 18:00
-
-
Save mypy-play/2d9be2c95f6cc3bb9d35ae6a27f6234b to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains 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 | |
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