Check for stylistic and formal issues in .rst and .py files included in the documentation
Primary programmatic interface for checking reStructuredText and Python documentation files for style and syntax issues. These functions form the foundation of sphinx-lint's functionality.
Check text content directly without reading from files, allowing for integration with editors, IDEs, or other text processing tools.
def check_text(filename, text, checkers, options=None):
"""
Check text content for linting errors.
Parameters:
- filename (str): filename for error reporting (affects which checkers run based on extension)
- text (str): text content to check
- checkers (set): set of checker functions to run
- options (CheckersOptions, optional): configuration options (defaults to CheckersOptions() if None)
Returns:
list: List of LintError objects representing found issues
"""from sphinxlint import check_text
from sphinxlint.checkers import all_checkers
# Check reStructuredText content
rst_content = """
Title
=====
This is :func:`function_name` without backticks.
"""
# Use all enabled checkers
enabled_checkers = {checker for checker in all_checkers.values() if checker.enabled}
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 files directly from the filesystem, handling file reading, encoding detection, and special file type processing.
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 (defaults to CheckersOptions() if None)
Returns:
list: List of LintError objects representing found issues, or error message strings for file access issues
"""from sphinxlint import check_file
from sphinxlint.checkers import all_checkers
from sphinxlint.sphinxlint import CheckersOptions
# Check a specific file
enabled_checkers = {checker for checker in all_checkers.values() if checker.enabled}
options = CheckersOptions()
options.max_line_length = 100
errors = check_file("docs/api.rst", enabled_checkers, options)
if errors:
for error in errors:
if isinstance(error, str):
print(f"File error: {error}")
else:
print(f"{error.filename}:{error.line_no}: {error.msg} ({error.checker_name})")
else:
print("No issues found")Both functions accept a set of checker functions. You can filter checkers based on your needs:
from sphinxlint.checkers import all_checkers
# All enabled checkers (default behavior)
enabled_checkers = {checker for checker in all_checkers.values() if checker.enabled}
# Specific checkers only
specific_checkers = {
all_checkers['trailing-whitespace'],
all_checkers['missing-space-after-role']
}
# All checkers (including disabled ones)
all_available_checkers = set(all_checkers.values())
# Filter by file type support
rst_checkers = {checker for checker in all_checkers.values() if '.rst' in checker.suffixes}The checking functions automatically filter checkers based on file extensions:
.rst: reStructuredText files (most checkers).py: Python files (Python syntax checker and general formatters).po: Gettext PO files (converted to RST for checking).html: HTML files (leaked markup checker only)File checking handles various error conditions gracefully:
The functions use UTF-8 encoding by default and will report encoding issues if files cannot be decoded.
Install with Tessl CLI
npx tessl i tessl/pypi-sphinx-lint