Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created Jul 28, 2020
Embed
What would you like to do?
def hello(name: str) -> None:
pass
from typing_extensions import Annotated
class Description:
def __init__(self, description: str) -> None:
self.description = description
def hello(*, name: Annotated[str, Description("the name of person")]) -> None:
print(f"hello {name}")
from typing_extensions import Annotated
class Description:
def __init__(self, description: str) -> None:
self.description = description
def hello(*, name: Annotated[str, Description("the name of person")]) -> None:
reveal_type(name)
print(f"hello {name}")
from typing_extensions import Annotated
class Description:
def __init__(self, description: str) -> None:
self.description = description
def hello(*, name: Annotated[str, Description("the name of person")]) -> None:
print(f"hello {name}")
if __name__ == "__main__":
from typing_extensions import get_type_hints
print(get_type_hints(hello))
# {'name': <class 'str'>, 'return': <class 'NoneType'>}
print(get_type_hints(hello, include_extras=True))
# {'name': typing_extensions.Annotated[str, <__main__.Description object at 0x10cd1e730>], 'return': <class 'NoneType'>}
from typing_extensions import get_args, get_origin
hints = get_type_hints(hello, include_extras=True)
print(get_args(hints["name"]))
# (<class 'str'>, <__main__.Description object at 0x106427730>)
print(get_origin(hints["name"]))
# <class 'typing_extensions.Annotated'>
from typing_extensions import Annotated
class Description:
def __init__(self, description: str) -> None:
self.description = description
def hello(*, name: Annotated[str, Description("the name of person")]) -> None:
print(f"hello {name}")
if __name__ == "__main__":
from typing_extensions import get_type_hints
hints = get_type_hints(hello, include_extras=True)
import typing_inspect
print(typing_inspect.get_args(hints["name"]))
# (<class 'str'>,)
print(hasattr(hints["name"], "__metadata__"))
# True
print(hints["name"].__metadata__)
# (<__main__.Description object at 0x104b9b730>,)
PY_FILE = $(shell echo $@*.py)
00:
mypy --strict $(PY_FILE)
01:
mypy --strict $(PY_FILE)
02:
diff -u 01*.py 02*.py || exit 0
mypy --strict $(PY_FILE)
setup:
python -m pip install -r requirements.txt
typing-extensions
typing-inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment