Created
May 11, 2024 13:59
-
-
Save mypy-play/cc89e323f3de43510aaf34cddcbf08d8 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 Type | |
from typing import Any | |
from typing import TypeVar | |
from typing import cast | |
T = TypeVar("T") | |
def assert_type(x: Any, type: Type[T]) -> None: | |
if not isinstance(x, type): | |
raise TypeError() | |
return None | |
def func(x: str) -> str: | |
return x | |
unbound: object | |
assert_type(unbound, str) | |
func(cast(str, unbound)) | |
bound: str | |
assert_type(bound, str) | |
func(cast(str, bound)) | |
func(bound) # type: ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment