flake8-pep585

raw JSON →
0.1.7 verified Fri May 01 auth: no python

A flake8 plugin that enforces the use of new-style type hints as specified in PEP 585 (e.g., list[str] instead of typing.List[str]). Current version 0.1.7 supports Python 3.7+ with configurable activation on older versions. Released periodically.

pip install flake8-pep585
error ModuleNotFoundError: No module named 'flake8_pep585'
cause The plugin is installed but users try to import it directly as a Python module.
fix
Do not import flake8_pep585 directly; it is a flake8 plugin. Run flake8 with the plugin installed and ensure require-plugins is set.
error PEP 585: Use new-style type hint '...' instead of 'typing...'
cause The plugin correctly flags old-style type hints (e.g., typing.List) that should be replaced with built-in generics (e.g., list).
fix
Change typing.List[int] to list[int], typing.Dict[str, int] to dict[str, int], etc.
gotcha The plugin only works when flake8 is invoked with the '--require-plugins=flake8-pep585' flag or a compatible configuration if using flake8>=6.0.
fix Run flake8 with '--require-plugins=flake8-pep585' or configure 'require_plugins = flake8-pep585' in setup.cfg or .flake8.
gotcha On Python 3.7 and 3.8, the plugin is disabled by default unless 'from __future__ import annotations' is used. This can be confusing when running CI on multiple Python versions.
fix Use '--pep585-activation=always' to force enable on Python 3.7/3.8, or add 'from __future__ import annotations' to files.

Install the plugin and run flake8 on a file with an old-style type hint. The plugin will emit an error (PE P585) suggesting to use list[int] instead of List[int].

pip install flake8 flake8-pep585
cat > test.py <<EOF
from typing import List
def foo() -> List[int]:
    return [1, 2]
EOF
flake8 test.py