Created
October 12, 2019 00:12
-
-
Save podhmo/1b614c39b5c07bada4a706ee036d0f0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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