Last active
August 10, 2020 06:31
-
-
Save jaycosaur/b511a096a9a524614bd35252eadc8b92 to your computer and use it in GitHub Desktop.
Implicit vs Explicit typing [python-protocol] - Typescript to Python field guide
This file contains hidden or 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
from typing import Protocol, Sequence | |
from dataclasses import dataclass | |
class PersonProtocol(Protocol): | |
name: str | |
age: int | |
height: float | |
friends: Sequence["PersonProtocol"] | |
# Person implicitly implements the PersonProtocol interface | |
@dataclass | |
class Person: | |
name: str | |
age: int | |
height: float | |
friends: Sequence[PersonProtocol] | |
jane: PersonProtocol = Person(name="Jane", age=25, height=1.74, friends=[]) # it works | |
tim: PersonProtocol = Person( | |
name="Tim", age=23, height=1.71, friends=[jane] | |
) # it works | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment