Mypy Zope Plugin
mypy-zope is a plugin for Mypy that provides type checking support for projects utilizing Zope interfaces (`zope.interface`). It allows Mypy to correctly understand and validate code that implements or interacts with Zope interfaces. The current version is 1.0.14, with releases occurring periodically to support newer Mypy versions and address issues.
Common errors
-
Mypy could not find plugin 'mypy_zope.plugin'
cause The `mypy-zope` package is either not installed in the environment Mypy is running in, or the specified plugin path in `mypy.ini` is incorrect.fixEnsure `mypy-zope` is installed with `pip install mypy-zope`. Verify the `plugins` line in your `mypy.ini` correctly states `plugins = mypy_zope.plugin`. -
Error: Incompatible types in assignment (expression has type "Foo", variable has type "IBar")
cause Mypy is reporting type errors related to `zope.interface` even though you expect the plugin to handle them, indicating the plugin is likely not active.fixConfirm that `plugins = mypy_zope.plugin` is correctly configured in your `mypy.ini` file and that Mypy is using that configuration (e.g., by running `mypy --config-file mypy.ini your_file.py`).
Warnings
- gotcha The `mypy-zope` plugin requires Mypy version 1.0.0 or higher. Using it with older Mypy versions may lead to unexpected behavior or errors.
- gotcha The plugin must be explicitly enabled in your Mypy configuration file (e.g., `mypy.ini`, `pyproject.toml`). It is not automatically discovered or activated upon installation.
Install
-
pip install mypy-zope
Imports
- Mypy Plugin Configuration
import mypy_zope.plugin
plugins = mypy_zope.plugin
Quickstart
# 1. Create a mypy.ini file in your project root:
# [mypy]
# plugins = mypy_zope.plugin
# python_version = 3.9 # or your project's Python version
# 2. Create a Python file, e.g., my_app.py:
# from zope.interface import Interface, implementer
# class IGreeter(Interface):
# def greet(name: str) -> str:
# """Greets the given name."""
# @implementer(IGreeter)
# class HelloGreeter:
# def greet(self, name: str) -> str:
# return f"Hello, {name}!"
# def use_greeter(greeter: IGreeter) -> None:
# print(greeter.greet("World"))
# use_greeter(HelloGreeter())
# 3. Run mypy from your terminal:
# mypy my_app.py
# Expected output for the above example (no errors if configured correctly):
# Success: no issues found in 1 source file