Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 11, 2024 13:59
Show Gist options
  • Save mypy-play/cc89e323f3de43510aaf34cddcbf08d8 to your computer and use it in GitHub Desktop.
Save mypy-play/cc89e323f3de43510aaf34cddcbf08d8 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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