Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Last active July 26, 2024 15:38
Show Gist options
  • Save CodeByAidan/1770443d22e72ed34d2c80f5305beb95 to your computer and use it in GitHub Desktop.
Save CodeByAidan/1770443d22e72ed34d2c80f5305beb95 to your computer and use it in GitHub Desktop.
will be updating this a lot whenever I want to use typing but with typing that's not present in typing library. :trollface: mypy + pylance ready!
from __future__ import annotations
from typing import Any, TypeVar, overload
T = TypeVar("T")
@overload
def cast(typ: type[T], val: Any, /) -> T: ...
@overload
def cast(typ: str, val: Any, /) -> Any: ...
@overload
def cast(typ: object, val: Any, /) -> Any: ...
def cast(typ: Any, val: Any, /) -> Any: # fmt: off
"""Cast a value to a type.
This returns the value unchanged. To the type checker this
signals that the return value has the designated type, but at
runtime we intentionally don't check anything (we want this
to be as fast as possible).
"""
return val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment