pysen
Pysen is a Python library designed to simplify code linting and formatting by centralizing the configuration and orchestration of various tools like Black, Flake8, Isort, and Mypy. It aims to provide a unified platform for maintaining consistent coding styles across projects, reducing the burden of managing individual linter configurations. Developed by Preferred Networks, it is actively maintained with regular releases.
Warnings
- gotcha Pysen explicitly does not manage the versions of the underlying linting tools (e.g., black, mypy, flake8, isort). Users are expected to install and manage these dependencies separately using their preferred package manager (e.g., pipenv, poetry).
- gotcha By default, pysen only checks files that are tracked in Git. Files not under Git version control will be ignored.
- gotcha Mypy has its own mechanism for discovering files and does not honor the `tool.pysen.lint.source` option in `pyproject.toml` like other linters.
- gotcha Mypy may report an error 'Source file found twice under different module names' if not configured correctly.
- breaking Python 3.7 is no longer supported starting from pysen 0.12.0.
- breaking Pysen 0.12.0 includes fixes for colons that violated new `configparser` rules introduced in Python 3.14. Older versions of pysen might break when run with Python 3.14.
Install
-
pip install "pysen[lint]" -
pip install pysen -
poetry add -D pysen==0.12.0 -E lint
Imports
- Config
from pysen.pyproject_model import Config
Quickstart
# 1. Create a pyproject.toml or pysen.toml file in your project root: # pyproject.toml # [tool.pysen] # version = "0.11" # Specify the pysen configuration version # # [tool.pysen.lint] # enable_black = true # enable_flake8 = true # enable_isort = true # enable_mypy = true # mypy_preset = "strict" # line_length = 88 # py_version = "py310" # Adjust to your target Python version # # [[tool.pysen.lint.mypy_targets]] # paths = ["."] # 2. Run pysen from your terminal: # To check for linting issues: pysen run lint # To automatically format code (where supported by linters like black, isort): pysen run format