Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created October 12, 2019 00:12
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/1b614c39b5c07bada4a706ee036d0f0d to your computer and use it in GitHub Desktop.
Save podhmo/1b614c39b5c07bada4a706ee036d0f0d to your computer and use it in GitHub Desktop.
import typing as t
import typing_extensions as tx
"""
$ mypy --strict 05protocol.py
"""
class HasName(tx.Protocol):
@property
def name(self) -> str:
...
class Person:
name: str
def __init__(self, name: str) -> None:
self.name = name
class Display:
def __init__(self, typ: t.Type[t.Any]) -> None:
self.typ = typ
@property
def name(self) -> str:
return self.typ.__name__
def get_name(o: HasName) -> str:
return o.name
def main() -> None:
get_name(Person("foo"))
get_name(Display(Person))
# get_name(object())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment