Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created July 28, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/fc74f85f1800a6bad80c4b9f7b1ed955 to your computer and use it in GitHub Desktop.
Save podhmo/fc74f85f1800a6bad80c4b9f7b1ed955 to your computer and use it in GitHub Desktop.
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