|
| 1 | +"""Stub docstring converter.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from ..components import DESCRIPTION_PLACEHOLDER, Parameter |
| 8 | +from .base import DocstringConverter |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from ..nodes.base import DocstringNode |
| 12 | + from ..nodes.function import FunctionDocstringNode |
| 13 | + |
| 14 | + |
| 15 | +class StubDocstringConverter(DocstringConverter): |
| 16 | + """ |
| 17 | + Class defining the DocstringConverter API for injecting ``__description__`` stubs only. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + quote : bool |
| 22 | + Whether to surround the generated docstrings in triple quotes. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, quote: bool) -> None: |
| 26 | + super().__init__( |
| 27 | + parameters_section_template='', returns_section_template='', quote=quote |
| 28 | + ) |
| 29 | + |
| 30 | + def to_function_docstring( |
| 31 | + self, docstring_node: FunctionDocstringNode, indent: int |
| 32 | + ) -> str: |
| 33 | + """ |
| 34 | + Convert an AST node into a function docstring. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + docstring_node : FunctionDocstringNode |
| 39 | + An instance of :class:`.FunctionDocstringNode`, which wraps an instance of |
| 40 | + :class:`ast.FunctionDef` or :class:`ast.AsyncFunctionDef`, adding additional |
| 41 | + context relevant for Docstringify. |
| 42 | + indent : int |
| 43 | + The number of spaces by which to indent the docstring. |
| 44 | +
|
| 45 | + Returns |
| 46 | + ------- |
| 47 | + str |
| 48 | + The function docstring. |
| 49 | + """ |
| 50 | + return self.format_docstring(DESCRIPTION_PLACEHOLDER, indent=indent) |
| 51 | + |
| 52 | + def format_parameter(self, parameter: Parameter) -> str: |
| 53 | + """ |
| 54 | + Convert a :class:`.Parameter` instance into an entry in the parameters section |
| 55 | + of the docstring. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + parameter : Parameter |
| 60 | + Information on the function parameter, including its name, type, and default |
| 61 | + value (if it has one). |
| 62 | +
|
| 63 | + Returns |
| 64 | + ------- |
| 65 | + str |
| 66 | + An empty string. |
| 67 | + """ |
| 68 | + return '' |
| 69 | + |
| 70 | + def format_return(self, return_type: str | None) -> str: |
| 71 | + """ |
| 72 | + Convert a return type into an entry in the returns section of the docstring. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + return_type : str | None |
| 77 | + The return type name for the function. This will be either a string for the |
| 78 | + name (e.g., ``list[str]``) or ``None``, if ``None`` is returned. |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + str |
| 83 | + An empty string. |
| 84 | + """ |
| 85 | + return '' |
| 86 | + |
| 87 | + def to_module_docstring(self, docstring_node: DocstringNode) -> str: |
| 88 | + """ |
| 89 | + Convert an AST node into a module docstring. |
| 90 | +
|
| 91 | + Parameters |
| 92 | + ---------- |
| 93 | + docstring_node : DocstringNode |
| 94 | + An instance of :class:`.DocstringNode`, which wraps an instance of |
| 95 | + :class:`ast.Module`, adding additional context relevant for Docstringify. |
| 96 | +
|
| 97 | + Returns |
| 98 | + ------- |
| 99 | + str |
| 100 | + The module docstring. |
| 101 | + """ |
| 102 | + return self.format_docstring(DESCRIPTION_PLACEHOLDER, indent=0) |
| 103 | + |
| 104 | + def to_class_docstring(self, docstring_node: DocstringNode, indent: int) -> str: |
| 105 | + """ |
| 106 | + Convert an AST node into a class docstring. |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + docstring_node : DocstringNode |
| 111 | + An instance of :class:`.DocstringNode`, which wraps an instance of |
| 112 | + :class:`ast.ClassDef`, adding additional context relevant for Docstringify. |
| 113 | + indent : int |
| 114 | + The number of spaces by which to indent the docstring. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + str |
| 119 | + The class docstring. |
| 120 | + """ |
| 121 | + return self.format_docstring(DESCRIPTION_PLACEHOLDER, indent=indent) |
0 commit comments