Created
April 25, 2024 12:26
-
-
Save mypy-play/176659a7fecfb5a33b95a20ccf74e2a2 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