Interview Prep/Python for DevOps/Language essentials & functions

What are `TypedDict` and `Protocol`, and when are they useful?

AdvancedConceptLanguage essentials & functions
Answer
  • TypedDict types a dict with known string keys, perfect for API payloads you keep as dicts: class Tag(TypedDict): Key: str; Value: str. boto3-stubs uses TypedDicts for every boto3 response.
  • Protocol is structural typing ("anything with a .describe_instances() method"), so you can type an injected client without depending on a concrete class, which makes fakes in tests type-check cleanly.
class SupportsSend(Protocol):
    def send(self, msg: str) -> None: ...
All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions