Skip to main content

sphinxlinter

GitHub tag PyPI CI codecov License

A lightweight Python linter for Sphinx-style docstrings.
It validates structure, field consistency, and alignment between documentation and code.


Overview

Sphinx-style docstrings are widely used across Python projects, but existing tools such as pydocstyle, pydoclint, and ruff focus primarily on general docstring formatting, PEP257 compliance, and style enforcement.

It is designed to complement, not overlap with, these tools.
It targets Sphinx-specific field list conventions and performs semantic consistency checks that go beyond what other linters cover.

Specifically, it focuses on:

  • 🧩 Enforces Sphinx-style field list formatting
  • 🛠️ Ensures consistency between docstrings, signatures, and implementation
  • 📐 Validates section order, duplication, and syntax of documented types and errors
  • 📊 Generates concise, CI-friendly reports
  • ⚙️ Provides a minimalist CLI for easy workflow integration
  • 🐍 Uses only the Python standard library for full compatibility
  • 🧼 Promotes clean, maintainable documentation

📦 Installation

Requires Python ≥ 3.9, but Python ≥ 3.11 is recommended to allow loading configuration files with tomlib from the standard library.

To install via pip, run:

pip install sphinx-linter

⚡ Quick Start

CLI Tool

After installation via pip, you can run the sphinxlinter command directly from your terminal.

Run on the current directory:

  • Run in the current directory: sphinxlinter .
  • Or use the short alias: spxl .

Run on specific files or directories:

spxl path/to/file.py path/to/package/

Standalone Script

Alternatively, you can download the standalone script by clicking the following link and run it with Python:

python sphinxlinter.py path/to/source/

Command Line Usage

Argument / Option Description
[FILES] Files or directories to lint.
--help Show help message and exit.
--enable Enable specific rule codes (or ALL).
--disable Disable specific rule codes (overrides --enable).
--ignore Exclude directories (e.g. venv, .cache).
--statistics Show per-rule violation counts.
--quiet Print diagnostics, but nothing else
--version Print version and exit
--config If not provided, search upward from the [FILES]’ common ancestor for pyproject.toml
--isolated Run in isolated mode, ignoring configuration files

Setting Configuration

Configuration is done via pyproject.toml. The configuration section is [tool.sphinx-linter].

Configuration File Location

The linter uses --config to specify a configuration file (pyproject.toml). If this option is not provided, it searches for a configuration file starting at the common ancestor of all specified files/directories, moving upward until it finds one or reaches the filesystem root. If no configuration file is found, the linter runs with its default settings.

Example configuration:

[tool.sphinx-linter]
# Enable all rules, alternatively specify individual rule codes
enable = ["ALL"]
# Disable specific rules, taking precedence over enable
disable = ["DOC003", "DOC101"]
# Ignore specific directories from linting
ignore = ["venv", ".cache"]

Output Format

When violations are found, the tool outputs lines in the following format:

path/to/file.py:LINE-NUMBER: [CODE] Description of the violation.

Categories:

  • DOC0xx: Structure and formatting issues
  • DOC1xx: Parameter issues
  • DOC2xx: Return issues
  • DOC3xx: Raises issues
  • DOC4xx: Variable issues

Violation Codes

DOC0xx — Structure

Code Description Purpose Enabled by Default
DOC001 Invalid docstring section Detects unknown Sphinx fields. Yes
DOC002 Malformed section Ensures valid field list syntax. Yes
DOC003 Missing blank line after docstring Improves readability. Yes
DOC004 Missing blank line between summary and sections Enforces structure consistency. Yes
DOC005 Too many consecutive blank lines Prevents unnecessary whitespace. Yes
DOC006 Trailing empty lines Keeps docstrings compact. Yes
DOC007 Misplaced section Enforces section order and grouping. Yes
DOC008 One-line docstring should end with a period Complies with PEP 257. Yes
DOC009 Docstring must not use more than 3 double quotes Promotes consistent quoting. Yes
DOC010 Section definition contains invalid whitespace Ensures proper formatting. Yes
DOC011 Trailing non-empty lines after last section Maintains clean endings. Yes
DOC012 Leading whitespaces in first non-blank line Ensures no leading spaces before docstring content. Yes
DOC013 Use the common section key Complies with Sphinx common sections keys No
DOC014 Summary must fit on a single line and is separated from the rest by a blank line Complies with PEP 257. No


DOC1xx — Parameters

Code Description Purpose Enabled by Default
DOC101 Parameter documented but not in signature Detects undocumented or extra parameters. Yes
DOC102 Invalid parameter type syntax Enforces valid Python type hints. Yes
DOC103 Parameter type already in signature Avoids redundant type info. Yes
DOC104 Parameter type mismatch with annotation Ensures consistency with annotations. Yes
DOC105 Duplicated parameter Prevents repetition. Yes
DOC106 Parameter order mismatch with signature Validates parameter order. Yes
DOC107 Missing parameter in docstring Ensures all parameters are documented No

DOC2xx — Returns

Code Description Purpose Enabled by Default
DOC201 Return documented but function has no return statement Detects unnecessary return sections. Yes
DOC202 Invalid return type syntax Enforces valid type expressions. Yes
DOC203 Return type already in signature Avoids redundancy. Yes
DOC204 Return type mismatch with annotation Validates against function annotations. Yes
DOC205 Duplicated return section Prevents duplication. Yes

DOC3xx — Raises

Code Description Purpose Enabled by Default
DOC302 Invalid exception type syntax Ensures valid Python exception syntax. Yes
DOC305 Duplicated exception type Prevents redundant entries. Yes

DOC4xx — Variables

Code Description Purpose Enabled by Default
DOC402 Invalid variable type syntax Enforces valid Python type hints. Yes
DOC403 Variable name contains invalid whitespace Ensures valid identifiers. Yes
DOC405 Duplicated variable Prevents repetition. Yes

How It Works

The tool statically analyzes Python source code using the built-in AST module:

  1. Parses FunctionDef, AsyncFunctionDef, ClassDef, and Module nodes.
  2. Extracts Sphinx-style docstring fields.
  3. Validates structure, syntax, and consistency with annotations.

The tool prints findings to stdout and never modifies source files.

CI Integration:
Treat any output as a failure signal in your build pipeline.


🛠️ Development

To contribute to the project, you can run the following commands for testing and documentation:

First, ensure you have the latest version of pip:

python -m pip install --upgrade pip

Running Tests

pip install --group=test --upgrade # Install test dependencies, skip if already installed
python -m pytest tests/ # Run all tests
python -m pytest tests/ --cov # Run tests with coverage

Running Linter

pip install --group=lint --upgrade  # Install lint dependencies, skip if already installed
ruff check . # Run linter

🗒️ License

This project is licensed under the MIT license._

Metadata

Release files for sphinx-linter 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sphinx-linter 0.1.8
File Size Uploaded
sphinx_linter-0.1.8.tar.gz 27.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sphinx-linter 0.1.8
File Interpreter ABI Platform
sphinx_linter-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 45.7 kB

Release files / sphinx_linter-0.1.8.tar.gz

Download URL sphinx_linter-0.1.8.tar.gz
Size 27.1 kB
Tags Source
SHA-256 checksum
How to use checksums
838e476a5526b3d50b70e84f2d2e402de0dfb71aca59b46ba66bd8fc5b72fcda
BLAKE2b-256 checksum
How to use checksums
23e3115db0ca204a2e94b59bdfafe13bcb31bf680267d02883fec11432fb0765
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / sphinx_linter-0.1.8-py3-none-any.whl

Download URL sphinx_linter-0.1.8-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dffd3bd18f45ed047f96bfc527d9cc761fc66f7705ba7ec89413fe0aa49d0550
BLAKE2b-256 checksum
How to use checksums
e5b1dca463909651aecee651f36a55cca3517e5e9de95ba8b87ee06f149873ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page