Mypy Extensions

1.1.0 · active · verified Sat Mar 28

mypy-extensions provides specific type system extensions for programs checked with the mypy type checker and the mypyc compiler. It acts as a bridge for experimental or mypy-specific typing features that may eventually be standardized in Python's `typing` module or `typing_extensions`, but are supported early by mypy. The library follows mypy's active development and releases are tied to mypy's needs, which typically sees multiple releases per month for the main mypy project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the use of the `override` decorator from `mypy_extensions`. Run `mypy your_file_name.py` to type-check this example. If `SpecificAnalyzer.analyze` did not correctly override `BaseAnalyzer.analyze`, mypy would report an error. Note that for Python 3.12+, `typing.override` should generally be preferred if `mypy-extensions` is not otherwise needed. The core functionality of `mypy-extensions` is to provide types for mypy to check, not to execute specific runtime logic itself.

from mypy_extensions import override
from typing import Dict, Any, List

class BaseAnalyzer:
    def analyze(self, data: Dict[str, Any]) -> List[str]:
        raise NotImplementedError

class SpecificAnalyzer(BaseAnalyzer):
    @override
    def analyze(self, data: Dict[str, Any]) -> List[str]:
        if "items" in data:
            return [str(item) for item in data["items"]]
        return []

# Example usage for mypy checking
def process_data(analyzer: BaseAnalyzer, input_data: Dict[str, Any]) -> List[str]:
    return analyzer.analyze(input_data)

if __name__ == "__main__":
    analyzer_instance = SpecificAnalyzer()
    results = process_data(analyzer_instance, {"items": [1, "two", 3.0]})
    print(f"Analysis results: {results}")

# To type-check this file:
# mypy your_file_name.py

view raw JSON →