Skip to main content

Sphinx awesome codelinter

License PyPI version

The awesome codelinter extension for the Sphinx documentation generator lets you verify that code blocks are valid or follow a preferred style. The extension lets you run external tools, such as linters, over all code blocks in your Sphinx documentation.

Install

Install the extension:

pip install sphinxawesome-codelinter

Requirements:

  • Python >= 3.9
  • Sphinx >= 7

Configure

To use this extension, add it to the list of extensions in your Sphinx configuration file conf.py:

extensions += ["sphinxawesome.codelinter"]

To use an external tool to check code blocks, add the language and tool as key-value pairs to the codelinter_languages dictionary. By default, the dictionary is empty, so no code blocks will be processed unless added.

For example, to pass all JSON blocks to Python's built-in JSON module, use:

codelinter_languages = {"json": "python -m json.tool"}

The Python command python -m json.tool returns an error for non-valid JSON code. To lint YAML code blocks, install the yamllint tool and add:

codelinter_languages = {"yaml": "yamllint -"}

The - tells yamllint to read from stdin. You can write your own tools that read from stdin and write to stdout or stderr. They should return 0 if no errors are found, a non-zero value otherwise.

You can use any reStructuredText directive that gets parsed as a literal_block node. For example, you can use .. code-block:: json, .. code:: json, or ..literalinclude:: <filename> to include code from files.

.. literalinclude:: code.json
   :language: json

Use

Run sphinx-build -b codelinter to check your code blocks. If any tool returns a non-zero value, a warning is logged. To turn warnings into errors and stop the build process, use sphinx-build -W.

Bring your own tools

Since the codelinter_languages dictionary accepts any program, you're not limited to existing solutions.

The following example runs a custom linter on Python and C code blocks. It shows how you can achieve the following tasks:

  • Check that Python code passes mypy checks. Mypy doesn't support reading from standard input, so you can't use it directly.
  • Check that C code can compile without warnings.
  • Add C boilerplate code if there's a special comment in the code block.
  • Opt out of linting specific code blocks if there's a special comment.
# File: docs/linters.py
import argparse
import subprocess
import sys

python_code_preface = """
import numpy as np
import customlib
"""


def lint_python(code: str) -> None:
    full_code = python_code_preface + code
    if "# no-check" in full_code:  # skip checking
        return
    subprocess.run(["mypy", "-c", f"{full_code}"], check=True)


def lint_c(code: str) -> None:
    if "// no-check" in code:  # skip checking
        return

    # it is a full example
    if "// full" in code:
        full_code = (
            """
#include <stdlib.h>
#include <stdio.h>
"""
            + code
        )
    else:
        full_code = (
            """
#include <stdlib.h>
#include <stdio.h>

int main() {
"""
            + code
            + """
    return 0;
}
"""
        )
    subprocess.run(
        [
            "gcc",
            "-c",
            "-x",
            "c",
            "-Wall",
            "-Werror",
            "-ansi",
            "-std=c99",
            "-pedantic",
            "-pedantic-errors",
            "-Wno-unused-variable",
            "-Wno-unused-but-set-variable",
            "-Wno-unused-local-typedefs",
            "-o",
            "/dev/null",
            "-",
        ],
        input=full_code.encode(),
        check=True,
    )


def main() -> None:
    parser = argparse.ArgumentParser()

    parser.add_argument("linter", choices=("python", "c"))
    args = parser.parse_args()
    code = sys.stdin.read()

    if args.linter == "python":
        lint_python(code)
    else:
        lint_c(code)


if __name__ == "__main__":
    main()

To use this linter, add the following to your Sphinx configuration:

# File: conf.py
codelinter_languages = {
    "python": "python -m docs.linters python",
    "c": "python -m docs.linters c",
}

Metadata

Release files for sphinxawesome-codelinter 3.0.2

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

Source distribution (sdist)

Source distribution for sphinxawesome-codelinter 3.0.2
File Size Uploaded Provenance Actions
sphinxawesome_codelinter-3.0.2.tar.gz 5.1 kB With provenance Details for sphinxawesome_codelinter-3.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sphinxawesome-codelinter 3.0.2
File Interpreter ABI Platform Actions
sphinxawesome_codelinter-3.0.2-py3-none-any.whl Python 3 none any Details for sphinxawesome_codelinter-3.0.2-py3-none-any.whl

Total release size: 10.5 kB

Release files / sphinxawesome_codelinter-3.0.2.tar.gz

Download URL sphinxawesome_codelinter-3.0.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
95a7d08162ddeff945d2ff5617fe56bcd9b7ed781fc3502651445781fc21ba59
BLAKE2b-256 checksum
How to use checksums
a92f4608360f63cce5405cc5a2d37796c219cf02d2e534a1a5fcf00cf017fe55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / sphinxawesome_codelinter-3.0.2-py3-none-any.whl

Download URL sphinxawesome_codelinter-3.0.2-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2f4e66cc85c189f781fb5009f8b09086fbbfe765d90af54aa592112deb6dfbe
BLAKE2b-256 checksum
How to use checksums
6e3f517520e8e0535af6da1920429ff112116c807fd55e5b80c8bef6ada5b11a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for sphinxawesome-codelinter 3.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sphinxawesome_codelinter-3.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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