Last active
August 10, 2020 06:32
-
-
Save jaycosaur/84787af794ee57ec584a01628f21c326 to your computer and use it in GitHub Desktop.
Intersection types [python-protocols] - 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 | |
from dataclasses import dataclass | |
class HasAge(Protocol): | |
age: int | |
class HasName(Protocol): | |
name: str | |
# intersection protocol type through multiple inheritance | |
class HasNameAndAge(HasName, HasAge, Protocol): | |
... | |
# implicitly implements the HasNameAndAge interface | |
@dataclass | |
class WithNameAndAge: | |
age: int | |
name: str | |
correct: HasNameAndAge = WithNameAndAge(name="Tim", age=23) # it works! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment