Check for stylistic and formal issues in .rst and .py files included in the documentation
npx @tessl/cli install tessl/pypi-sphinx-lint@1.0.0A command-line tool for checking stylistic and formal issues in reStructuredText (.rst) files and Python documentation files. Based on rstlint.py from CPython, sphinx-lint provides fast linting capabilities designed to find errors that are not visible to sphinx-build, focusing on reStructuredText syntax validation, role and directive checking, and documentation style enforcement.
pip install sphinx-lintimport sphinxlintFor programmatic usage:
from sphinxlint import check_text, check_fileFor CLI functionality:
from sphinxlint.cli import main# Check specific files
sphinx-lint file1.rst file2.py
# Check directory recursively
sphinx-lint docs/
# Enable/disable specific checkers
sphinx-lint --disable trailing-whitespace --enable line-too-long docs/
# Run with parallel processing
sphinx-lint --jobs 4 docs/
# List available checkers
sphinx-lint --listfrom sphinxlint import check_text, check_file
from sphinxlint.checkers import all_checkers
# Check text content
enabled_checkers = set(all_checkers.values())
errors = check_text("example.rst", rst_content, enabled_checkers)
for error in errors:
print(f"{error.filename}:{error.line_no}: {error.msg} ({error.checker_name})")
# Check file
errors = check_file("docs/example.rst", enabled_checkers)Sphinx-lint is organized around a modular checker system:
all_checkers) containing all available checkersThe checker decorator system allows easy registration of new checkers with metadata about supported file types and default enabled state.
Primary API for programmatically checking content and files for reStructuredText and Python documentation issues.
def check_text(filename, text, checkers, options=None):
"""
Check text content for linting errors.
Parameters:
- filename (str): filename for error reporting and checker filtering
- text (str): text content to check
- checkers (set): set of checker functions to run
- options (CheckersOptions, optional): configuration options
Returns:
list of LintError objects
"""
def check_file(filename, checkers, options=None):
"""
Check a file for linting errors.
Parameters:
- filename (str): path to file to check
- checkers (set): set of checker functions to run
- options (CheckersOptions, optional): configuration options
Returns:
list of LintError objects or error message strings
"""Full-featured CLI with support for checker configuration, parallel processing, error sorting, and output formatting.
def main(argv=None):
"""
Main CLI entry point for sphinx-lint.
Parameters:
- argv (list, optional): command-line arguments (defaults to sys.argv)
Returns:
int: exit code (0 for success, non-zero for errors)
"""
def parse_args(argv=None):
"""
Parse command line arguments.
Parameters:
- argv (list, optional): command-line arguments
Returns:
tuple: (enabled_checkers_set, parsed_args_namespace)
"""Built-in checkers for detecting common reStructuredText syntax issues, Python documentation problems, and general formatting issues.
def checker(*suffixes, **kwds):
"""
Decorator to register a function as a checker.
Parameters:
- *suffixes: file extensions this checker supports
- **kwds: checker properties (enabled, rst_only)
Returns:
decorated function with checker metadata
"""
# Global registry of all checker functions
all_checkers = {} # dict mapping checker names to functionsConfiguration system for controlling checker behavior and output formatting.
class CheckersOptions:
"""Configuration options for checkers"""
max_line_length = 80
@classmethod
def from_argparse(cls, namespace):
"""Create options from argparse namespace"""Sphinx-lint uses structured error reporting through the LintError class. All checking functions return lists of error objects or handle file system errors gracefully by returning error messages.
@dataclass(frozen=True)
class LintError:
"""A linting error found by one of the checkers"""
filename: str
line_no: int
msg: str
checker_name: str
def __str__(self):
"""Return formatted error string"""
class CheckersOptions:
"""Configuration options for checkers"""
max_line_length = 80
@classmethod
def from_argparse(cls, namespace):
"""Create CheckersOptions from argparse namespace"""