-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
61 lines (48 loc) · 2.05 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Doctest configuration."""
from __future__ import annotations
from doctest import ELLIPSIS
from typing import TYPE_CHECKING
from sybil import Sybil
from sybil.evaluators.python import PythonEvaluator, pad
from sybil.parsers.abstract import AbstractCodeBlockParser
from sybil.parsers.rest import DocTestParser, SkipParser
from sybil.parsers.rest.lexers import DirectiveInCommentLexer, DirectiveLexer
if TYPE_CHECKING:
from collections.abc import Sequence
from sybil.typing import Lexer
class PythonCodeBlockParser(AbstractCodeBlockParser):
"""An abstract parser for use when evaluating blocks of code.
:param lexers:
A sequence of :any:`Lexer` objects that will be applied in turn to each
:class:`~sybil.Document` that is parsed. The :class:`~sybil.LexedRegion`
objects returned by these lexers must have both an ``arguments`` string,
containing the language of the lexed region, and a ``source``
:class:`~sybil.Lexeme` containing the source code of the lexed region.
:param 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.
"""
def __init__(
self, lexers: Sequence[Lexer], future_imports: tuple[str, ...] = ()
) -> None:
super().__init__(lexers, "python", PythonEvaluator(future_imports))
pad = staticmethod(pad)
pytest_collect_file = Sybil(
parsers=[
DocTestParser(optionflags=ELLIPSIS),
PythonCodeBlockParser(
lexers=[
DirectiveLexer(directive=r"code-block"),
DirectiveInCommentLexer(
directive=r"(invisible-)?code(-block)?"
), # sybil
DirectiveLexer(directive=r"testsetup"), # sphinx.ext.doctest
DirectiveLexer(directive=r"testcleanup"), # sphinx.ext.doctest
],
future_imports=("annotations",),
),
SkipParser(),
],
patterns=["*.rst", "*.py"],
).pytest()