|
26 | 26 | # OR OTHER DEALINGS IN THE SOFTWARE. |
27 | 27 | # |
28 | 28 |
|
| 29 | +# stdlib |
| 30 | +import ast |
| 31 | +import inspect |
| 32 | +from types import FunctionType |
| 33 | +from typing import Any, Dict, Optional, Tuple |
| 34 | + |
| 35 | +# 3rd party |
| 36 | +from sphinx.application import Sphinx |
| 37 | +from sphinx.domains import ObjType |
| 38 | +from sphinx.domains.python import PyClasslike, PyXRefRole |
| 39 | +from sphinx.ext.autodoc import FunctionDocumenter, Options |
| 40 | +from sphinx.ext.autodoc.directive import DocumenterBridge |
| 41 | +from sphinx.locale import _ |
| 42 | + |
29 | 43 | __author__: str = "Dominic Davis-Foster" |
30 | 44 | __copyright__: str = "2020 Dominic Davis-Foster" |
31 | 45 | __license__: str = "MIT License" |
32 | 46 | __version__: str = "0.0.0" |
33 | 47 | __email__: str = "dominic@davis-foster.co.uk" |
| 48 | + |
| 49 | +__all__ = ["FixtureDecoratorFinder", "FixtureDocumenter", "is_fixture", "setup"] |
| 50 | + |
| 51 | + |
| 52 | +class FixtureDecoratorFinder(ast.NodeVisitor): |
| 53 | + """ |
| 54 | + :class:`ast.NodeVisitor` for finding pytest fixtures. |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self): |
| 58 | + |
| 59 | + #: Is the function a fixture? |
| 60 | + self.is_fixture = False |
| 61 | + |
| 62 | + #: If it is, the scope of the fixture. |
| 63 | + self.scope = "function" |
| 64 | + |
| 65 | + def visit_FunctionDef(self, node: ast.FunctionDef) -> Any: |
| 66 | + if node.decorator_list: |
| 67 | + for deco in node.decorator_list: |
| 68 | + |
| 69 | + if isinstance(deco, ast.Call): |
| 70 | + keywords: Dict[str, ast.Constant] = {k.arg: k.value for k in deco.keywords} |
| 71 | + |
| 72 | + if "scope" in keywords: |
| 73 | + scope = keywords["scope"] |
| 74 | + if isinstance(scope, ast.Constant): |
| 75 | + self.scope = scope.value |
| 76 | + else: # pragma: no cover |
| 77 | + raise NotImplementedError(type(scope)) |
| 78 | + |
| 79 | + deco = deco.func |
| 80 | + else: |
| 81 | + self.scope = "function" |
| 82 | + |
| 83 | + if isinstance(deco, ast.Name): |
| 84 | + if deco.id == "fixture": |
| 85 | + self.is_fixture = True |
| 86 | + return |
| 87 | + elif isinstance(deco, ast.Attribute): |
| 88 | + if deco.attr == "fixture": |
| 89 | + self.is_fixture = True |
| 90 | + return |
| 91 | + else: # pragma: no cover |
| 92 | + raise NotImplementedError(str(type(deco))) |
| 93 | + |
| 94 | + |
| 95 | +def is_fixture(function: FunctionType) -> Tuple[bool, Optional[str]]: |
| 96 | + """ |
| 97 | + Returns whether the given function is a fixture, and the fixture's scope if it is. |
| 98 | +
|
| 99 | + :param function: |
| 100 | + """ |
| 101 | + |
| 102 | + visitor = FixtureDecoratorFinder() |
| 103 | + |
| 104 | + try: |
| 105 | + visitor.visit(ast.parse(inspect.getsource(function))) |
| 106 | + except IndentationError: |
| 107 | + # Triggered when trying to parse a method |
| 108 | + return False, None |
| 109 | + |
| 110 | + if not visitor.is_fixture: |
| 111 | + return False, None |
| 112 | + |
| 113 | + return True, visitor.scope |
| 114 | + |
| 115 | + |
| 116 | +class FixtureDocumenter(FunctionDocumenter): |
| 117 | + """ |
| 118 | + Sphinx autodoc :class:`~sphinx.ext.autodoc.Documenter` for documenting pytest fixtures. |
| 119 | + """ |
| 120 | + |
| 121 | + objtype = "fixture" |
| 122 | + directivetype = "fixture" |
| 123 | + priority = 20 |
| 124 | + object: FunctionType # noqa: A003 |
| 125 | + |
| 126 | + def __init__(self, directive: DocumenterBridge, name: str, indent: str = '') -> None: |
| 127 | + super().__init__(directive, name, indent) |
| 128 | + self.options = Options(self.options.copy()) |
| 129 | + |
| 130 | + @classmethod |
| 131 | + def can_document_member( |
| 132 | + cls, |
| 133 | + member: Any, |
| 134 | + membername: str, |
| 135 | + isattr: bool, |
| 136 | + parent: Any, |
| 137 | + ) -> bool: |
| 138 | + """ |
| 139 | + Called to see if a member can be documented by this documenter. |
| 140 | +
|
| 141 | + :param member: The member being checked. |
| 142 | + :param membername: The name of the member. |
| 143 | + :param isattr: |
| 144 | + :param parent: The parent of the member. |
| 145 | + """ |
| 146 | + |
| 147 | + if isinstance(member, FunctionType): |
| 148 | + return is_fixture(member)[0] |
| 149 | + else: |
| 150 | + return False |
| 151 | + |
| 152 | + def add_directive_header(self, sig: str = '') -> None: |
| 153 | + """ |
| 154 | + Add the directive's header. |
| 155 | +
|
| 156 | + :param sig: Unused -- fixtures have no useful signature. |
| 157 | + """ |
| 158 | + |
| 159 | + # doc_lines = (self.object.__doc__ or '').splitlines() |
| 160 | + # docstring = StringList([dedent(doc_lines[0]) + dedent("\n".join(doc_lines))[1:]]) |
| 161 | + # print(docstring) |
| 162 | + # input(">>>") |
| 163 | + |
| 164 | + super().add_directive_header('') |
| 165 | + |
| 166 | + self.add_line('', self.get_sourcename()) |
| 167 | + self.add_line( |
| 168 | + f" **Scope:** |nbsp| |nbsp| |nbsp| |nbsp| {is_fixture(self.object)[1]}", self.get_sourcename() |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 173 | + """ |
| 174 | + Setup :mod:`sphinx_autofixture`. |
| 175 | +
|
| 176 | + :param app: The Sphinx app. |
| 177 | + """ |
| 178 | + |
| 179 | + app.registry.domains["py"].object_types["fixture"] = ObjType(_("fixture"), "fixture", "function", "obj") |
| 180 | + app.add_directive_to_domain("py", "fixture", PyClasslike) |
| 181 | + app.add_role_to_domain("py", "fixture", PyXRefRole()) |
| 182 | + |
| 183 | + app.add_autodocumenter(FixtureDocumenter) |
| 184 | + |
| 185 | + return { |
| 186 | + "version": __version__, |
| 187 | + "parallel_read_safe": True, |
| 188 | + } |
0 commit comments