Skip to content

Instantly share code, notes, and snippets.

@jaycosaur
Last active August 10, 2020 06:32
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 jaycosaur/84787af794ee57ec584a01628f21c326 to your computer and use it in GitHub Desktop.
Save jaycosaur/84787af794ee57ec584a01628f21c326 to your computer and use it in GitHub Desktop.
Intersection types [python-protocols] - Typescript to Python field guide
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