AI Space

DevOps & Tooling / Knowledge library

mypy

gradual typing annotations protocols and type narrowing

Python static type checker

Why learn mypy?

Useful for catching interface mismatches before runtime. Unchecked dynamic code and Any values can leave gaps, and annotations do not validate incoming data.

Where it came from

mypy analyzes Python type annotations to find inconsistencies without executing the program. Its gradual typing model supports incremental adoption.

Build your understanding

Work through these exercises in order. Try the task before opening a reference.

  1. Annotate a function's inputs and output.
  2. Introduce and fix a deliberate type mismatch.
  3. Narrow an optional value before use.
  4. Define a protocol for a dependency.
  5. Inspect how Any or missing annotations reduce coverage.

Documentation and sources

Catalog research notes

Connect the ideas

Built on

Python

Explore next

RuffpytestTypeScript

Real World Example

Illustrative business scenario · AI-generated from linked documentation · Not a measured customer case study

Protocol-Based Plugin Architecture for Data Ingestion

Data engineering teams building modular ingestion pipelines that must support diverse, third-party data sources without tight coupling.

The business problem

Runtime errors occur when ingestion modules expect specific attributes from dynamic data objects, leading to attribute errors during production execution.

How this topic solves it

Mypy Protocols enable structural subtyping, allowing developers to define an interface based on required attributes rather than inheritance, while type narrowing safely confirms object capabilities at runtime.

  1. Define a Protocol class specifying the required methods or attributes for a data source.
  2. Annotate ingestion functions to accept the Protocol type instead of concrete classes.
  3. Implement type narrowing using isinstance() or type guards to verify object structure before access.
  4. Run mypy to statically validate that all passed objects satisfy the Protocol requirements.
Protocol-Based Ingestion Flow · Entity relationships

Shows how ingestion logic validates data sources via Protocols.

IngestionEngine

Core processing logic

DataSourceProtocol

Interface definition

ExternalAPI

Concrete data source

DatabaseSource

Concrete data source

  • IngestionEngine → depends on → DataSourceProtocol
  • ExternalAPI → satisfies → DataSourceProtocol
  • DatabaseSource → satisfies → DataSourceProtocol

Expected business outcome

Increased system reliability through compile-time detection of missing interface implementations, reducing runtime attribute errors.

Limitations and checks

Protocols rely on structural compatibility, which can lead to false positives if two objects share the same attribute names but have different semantic meanings; verify with runtime checks.

Sources

Generated 2026-10-18 with gemini-3.1-flash-lite. Your assessment includes two questions about this example.

Catalog source: technology-maxxing. This page reproduces the existing catalog’s learning material and source links.