docstring-generator 0.3.4__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docstring_generator-2.0.1/PKG-INFO +341 -0
- docstring_generator-2.0.1/README.md +325 -0
- docstring_generator-2.0.1/pyproject.toml +59 -0
- docstring_generator-2.0.1/setup.cfg +4 -0
- docstring_generator-2.0.1/src/docstring_generator/new_gen_docs.py +109 -0
- docstring_generator-2.0.1/src/docstring_generator/output.py +75 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/PKG-INFO +341 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/SOURCES.txt +11 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/dependency_links.txt +1 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/entry_points.txt +2 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/requires.txt +2 -0
- docstring_generator-2.0.1/src/docstring_generator.egg-info/top_level.txt +1 -0
- docstring-generator-0.3.4/PKG-INFO +0 -36
- docstring-generator-0.3.4/README.md +0 -8
- docstring-generator-0.3.4/docstring_generator/new_gen_docs.py +0 -33
- docstring-generator-0.3.4/pyproject.toml +0 -46
- docstring-generator-0.3.4/setup.py +0 -34
- {docstring-generator-0.3.4 → docstring_generator-2.0.1/src}/docstring_generator/__init__.py +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: docstring-generator
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Auto generate docstring from type-hints.
|
|
5
|
+
Author: FelixTheC
|
|
6
|
+
Project-URL: Repository, https://github.com/FelixTheC/docstring_generator
|
|
7
|
+
Classifier: Environment :: Console
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Topic :: Utilities
|
|
10
|
+
Classifier: Topic :: Software Development :: Documentation
|
|
11
|
+
Classifier: Typing :: Typed
|
|
12
|
+
Requires-Python: >=3.13
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: click==8.4.2
|
|
15
|
+
Requires-Dist: docstring-generator-ext==2.0.2
|
|
16
|
+
|
|
17
|
+
[](https://www.python.org/downloads/)
|
|
18
|
+
[](https://badge.fury.io/py/docstring-generator)
|
|
19
|
+
[](https://github.com/astral-sh/ruff)
|
|
20
|
+
[](https://github.com/astral-sh/uv)
|
|
21
|
+
[](https://pypi.org/project/docstring-generator/)
|
|
22
|
+
|
|
23
|
+
# docstring_generator
|
|
24
|
+
|
|
25
|
+
> **Stop writing boilerplate docstrings by hand.** `docstring_generator` reads your type hints and generates professional, standards-compliant documentation in seconds — keeping your codebase clean, consistent, and AI-ready.
|
|
26
|
+
|
|
27
|
+
Python documentation tooling that automatically generates docstrings for functions and class methods from their type hints, with full support for **NumPy**, **Google**, and **reStructuredText** styles.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Why docstring_generator?
|
|
32
|
+
|
|
33
|
+
Good documentation is no longer optional. AI coding assistants, static analysis tools, and auto-generated API docs all depend on structured, accurate docstrings. Yet writing them by hand is tedious, error-prone, and rarely kept up-to-date.
|
|
34
|
+
|
|
35
|
+
`docstring_generator` solves this by:
|
|
36
|
+
|
|
37
|
+
- ⚡ **Saving hours** — generate docs for an entire codebase in one command
|
|
38
|
+
- 🔄 **Staying in sync** — re-running only updates what changed in the function signature
|
|
39
|
+
- ✍️ **Preserving your words** — existing descriptions and custom notes are never overwritten
|
|
40
|
+
- 🧠 **AI-workflow friendly** — well-structured docstrings improve context quality for LLM-assisted development
|
|
41
|
+
- 🚨 **Exception-aware** — automatically detects `raise` statements and documents them in a `Raises` section, so failure modes are part of your API contract
|
|
42
|
+
- 🏎️ **Blazing fast** — core engine written in C++ via [pybind11](https://github.com/FelixTheC/docstring_generator_ext)
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
One command. Any file or directory:
|
|
49
|
+
|
|
50
|
+
```shell
|
|
51
|
+
pip install docstring-generator
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```shell
|
|
55
|
+
gendocs_new file.py # single file
|
|
56
|
+
gendocs_new mydir/ # entire directory
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
That's it. Your functions now have properly formatted docstrings.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Options
|
|
64
|
+
|
|
65
|
+
### `--style` — Choose your docstring convention
|
|
66
|
+
|
|
67
|
+
| Style | Flag | Description |
|
|
68
|
+
|-------|------|-------------|
|
|
69
|
+
| NumPy | `--style numpy` | Standard in scientific Python (default) |
|
|
70
|
+
| Google | `--style google` | Preferred in many enterprise codebases |
|
|
71
|
+
| reStructuredText | `--style rest` | Compatible with Sphinx auto-documentation |
|
|
72
|
+
|
|
73
|
+
**Default:** `numpy`
|
|
74
|
+
|
|
75
|
+
### `--check` — Docstring coverage report
|
|
76
|
+
|
|
77
|
+
Scan a file or directory and get a coverage overview without modifying anything:
|
|
78
|
+
|
|
79
|
+
```shell
|
|
80
|
+
gendocs_new mydir/ --check
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Outputs a per-file summary showing which functions are documented and which are missing docstrings.
|
|
84
|
+
|
|
85
|
+
### `--strict` — Treat partial docstrings as missing
|
|
86
|
+
|
|
87
|
+
By default, a function with any docstring counts as documented. Strict mode raises the bar:
|
|
88
|
+
|
|
89
|
+
```shell
|
|
90
|
+
gendocs_new mydir/ --check --strict
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
A partial docstring (e.g. missing parameter sections) is treated as undocumented in strict mode.
|
|
94
|
+
|
|
95
|
+
### `--threshold` — Enforce a minimum coverage percentage
|
|
96
|
+
|
|
97
|
+
Fail the check if coverage drops below a given percentage (0–100):
|
|
98
|
+
|
|
99
|
+
```shell
|
|
100
|
+
gendocs_new mydir/ --check --threshold 80
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Useful in CI pipelines to enforce documentation standards across the codebase.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Configuration via `pyproject.toml`
|
|
108
|
+
|
|
109
|
+
Instead of passing flags on every invocation, persist defaults in your project's `pyproject.toml` under the `[tool.docstring_generator]` namespace:
|
|
110
|
+
|
|
111
|
+
```toml
|
|
112
|
+
[tool.docstring_generator]
|
|
113
|
+
strict = true
|
|
114
|
+
threshold = 90
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
CLI flags always override `pyproject.toml` values. The tool automatically walks up from the target path to find the nearest `pyproject.toml`.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Preserve Custom Descriptions with `$<num>` Placeholders
|
|
122
|
+
|
|
123
|
+
Write your domain-specific notes once — `docstring_generator` will place them in the right parameter slot automatically.
|
|
124
|
+
|
|
125
|
+
Use `$1`, `$2`, … in your docstring body to map descriptions to positional parameters:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from typing import List
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def foo(val_a: int, val_b: List[int]):
|
|
132
|
+
"""
|
|
133
|
+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
|
134
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
|
|
135
|
+
|
|
136
|
+
$1 Lorem ipsum dolor sit amet
|
|
137
|
+
$2 nonumy eirmod tempor invidun
|
|
138
|
+
"""
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
After running `gendocs_new` (NumPy style):
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from typing import List
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def foo(val_a: int, val_b: List[int]):
|
|
148
|
+
"""
|
|
149
|
+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
|
150
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
|
|
151
|
+
|
|
152
|
+
Parameters
|
|
153
|
+
----------
|
|
154
|
+
val_a : argument of type int
|
|
155
|
+
Lorem ipsum dolor sit amet
|
|
156
|
+
val_b : argument of type List(int)
|
|
157
|
+
nonumy eirmod tempor invidun
|
|
158
|
+
|
|
159
|
+
"""
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Automatic `Raises` Extraction
|
|
165
|
+
|
|
166
|
+
`docstring_generator` statically analyzes your function body for `raise` statements and adds a `Raises` section describing each exception — including the condition that triggers it. This works seamlessly with frameworks like **Pydantic**, **FastAPI**, or any custom validation logic.
|
|
167
|
+
|
|
168
|
+
### Before
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
class PluginConfig(BaseModel):
|
|
172
|
+
name: str = Field(default="default")
|
|
173
|
+
api_config: dict = Field(default_factory=dict)
|
|
174
|
+
|
|
175
|
+
@field_validator("api_config", mode='before')
|
|
176
|
+
@classmethod
|
|
177
|
+
def validate_api_config(cls, values: dict) -> dict:
|
|
178
|
+
required_key_obj = values.get("required_keys", None)
|
|
179
|
+
if not required_key_obj:
|
|
180
|
+
raise ValueError("The first key must be 'required_keys'")
|
|
181
|
+
if not isinstance(required_key_obj, dict):
|
|
182
|
+
raise ValueError("The 'required_keys' must be a dict")
|
|
183
|
+
return values
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### After running `gendocs_new`
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
class PluginConfig(BaseModel):
|
|
190
|
+
name: str = Field(default="default")
|
|
191
|
+
api_config: dict = Field(default_factory=dict)
|
|
192
|
+
|
|
193
|
+
@field_validator("api_config", mode='before')
|
|
194
|
+
@classmethod
|
|
195
|
+
def validate_api_config(cls, values: dict) -> dict:
|
|
196
|
+
"""
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
cls : [Argument]
|
|
200
|
+
values : dict [Argument]
|
|
201
|
+
|
|
202
|
+
Returns
|
|
203
|
+
-------
|
|
204
|
+
dict
|
|
205
|
+
|
|
206
|
+
Raises
|
|
207
|
+
-------
|
|
208
|
+
ValueError
|
|
209
|
+
If not isinstance(required_key_obj, dict)
|
|
210
|
+
ValueError
|
|
211
|
+
If not required_key_obj
|
|
212
|
+
"""
|
|
213
|
+
required_key_obj = values.get("required_keys", None)
|
|
214
|
+
if not required_key_obj:
|
|
215
|
+
raise ValueError("The first key must be 'required_keys'")
|
|
216
|
+
if not isinstance(required_key_obj, dict):
|
|
217
|
+
raise ValueError("The 'required_keys' must be a dict")
|
|
218
|
+
return values
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Every `raise` — even multiple ones in the same function — is captured, so complex validators document all their failure modes at once.
|
|
222
|
+
|
|
223
|
+
No more hunting through code to find out what a function can throw — it's documented right where it matters.
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Pre-commit Integration
|
|
228
|
+
|
|
229
|
+
Automatically generate docstrings on every commit using the [pre-commit](https://pre-commit.com/) framework.
|
|
230
|
+
|
|
231
|
+
### Setup
|
|
232
|
+
|
|
233
|
+
Add this to your project's `.pre-commit-config.yaml`:
|
|
234
|
+
|
|
235
|
+
```yaml
|
|
236
|
+
repos:
|
|
237
|
+
- repo: https://github.com/FelixTheC/docstring_generator
|
|
238
|
+
rev: v0.3.4 # pin to a release tag
|
|
239
|
+
hooks:
|
|
240
|
+
- id: gendocs
|
|
241
|
+
args: [src/] # directory to process
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Install the hook:
|
|
245
|
+
|
|
246
|
+
```shell
|
|
247
|
+
pip install pre-commit
|
|
248
|
+
pre-commit install
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### How it works
|
|
252
|
+
|
|
253
|
+
The hook runs `gendocs_new` before each commit. If it generates or updates any docstrings, the commit is intentionally stopped so you can review and stage the changes:
|
|
254
|
+
|
|
255
|
+
| Run | What happens | Status |
|
|
256
|
+
|-----|-------------|--------|
|
|
257
|
+
| 1st commit | Hook generates docstrings → files modified | ❌ Stopped (intentional) |
|
|
258
|
+
| `git add` modified files | Stage the generated docstrings | — |
|
|
259
|
+
| 2nd commit | Hook runs, nothing changed | ✅ Passed |
|
|
260
|
+
|
|
261
|
+
This is standard behavior for any auto-fix hook (same as Black or isort).
|
|
262
|
+
|
|
263
|
+
### Customizing the style
|
|
264
|
+
|
|
265
|
+
```yaml
|
|
266
|
+
hooks:
|
|
267
|
+
- id: gendocs
|
|
268
|
+
args: [src/, --style, google]
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## FAQ
|
|
274
|
+
|
|
275
|
+
### What happens if I re-run docstring generation?
|
|
276
|
+
|
|
277
|
+
Nothing is lost. If the function signature hasn't changed, the existing docstring stays untouched. If you add or rename parameters, only the structural part is updated — your custom descriptions are preserved.
|
|
278
|
+
|
|
279
|
+
### Is it safe to use on an existing codebase?
|
|
280
|
+
|
|
281
|
+
Yes. The tool is non-destructive by design. It never deletes content; it only adds or updates parameter sections based on type hints.
|
|
282
|
+
|
|
283
|
+
### Does it work with class methods?
|
|
284
|
+
|
|
285
|
+
Yes — both standalone functions and class methods are fully supported.
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## Examples
|
|
290
|
+
|
|
291
|
+
Ready-to-run examples are available in the [`examples/`](examples/) directory.
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## Installation
|
|
296
|
+
|
|
297
|
+
```shell
|
|
298
|
+
pip install docstring-generator
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
Requires Python 3.10+.
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
## How It Works
|
|
306
|
+
|
|
307
|
+
The core engine is implemented in C++ and exposed to Python via [pybind11](https://github.com/pybind/pybind11), delivering performance that scales to large codebases without slowing down your workflow.
|
|
308
|
+
|
|
309
|
+
- **Extension:** [docstring-generator-ext](https://github.com/FelixTheC/docstring_generator_ext) — the high-performance backbone of this project
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Roadmap
|
|
314
|
+
|
|
315
|
+
Planned features and areas of investment:
|
|
316
|
+
|
|
317
|
+
- [x] Pre-commit hook integration for automatic docstring enforcement
|
|
318
|
+
- [x] Return type documentation generation
|
|
319
|
+
- [x] Raises documentation generation
|
|
320
|
+
- [x] Docstring coverage reporting (`--check`, `--strict`, `--threshold`)
|
|
321
|
+
- [x] `pyproject.toml` configuration support
|
|
322
|
+
- [ ] add `>>` as placeholder for additional return description like `$1`
|
|
323
|
+
- [ ] IDE plugin support (JetBrains, VS Code)
|
|
324
|
+
- [ ] CI/CD pipeline gate (fail build below coverage threshold)
|
|
325
|
+
- [ ] LLM-assisted description generation (opt-in enrichment mode)
|
|
326
|
+
|
|
327
|
+
Community feedback shapes priorities — open an issue to vote on features or suggest new ones.
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## Versioning
|
|
332
|
+
|
|
333
|
+
Follows [Semantic Versioning](https://semver.org/). See the [tags](../../tags) for all available releases.
|
|
334
|
+
|
|
335
|
+
## Authors
|
|
336
|
+
|
|
337
|
+
- **Felix Eisenmenger** — creator & maintainer
|
|
338
|
+
|
|
339
|
+
## License
|
|
340
|
+
|
|
341
|
+
MIT License — free to use in personal and commercial projects.
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
[](https://www.python.org/downloads/)
|
|
2
|
+
[](https://badge.fury.io/py/docstring-generator)
|
|
3
|
+
[](https://github.com/astral-sh/ruff)
|
|
4
|
+
[](https://github.com/astral-sh/uv)
|
|
5
|
+
[](https://pypi.org/project/docstring-generator/)
|
|
6
|
+
|
|
7
|
+
# docstring_generator
|
|
8
|
+
|
|
9
|
+
> **Stop writing boilerplate docstrings by hand.** `docstring_generator` reads your type hints and generates professional, standards-compliant documentation in seconds — keeping your codebase clean, consistent, and AI-ready.
|
|
10
|
+
|
|
11
|
+
Python documentation tooling that automatically generates docstrings for functions and class methods from their type hints, with full support for **NumPy**, **Google**, and **reStructuredText** styles.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Why docstring_generator?
|
|
16
|
+
|
|
17
|
+
Good documentation is no longer optional. AI coding assistants, static analysis tools, and auto-generated API docs all depend on structured, accurate docstrings. Yet writing them by hand is tedious, error-prone, and rarely kept up-to-date.
|
|
18
|
+
|
|
19
|
+
`docstring_generator` solves this by:
|
|
20
|
+
|
|
21
|
+
- ⚡ **Saving hours** — generate docs for an entire codebase in one command
|
|
22
|
+
- 🔄 **Staying in sync** — re-running only updates what changed in the function signature
|
|
23
|
+
- ✍️ **Preserving your words** — existing descriptions and custom notes are never overwritten
|
|
24
|
+
- 🧠 **AI-workflow friendly** — well-structured docstrings improve context quality for LLM-assisted development
|
|
25
|
+
- 🚨 **Exception-aware** — automatically detects `raise` statements and documents them in a `Raises` section, so failure modes are part of your API contract
|
|
26
|
+
- 🏎️ **Blazing fast** — core engine written in C++ via [pybind11](https://github.com/FelixTheC/docstring_generator_ext)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
One command. Any file or directory:
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
pip install docstring-generator
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
gendocs_new file.py # single file
|
|
40
|
+
gendocs_new mydir/ # entire directory
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
That's it. Your functions now have properly formatted docstrings.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Options
|
|
48
|
+
|
|
49
|
+
### `--style` — Choose your docstring convention
|
|
50
|
+
|
|
51
|
+
| Style | Flag | Description |
|
|
52
|
+
|-------|------|-------------|
|
|
53
|
+
| NumPy | `--style numpy` | Standard in scientific Python (default) |
|
|
54
|
+
| Google | `--style google` | Preferred in many enterprise codebases |
|
|
55
|
+
| reStructuredText | `--style rest` | Compatible with Sphinx auto-documentation |
|
|
56
|
+
|
|
57
|
+
**Default:** `numpy`
|
|
58
|
+
|
|
59
|
+
### `--check` — Docstring coverage report
|
|
60
|
+
|
|
61
|
+
Scan a file or directory and get a coverage overview without modifying anything:
|
|
62
|
+
|
|
63
|
+
```shell
|
|
64
|
+
gendocs_new mydir/ --check
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Outputs a per-file summary showing which functions are documented and which are missing docstrings.
|
|
68
|
+
|
|
69
|
+
### `--strict` — Treat partial docstrings as missing
|
|
70
|
+
|
|
71
|
+
By default, a function with any docstring counts as documented. Strict mode raises the bar:
|
|
72
|
+
|
|
73
|
+
```shell
|
|
74
|
+
gendocs_new mydir/ --check --strict
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
A partial docstring (e.g. missing parameter sections) is treated as undocumented in strict mode.
|
|
78
|
+
|
|
79
|
+
### `--threshold` — Enforce a minimum coverage percentage
|
|
80
|
+
|
|
81
|
+
Fail the check if coverage drops below a given percentage (0–100):
|
|
82
|
+
|
|
83
|
+
```shell
|
|
84
|
+
gendocs_new mydir/ --check --threshold 80
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Useful in CI pipelines to enforce documentation standards across the codebase.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Configuration via `pyproject.toml`
|
|
92
|
+
|
|
93
|
+
Instead of passing flags on every invocation, persist defaults in your project's `pyproject.toml` under the `[tool.docstring_generator]` namespace:
|
|
94
|
+
|
|
95
|
+
```toml
|
|
96
|
+
[tool.docstring_generator]
|
|
97
|
+
strict = true
|
|
98
|
+
threshold = 90
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
CLI flags always override `pyproject.toml` values. The tool automatically walks up from the target path to find the nearest `pyproject.toml`.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Preserve Custom Descriptions with `$<num>` Placeholders
|
|
106
|
+
|
|
107
|
+
Write your domain-specific notes once — `docstring_generator` will place them in the right parameter slot automatically.
|
|
108
|
+
|
|
109
|
+
Use `$1`, `$2`, … in your docstring body to map descriptions to positional parameters:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from typing import List
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def foo(val_a: int, val_b: List[int]):
|
|
116
|
+
"""
|
|
117
|
+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
|
118
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
|
|
119
|
+
|
|
120
|
+
$1 Lorem ipsum dolor sit amet
|
|
121
|
+
$2 nonumy eirmod tempor invidun
|
|
122
|
+
"""
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
After running `gendocs_new` (NumPy style):
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from typing import List
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def foo(val_a: int, val_b: List[int]):
|
|
132
|
+
"""
|
|
133
|
+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
|
|
134
|
+
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
val_a : argument of type int
|
|
139
|
+
Lorem ipsum dolor sit amet
|
|
140
|
+
val_b : argument of type List(int)
|
|
141
|
+
nonumy eirmod tempor invidun
|
|
142
|
+
|
|
143
|
+
"""
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Automatic `Raises` Extraction
|
|
149
|
+
|
|
150
|
+
`docstring_generator` statically analyzes your function body for `raise` statements and adds a `Raises` section describing each exception — including the condition that triggers it. This works seamlessly with frameworks like **Pydantic**, **FastAPI**, or any custom validation logic.
|
|
151
|
+
|
|
152
|
+
### Before
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
class PluginConfig(BaseModel):
|
|
156
|
+
name: str = Field(default="default")
|
|
157
|
+
api_config: dict = Field(default_factory=dict)
|
|
158
|
+
|
|
159
|
+
@field_validator("api_config", mode='before')
|
|
160
|
+
@classmethod
|
|
161
|
+
def validate_api_config(cls, values: dict) -> dict:
|
|
162
|
+
required_key_obj = values.get("required_keys", None)
|
|
163
|
+
if not required_key_obj:
|
|
164
|
+
raise ValueError("The first key must be 'required_keys'")
|
|
165
|
+
if not isinstance(required_key_obj, dict):
|
|
166
|
+
raise ValueError("The 'required_keys' must be a dict")
|
|
167
|
+
return values
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### After running `gendocs_new`
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
class PluginConfig(BaseModel):
|
|
174
|
+
name: str = Field(default="default")
|
|
175
|
+
api_config: dict = Field(default_factory=dict)
|
|
176
|
+
|
|
177
|
+
@field_validator("api_config", mode='before')
|
|
178
|
+
@classmethod
|
|
179
|
+
def validate_api_config(cls, values: dict) -> dict:
|
|
180
|
+
"""
|
|
181
|
+
Parameters
|
|
182
|
+
----------
|
|
183
|
+
cls : [Argument]
|
|
184
|
+
values : dict [Argument]
|
|
185
|
+
|
|
186
|
+
Returns
|
|
187
|
+
-------
|
|
188
|
+
dict
|
|
189
|
+
|
|
190
|
+
Raises
|
|
191
|
+
-------
|
|
192
|
+
ValueError
|
|
193
|
+
If not isinstance(required_key_obj, dict)
|
|
194
|
+
ValueError
|
|
195
|
+
If not required_key_obj
|
|
196
|
+
"""
|
|
197
|
+
required_key_obj = values.get("required_keys", None)
|
|
198
|
+
if not required_key_obj:
|
|
199
|
+
raise ValueError("The first key must be 'required_keys'")
|
|
200
|
+
if not isinstance(required_key_obj, dict):
|
|
201
|
+
raise ValueError("The 'required_keys' must be a dict")
|
|
202
|
+
return values
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Every `raise` — even multiple ones in the same function — is captured, so complex validators document all their failure modes at once.
|
|
206
|
+
|
|
207
|
+
No more hunting through code to find out what a function can throw — it's documented right where it matters.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## Pre-commit Integration
|
|
212
|
+
|
|
213
|
+
Automatically generate docstrings on every commit using the [pre-commit](https://pre-commit.com/) framework.
|
|
214
|
+
|
|
215
|
+
### Setup
|
|
216
|
+
|
|
217
|
+
Add this to your project's `.pre-commit-config.yaml`:
|
|
218
|
+
|
|
219
|
+
```yaml
|
|
220
|
+
repos:
|
|
221
|
+
- repo: https://github.com/FelixTheC/docstring_generator
|
|
222
|
+
rev: v0.3.4 # pin to a release tag
|
|
223
|
+
hooks:
|
|
224
|
+
- id: gendocs
|
|
225
|
+
args: [src/] # directory to process
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Install the hook:
|
|
229
|
+
|
|
230
|
+
```shell
|
|
231
|
+
pip install pre-commit
|
|
232
|
+
pre-commit install
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### How it works
|
|
236
|
+
|
|
237
|
+
The hook runs `gendocs_new` before each commit. If it generates or updates any docstrings, the commit is intentionally stopped so you can review and stage the changes:
|
|
238
|
+
|
|
239
|
+
| Run | What happens | Status |
|
|
240
|
+
|-----|-------------|--------|
|
|
241
|
+
| 1st commit | Hook generates docstrings → files modified | ❌ Stopped (intentional) |
|
|
242
|
+
| `git add` modified files | Stage the generated docstrings | — |
|
|
243
|
+
| 2nd commit | Hook runs, nothing changed | ✅ Passed |
|
|
244
|
+
|
|
245
|
+
This is standard behavior for any auto-fix hook (same as Black or isort).
|
|
246
|
+
|
|
247
|
+
### Customizing the style
|
|
248
|
+
|
|
249
|
+
```yaml
|
|
250
|
+
hooks:
|
|
251
|
+
- id: gendocs
|
|
252
|
+
args: [src/, --style, google]
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## FAQ
|
|
258
|
+
|
|
259
|
+
### What happens if I re-run docstring generation?
|
|
260
|
+
|
|
261
|
+
Nothing is lost. If the function signature hasn't changed, the existing docstring stays untouched. If you add or rename parameters, only the structural part is updated — your custom descriptions are preserved.
|
|
262
|
+
|
|
263
|
+
### Is it safe to use on an existing codebase?
|
|
264
|
+
|
|
265
|
+
Yes. The tool is non-destructive by design. It never deletes content; it only adds or updates parameter sections based on type hints.
|
|
266
|
+
|
|
267
|
+
### Does it work with class methods?
|
|
268
|
+
|
|
269
|
+
Yes — both standalone functions and class methods are fully supported.
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Examples
|
|
274
|
+
|
|
275
|
+
Ready-to-run examples are available in the [`examples/`](examples/) directory.
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## Installation
|
|
280
|
+
|
|
281
|
+
```shell
|
|
282
|
+
pip install docstring-generator
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Requires Python 3.10+.
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## How It Works
|
|
290
|
+
|
|
291
|
+
The core engine is implemented in C++ and exposed to Python via [pybind11](https://github.com/pybind/pybind11), delivering performance that scales to large codebases without slowing down your workflow.
|
|
292
|
+
|
|
293
|
+
- **Extension:** [docstring-generator-ext](https://github.com/FelixTheC/docstring_generator_ext) — the high-performance backbone of this project
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Roadmap
|
|
298
|
+
|
|
299
|
+
Planned features and areas of investment:
|
|
300
|
+
|
|
301
|
+
- [x] Pre-commit hook integration for automatic docstring enforcement
|
|
302
|
+
- [x] Return type documentation generation
|
|
303
|
+
- [x] Raises documentation generation
|
|
304
|
+
- [x] Docstring coverage reporting (`--check`, `--strict`, `--threshold`)
|
|
305
|
+
- [x] `pyproject.toml` configuration support
|
|
306
|
+
- [ ] add `>>` as placeholder for additional return description like `$1`
|
|
307
|
+
- [ ] IDE plugin support (JetBrains, VS Code)
|
|
308
|
+
- [ ] CI/CD pipeline gate (fail build below coverage threshold)
|
|
309
|
+
- [ ] LLM-assisted description generation (opt-in enrichment mode)
|
|
310
|
+
|
|
311
|
+
Community feedback shapes priorities — open an issue to vote on features or suggest new ones.
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## Versioning
|
|
316
|
+
|
|
317
|
+
Follows [Semantic Versioning](https://semver.org/). See the [tags](../../tags) for all available releases.
|
|
318
|
+
|
|
319
|
+
## Authors
|
|
320
|
+
|
|
321
|
+
- **Felix Eisenmenger** — creator & maintainer
|
|
322
|
+
|
|
323
|
+
## License
|
|
324
|
+
|
|
325
|
+
MIT License — free to use in personal and commercial projects.
|