Flake8 Annotations
Flake8 Annotations is a plugin for Flake8 that enforces the presence and style of PEP 3107-style function annotations in Python code. It helps maintain consistent typing practices by flagging missing or incorrect type annotations. The current version is 3.2.0, and the project is actively maintained with regular updates addressing Python and Flake8 compatibility.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, requiring Python >=3.8.1 and Flake8 >=5.0. Projects using older Python or Flake8 versions must upgrade their environment or pin flake8-annotations to a version <3.0.0.
- breaking As of v3.0.0, support for PEP 484-style type comments (e.g., `# type: (str) -> None`) was removed. Instead, the new `ANN402` warning code (disabled by default) flags the *presence* of these now-unsupported type comments.
- gotcha `ANN4xx` error codes (e.g., `ANN401` for `typing.Any`, `ANN402` for type comments) are considered 'opinionated warnings' and are disabled by default.
- gotcha The `ANN401` warning for `typing.Any` can be overly strict, especially for `*args: Any` and `**kwargs: Any`.
- gotcha By default, `flake8-annotations` does not respect `# type: ignore` comments.
Install
-
pip install flake8 flake8-annotations
Imports
- flake8_annotations
(Run flake8 command)
Quickstart
import os
def my_function(arg1, arg2):
# This function is missing type annotations
return arg1 + arg2
def another_function(value: int) -> str:
return str(value)
# To run flake8, save this as example.py and execute:
# flake8 example.py
# You should see errors like ANN001, ANN201 for my_function.