Additions to Sybil, the documentation testing tool.
Project description
sybil-extras
Add ons for Sybil.
Installation
$ pip install sybil-extras
Evaluators
MultiEvaluator
"""Use MultiEvaluator to run multiple evaluators on the same parser."""
from sybil import Example, Sybil
from sybil.evaluators.python import PythonEvaluator
from sybil.parsers.rest.codeblock import CodeBlockParser
from sybil.typing import Evaluator
from sybil_extras.evaluators.multi import MultiEvaluator
def _evaluator_1(example: Example) -> None:
"""Check that the example is long enough."""
minimum_length = 50
assert len(example.parsed) >= minimum_length
evaluators: list[Evaluator] = [_evaluator_1, PythonEvaluator()]
multi_evaluator = MultiEvaluator(evaluators=evaluators)
parser = CodeBlockParser(language="python", evaluator=multi_evaluator)
sybil = Sybil(parsers=[parser])
pytest_collect_file = sybil.pytest()
ShellCommandEvaluator
"""Use ShellCommandEvaluator to run shell commands against the code block."""
import sys
from sybil import Sybil
from sybil.parsers.rest.codeblock import CodeBlockParser
from sybil_extras.evaluators.shell_evaluator import ShellCommandEvaluator
evaluator = ShellCommandEvaluator(
args=[sys.executable, "-m", "mypy"],
# The code block is written to a temporary file
# with these suffixes.
tempfile_suffixes=[".example", ".py"],
# Pad the temporary file with newlines so that the
# line numbers in the error messages match the
# line numbers in the source document.
pad_file=True,
# Don't write any changes back to the source document.
# This option is useful when running a linter or formatter
# which modifies the code.
write_to_file=False,
# Use a pseudo-terminal for running commands.
# This can be useful e.g. to get color output, but can also break
# in some environments.
use_pty=True,
)
parser = CodeBlockParser(language="python", evaluator=evaluator)
sybil = Sybil(parsers=[parser])
pytest_collect_file = sybil.pytest()
Parsers
CustomDirectiveSkipParser
"""Use CustomDirectiveSkipParser to skip code blocks with a custom marker."""
from sybil import Sybil
from sybil.parsers.rest.codeblock import PythonCodeBlockParser
# Similar parsers are available at
# sybil_extras.parsers.markdown.custom_directive_skip and
# sybil_extras.parsers.myst.custom_directive_skip.
from sybil_extras.parsers.rest.custom_directive_skip import (
CustomDirectiveSkipParser,
)
skip_parser = CustomDirectiveSkipParser(directive="custom-marker-skip")
code_block_parser = PythonCodeBlockParser()
sybil = Sybil(parsers=[skip_parser, code_block_parser])
pytest_collect_file = sybil.pytest()
This allows you to skip code blocks in the same way as described in the Sybil documentation for skipping examples in reStructuredText, Markdown , and MyST files, but with custom text, e.g. custom-marker-skip replacing the word skip.
GroupedCodeBlockParser
"""Use GroupedCodeBlockParser to group code blocks by a custom directive."""
import sys
from pathlib import Path
from sybil import Sybil
from sybil.example import Example
from sybil.parsers.rest.codeblock import PythonCodeBlockParser
# Similar parsers are available at
# sybil_extras.parsers.markdown.grouped_code_block and
# sybil_extras.parsers.myst.grouped_code_block.
from sybil_extras.parsers.rest.grouped_code_block import GroupedCodeBlockParser
def evaluator(example: Example) -> None:
"""Evaluate the code block by printing it."""
sys.stdout.write(example.parsed)
group_parser = GroupedCodeBlockParser(
directive="group",
evaluator=evaluator,
# Pad the groups with newlines so that the
# line number differences between blocks in the output match the
# line number differences in the source document.
# This is useful for error messages that reference line numbers.
# However, this is detrimental to commands that expect the file
# to not have a bunch of newlines in it, such as formatters.
pad_groups=True,
)
code_block_parser = PythonCodeBlockParser()
sybil = Sybil(parsers=[code_block_parser, group_parser])
document = sybil.parse(path=Path("CHANGELOG.rst"))
for item in document.examples():
# One evaluate call will evaluate a code block with the contents of all
# code blocks in the group.
item.evaluate()
This makes Sybil act as though all of the code blocks within a group are a single code block, to be evaluated with the evaluator given to GroupedCodeBlockParser.
Only code blocks parsed by another parser in the same Sybil instance will be grouped.
A group is defined by a pair of comments, group: start and group: end. The group: end example is expanded to include the contents of the code blocks in the group.
A reStructuredText example:
.. code-block:: python
"""Code block outside the group."""
x = 1
assert x == 1
.. group: start
.. code-block:: python
"""Define a function to use in the next code block."""
import sys
def hello() -> None:
"""Print a greeting."""
sys.stdout.write("Hello, world!")
hello()
.. code-block:: python
"""Run a function which is defined in the previous code block."""
# We don't run ``hello()`` yet - ``doccmd`` does not support groups
.. group: end
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sybil_extras-2025.2.27.1.tar.gz.
File metadata
- Download URL: sybil_extras-2025.2.27.1.tar.gz
- Upload date:
- Size: 32.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
880168bedc5f2b5a20c47fa9346e198e1bc9c7d97a5bf6c45b04c2e38617f7bf
|
|
| MD5 |
c7370acf3cfb75a2bdc532f346ee8bec
|
|
| BLAKE2b-256 |
aca9e97ae81a0678e5fd3660725ab27ef7395ce8c8c7f560b1865897c911dc97
|
Provenance
The following attestation bundles were made for sybil_extras-2025.2.27.1.tar.gz:
Publisher:
release.yml on adamtheturtle/sybil-extras
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sybil_extras-2025.2.27.1.tar.gz -
Subject digest:
880168bedc5f2b5a20c47fa9346e198e1bc9c7d97a5bf6c45b04c2e38617f7bf - Sigstore transparency entry: 175236714
- Sigstore integration time:
-
Permalink:
adamtheturtle/sybil-extras@7af5fc7ed817aad163d11221be4946180f2565b5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7af5fc7ed817aad163d11221be4946180f2565b5 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file sybil_extras-2025.2.27.1-py2.py3-none-any.whl.
File metadata
- Download URL: sybil_extras-2025.2.27.1-py2.py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c2b38c0e2a2a0da45b565325654758477d0231c714bd5c8329603aa0c2b7bd6
|
|
| MD5 |
fb3a3b82d80b61be7afeb7576a5c4c11
|
|
| BLAKE2b-256 |
bc2d13079c1acbef46ae0b8077fc7e09bf786db32416d9a2477572a43b987593
|
Provenance
The following attestation bundles were made for sybil_extras-2025.2.27.1-py2.py3-none-any.whl:
Publisher:
release.yml on adamtheturtle/sybil-extras
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sybil_extras-2025.2.27.1-py2.py3-none-any.whl -
Subject digest:
2c2b38c0e2a2a0da45b565325654758477d0231c714bd5c8329603aa0c2b7bd6 - Sigstore transparency entry: 175236715
- Sigstore integration time:
-
Permalink:
adamtheturtle/sybil-extras@7af5fc7ed817aad163d11221be4946180f2565b5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7af5fc7ed817aad163d11221be4946180f2565b5 -
Trigger Event:
workflow_dispatch
-
Statement type: