Skip to content

Commit 9d57711

Browse files
authored
Add stub converter (#36)
1 parent cb2b362 commit 9d57711

4 files changed

Lines changed: 136 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ By default, all docstrings are required. If you want to be more lenient, you can
7373
args: [--threshold=0.75]
7474
```
7575
76-
If you would like to see suggested docstring templates (inferred from type annotations for functions and methods), provide the `--suggest-changes` argument, along with the docstring style you want to use (options are `google` and `numpydoc`). Here, we ask for [numpydoc-style docstring](https://numpydoc.readthedocs.io/en/latest/format.html#) suggestions:
76+
If you would like to see suggested docstring templates (inferred from type annotations for functions and methods), provide the `--suggest-changes` argument, along with the docstring style you want to use (options are `google`, `numpydoc`, and `stub`). Here, we ask for [numpydoc-style docstring](https://numpydoc.readthedocs.io/en/latest/format.html#) suggestions:
7777

7878
```yaml
7979
- repo: https://github.com/stefmolin/docstringify

src/docstringify/cli.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@
99

1010
from . import __doc__ as pkg_description
1111
from . import __version__
12-
from .converters import GoogleDocstringConverter, NumpydocDocstringConverter
12+
from .converters import (
13+
GoogleDocstringConverter,
14+
NumpydocDocstringConverter,
15+
StubDocstringConverter,
16+
)
1317
from .traversal import DocstringTransformer, DocstringVisitor
1418

1519
if TYPE_CHECKING:
1620
from collections.abc import Sequence
1721

1822

1923
PROG = __package__
20-
STYLES: dict[str, type[GoogleDocstringConverter] | type[NumpydocDocstringConverter]] = {
24+
STYLES: dict[
25+
str,
26+
type[GoogleDocstringConverter]
27+
| type[NumpydocDocstringConverter]
28+
| type[StubDocstringConverter],
29+
] = {
2130
'google': GoogleDocstringConverter,
2231
'numpydoc': NumpydocDocstringConverter,
32+
'stub': StubDocstringConverter,
2333
}
2434
CLI_DEFAULTS = {'threshold': 1.0}
2535

src/docstringify/converters/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from .base import DocstringConverter
44
from .google import GoogleDocstringConverter
55
from .numpydoc import NumpydocDocstringConverter
6+
from .stub import StubDocstringConverter
67

78
__all__ = [
89
'DocstringConverter',
910
'GoogleDocstringConverter',
1011
'NumpydocDocstringConverter',
12+
'StubDocstringConverter',
1113
]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)