API Reference

class sybil.Sybil(parsers: Sequence[Callable[[Document], Iterable[Region]]], pattern: str = None, patterns: Sequence[str] = (), exclude: str = None, excludes: Sequence[str] = (), filenames: Collection[str] = (), path: str = '.', setup: Callable[[dict], None] = None, teardown: Callable[[dict], None] = None, fixtures: Sequence[str] = (), encoding: str = 'utf-8', document_types: Mapping[Optional[str], Type[Document]] = None)

An object to provide test runner integration for discovering examples in documentation and ensuring they are correct.

Parameters:
  • parsers – A sequence of callables. See Parsers.

  • path

    The path in which source files are found, relative to the path of the Python source file in which this class is instantiated. Absolute paths can also be passed.

    Note

    This is ignored when using the pytest integration.

  • pattern – An optional pattern used to match source files that will be parsed for examples.

  • patterns – An optional sequence of patterns used to match source paths that will be parsed for examples.

  • exclude – An optional patterns for source file names that will excluded when looking for examples.

  • excludes – An optional sequence of patterns for source paths that will be excluded when looking for examples.

  • filenames – An optional collection of file names that, if found anywhere within the root path or its sub-directories, will be parsed for examples.

  • setup – An optional callable that will be called once before any examples from a Document are evaluated. If provided, it is called with the document’s namespace.

  • teardown – An optional callable that will be called after all the examples from a Document have been evaluated. If provided, it is called with the document’s namespace.

  • fixtures – An optional sequence of strings specifying the names of fixtures to be requested when using the pytest integration. The fixtures will be inserted into the document’s namespace before any examples for that document are evaluated. All scopes of fixture are supported.

  • encoding – An optional string specifying the encoding to be used when decoding documentation source files.

  • document_types – A mapping of file extension to Document subclass such that custom evaluation can be performed per document type.

pytest()

The helper method for when you use pytest.

unittest()

The helper method for when you use unittest.

class sybil.Region(start: int, end: int, parsed: Parsed, evaluator: Callable[[Example], Optional[str]])

Parsers should yield instances of this class for each example they discover in a documentation source file.

Parameters:
  • start – The character position at which the example starts in the Document.

  • end – The character position at which the example ends in the Document.

  • parsed – The parsed version of the example.

  • evaluator – The callable to use to evaluate this example and check if it is as it should be.

class sybil.Example(document: Document, line: int, column: int, region: Region, namespace: dict)

This represents a particular example from a documentation source file. It is assembled from the Document and Region the example comes from and is passed to the region’s evaluator.

document: Document

The Document from which this example came.

path: str

The absolute path of the Document.

line: int

The line number at which this example occurs in the Document.

column: int

The column number at which this example occurs in the Document.

region: Region

The Region from which this example came.

start: int

The character position at which this example starts in the Document.

end: int

The character position at which this example ends in the Document.

parsed: Parsed

The version of this example provided by the parser that yielded the Region containing it.

namespace: dict

The namespace of the document from which this example came.

Documents

class sybil.Document(text: str, path: str)

This is Sybil’s representation of a documentation source file. It will be instantiated by Sybil and provided to each parser in turn.

Different types of document can be used by subclassing to provide the required evaluation and mapping the required file types using the document_types parameter when instantiating a Sybil.

evaluator: Callable[[Example], Optional[str]] = None

This can be set by evaluators or subclasses to affect the evaluation of future examples. It can be set to a callable that takes an Example. This callable can then do whatever it needs to do, including not executing the example at all, modifying it, or the Document or calling the original evaluator on the example. This last case should always take the form of example.region.evaluator(example).

text: str

This is the text of the documentation source file.

path: str

This is the absolute path of the documentation source file.

namespace: dict

This dictionary is the namespace in which all examples parsed from this document will be evaluated.

classmethod parse(path: str, *parsers: Callable[[Document], Iterable[Region]], encoding: str = 'utf-8') Document

Read the text from the supplied path and parse it into a document using the supplied parsers.

line_column(position: int) str

Return a line and column location in this document based on a byte position.

find_region_sources(start_pattern: Pattern[str], end_pattern: Pattern[str]) Tuple[Match, Match, str]

This helper method can be called used to extract source text for regions based on the two regular expressions provided.

It will yield a tuple of (start_match, end_match, source) for each occurrence of start_pattern in the document’s text that is followed by an occurrence of end_pattern. The matches will be provided as match objects, while the source is provided as a string.

class sybil.document.PythonDocument(text: str, path: str)

A Document type that imports the document’s source file as a Python module, making names within it available in the document’s namespace.

evaluator(example: Example) Optional[str]

capture parser

sybil.parsers.capture.parse_captures(document: Document) Iterable[Region]

A parser function to be included when your documentation makes use of capture examples.

code-block parsers

class sybil.parsers.codeblock.CodeBlockParser(language: str = None, evaluator: Callable[[Example], Optional[str]] = None)

A class to instantiate and include when your documentation makes use of codeblock examples.

Parameters:
  • language – The language that this parser should look for.

  • evaluator – The evaluator to use for evaluating code blocks in the specified language. You can also override the evaluate() below.

pad(source: str, line: int) str

Pad the supplied source such that line numbers will be based on the one provided when the source is evaluated.

class sybil.parsers.codeblock.PythonCodeBlockParser(future_imports=())

A class to instantiate and include when your documentation makes use of Python codeblock examples.

Parameters:

future_imports – An optional list of strings that will be turned into from __future__ import ... statements and prepended to the code in each of the examples found by this parser.

doctest parser

class sybil.parsers.doctest.DocTestParser(optionflags=0)

A class to instantiate and include when your documentation makes use of doctest examples.

Parameters:

optionflagsdoctest option flags to use when evaluating the examples found by this parser.

skip parser

sybil.parsers.skip.skip(document: Document) Iterable[Region]

A parser function to be included when your documentation makes use of skipping examples in a document.