docstringify 0.7.0__tar.gz → 1.1.0__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.
Files changed (46) hide show
  1. {docstringify-0.7.0 → docstringify-1.1.0}/.github/dependabot.yml +4 -0
  2. {docstringify-0.7.0 → docstringify-1.1.0}/.github/workflows/ci.yml +4 -2
  3. {docstringify-0.7.0 → docstringify-1.1.0}/.pre-commit-config.yaml +10 -10
  4. docstringify-1.1.0/.python-version +1 -0
  5. {docstringify-0.7.0 → docstringify-1.1.0}/CONTRIBUTING.md +1 -1
  6. {docstringify-0.7.0 → docstringify-1.1.0}/PKG-INFO +33 -40
  7. {docstringify-0.7.0 → docstringify-1.1.0}/README.md +28 -8
  8. {docstringify-0.7.0 → docstringify-1.1.0}/pyproject.toml +16 -24
  9. docstringify-1.1.0/src/docstringify/__init__.py +10 -0
  10. docstringify-1.1.0/src/docstringify/components.py +47 -0
  11. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/converters/base.py +13 -6
  12. docstringify-1.1.0/src/docstringify/exceptions.py +25 -0
  13. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/nodes/base.py +42 -0
  14. docstringify-1.1.0/src/docstringify/nodes/function.py +344 -0
  15. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/traversal/transformer.py +81 -14
  16. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/traversal/visitor.py +8 -8
  17. docstringify-1.1.0/uv.lock +423 -0
  18. docstringify-0.7.0/setup.cfg +0 -4
  19. docstringify-0.7.0/src/docstringify/__init__.py +0 -6
  20. docstringify-0.7.0/src/docstringify/components.py +0 -22
  21. docstringify-0.7.0/src/docstringify/exceptions.py +0 -6
  22. docstringify-0.7.0/src/docstringify/nodes/function.py +0 -178
  23. docstringify-0.7.0/src/docstringify.egg-info/PKG-INFO +0 -223
  24. docstringify-0.7.0/src/docstringify.egg-info/SOURCES.txt +0 -38
  25. docstringify-0.7.0/src/docstringify.egg-info/dependency_links.txt +0 -1
  26. docstringify-0.7.0/src/docstringify.egg-info/entry_points.txt +0 -2
  27. docstringify-0.7.0/src/docstringify.egg-info/requires.txt +0 -6
  28. docstringify-0.7.0/src/docstringify.egg-info/top_level.txt +0 -1
  29. {docstringify-0.7.0 → docstringify-1.1.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  30. {docstringify-0.7.0 → docstringify-1.1.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  31. {docstringify-0.7.0 → docstringify-1.1.0}/.github/pull_request_template.md +0 -0
  32. {docstringify-0.7.0 → docstringify-1.1.0}/.github/release.yml +0 -0
  33. {docstringify-0.7.0 → docstringify-1.1.0}/.github/workflows/greetings.yml +0 -0
  34. {docstringify-0.7.0 → docstringify-1.1.0}/.github/workflows/pypi-publish.yml +0 -0
  35. {docstringify-0.7.0 → docstringify-1.1.0}/.gitignore +0 -0
  36. {docstringify-0.7.0 → docstringify-1.1.0}/.pre-commit-hooks.yaml +0 -0
  37. {docstringify-0.7.0 → docstringify-1.1.0}/CODE_OF_CONDUCT.md +0 -0
  38. {docstringify-0.7.0 → docstringify-1.1.0}/LICENSE +0 -0
  39. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/__main__.py +0 -0
  40. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/cli.py +0 -0
  41. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/converters/__init__.py +0 -0
  42. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/converters/google.py +0 -0
  43. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/converters/numpydoc.py +0 -0
  44. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/converters/stub.py +0 -0
  45. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/nodes/__init__.py +0 -0
  46. {docstringify-0.7.0 → docstringify-1.1.0}/src/docstringify/traversal/__init__.py +0 -0
@@ -9,3 +9,7 @@ updates:
9
9
  schedule:
10
10
  # Check for updates to GitHub Actions every month
11
11
  interval: "monthly"
12
+ groups:
13
+ actions:
14
+ patterns:
15
+ - "*"
@@ -22,6 +22,8 @@ on:
22
22
  - '!.github/**'
23
23
  - '.github/workflows/ci.yml'
24
24
 
25
+ workflow_dispatch:
26
+
25
27
  concurrency:
26
28
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
27
29
  cancel-in-progress: true
@@ -60,8 +62,8 @@ jobs:
60
62
  - name: Check entry point
61
63
  run: docstringify --version
62
64
 
63
- - name: Install testing dependencies
64
- run: python -m pip install '.[dev]'
65
+ - name: Install dev dependencies
66
+ run: python -m pip install --group dev
65
67
 
66
68
  - name: Validate pre-commit hook
67
69
  run: pre-commit try-repo . docstringify
@@ -11,27 +11,27 @@ repos:
11
11
  - id: end-of-file-fixer
12
12
  - id: trailing-whitespace
13
13
 
14
+ - repo: https://github.com/numpy/numpydoc
15
+ rev: v1.9.0
16
+ hooks:
17
+ - id: numpydoc-validation
18
+ exclude: (tests|docs)/.*
19
+
14
20
  - repo: https://github.com/stefmolin/docstringify
15
- rev: 0.6.0
21
+ rev: 1.0.0
16
22
  hooks:
17
23
  - id: docstringify
18
24
  args: [--make-changes-inplace=numpydoc]
19
25
 
20
26
  - repo: https://github.com/astral-sh/ruff-pre-commit
21
- rev: v0.11.9
27
+ rev: v0.12.2
22
28
  hooks:
23
- - id: ruff
29
+ - id: ruff-check
24
30
  args: [--fix, --exit-non-zero-on-fix, --show-fixes]
25
31
  - id: ruff-format
26
32
 
27
33
  - repo: https://github.com/tox-dev/pyproject-fmt
28
- rev: v2.5.1
34
+ rev: v2.6.0
29
35
  hooks:
30
36
  - id: pyproject-fmt
31
37
  args: [--keep-full-version, --no-print-diff]
32
-
33
- - repo: https://github.com/numpy/numpydoc
34
- rev: v1.8.0
35
- hooks:
36
- - id: numpydoc-validation
37
- exclude: (tests|docs)/.*
@@ -0,0 +1 @@
1
+ 3.13
@@ -9,7 +9,7 @@ Open up an issue describing in detail the bug or feature request you are proposi
9
9
  Once you have been given the go ahead, you can start working on the code. Start by forking the project, cloning locally, and then creating a branch to work on. You will need to then install main dependencies as well as the `dev` dependencies, which can be done by running the following command:
10
10
 
11
11
  ```shell
12
- $ pip install -e '.[dev]'
12
+ $ pip install -e . --group dev
13
13
  ```
14
14
 
15
15
  Set up the pre-commit hooks to make sure you can pass the CI checks:
@@ -1,35 +1,15 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docstringify
3
- Version: 0.7.0
3
+ Version: 1.1.0
4
4
  Summary: Flag missing docstrings and, optionally, generate them from signatures and type annotations.
5
- Author-email: Stefanie Molin <docstringify@stefaniemolin.com>
6
- License: MIT License
7
-
8
- Copyright (c) 2025 Stefanie Molin
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in all
18
- copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
- SOFTWARE.
27
-
28
5
  Project-URL: Documentation, https://github.com/stefmolin/docstringify
29
6
  Project-URL: Homepage, https://github.com/stefmolin/docstringify
30
7
  Project-URL: Source, https://github.com/stefmolin/docstringify
8
+ Author-email: Stefanie Molin <docstringify@stefaniemolin.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
31
11
  Keywords: ast,docstring generation,google-style docstring,numpydoc-style docstring,pre-commit
32
- Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Development Status :: 4 - Beta
33
13
  Classifier: Programming Language :: Python
34
14
  Classifier: Programming Language :: Python :: 3 :: Only
35
15
  Classifier: Programming Language :: Python :: 3.9
@@ -39,13 +19,6 @@ Classifier: Programming Language :: Python :: 3.12
39
19
  Classifier: Programming Language :: Python :: 3.13
40
20
  Requires-Python: >=3.9
41
21
  Description-Content-Type: text/markdown
42
- License-File: LICENSE
43
- Provides-Extra: dev
44
- Requires-Dist: pre-commit; extra == "dev"
45
- Requires-Dist: pytest; extra == "dev"
46
- Requires-Dist: pytest-cov; extra == "dev"
47
- Requires-Dist: pytest-randomly; extra == "dev"
48
- Dynamic: license-file
49
22
 
50
23
  # Docstringify
51
24
  Flag missing docstrings and, optionally, generate them from signatures and type annotations.
@@ -103,11 +76,11 @@ You can use Docstringify in three modes:
103
76
 
104
77
  ### Pre-commit hook
105
78
 
106
- Add the following to your `.pre-commit-config.yaml` file to block commits with missing docstrings:
79
+ Add the following to your `.pre-commit-config.yaml` file to block commits with missing docstrings before any formatters like `ruff`:
107
80
 
108
81
  ```yaml
109
82
  - repo: https://github.com/stefmolin/docstringify
110
- rev: 0.7.0
83
+ rev: 1.1.0
111
84
  hooks:
112
85
  - id: docstringify
113
86
  ```
@@ -116,17 +89,17 @@ By default, all docstrings are required. If you want to be more lenient, you can
116
89
 
117
90
  ```yaml
118
91
  - repo: https://github.com/stefmolin/docstringify
119
- rev: 0.7.0
92
+ rev: 1.1.0
120
93
  hooks:
121
94
  - id: docstringify
122
95
  args: [--threshold=0.75]
123
96
  ```
124
97
 
125
- 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:
98
+ 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 stub suggestions (just single lines of `"""__description__"""`):
126
99
 
127
100
  ```yaml
128
101
  - repo: https://github.com/stefmolin/docstringify
129
- rev: 0.7.0
102
+ rev: 1.1.0
130
103
  hooks:
131
104
  - id: docstringify
132
105
  args: [--suggest-changes=numpydoc]
@@ -136,13 +109,21 @@ Use `--make-changes` to create a copy of each file with docstring templates. Her
136
109
 
137
110
  ```yaml
138
111
  - repo: https://github.com/stefmolin/docstringify
139
- rev: 0.7.0
112
+ rev: 1.1.0
140
113
  hooks:
141
114
  - id: docstringify
142
115
  args: [--make-changes=google]
143
116
  ```
144
117
 
145
- If you want the changes to be made in place, change `--make-changes` to `--make-changes-inplace` &ndash; make sure you only operate on files that are in version control with this setting. Note that the resulting format of your file may be a little different (spacing, newlines, *etc.*).
118
+ If you want the changes to be made in place, change `--make-changes` to `--make-changes-inplace` &ndash; make sure you only operate on files that are in version control with this setting. Here, we ask for [numpydoc-style docstring](https://numpydoc.readthedocs.io/en/latest/format.html#) suggestions:
119
+
120
+ ```yaml
121
+ - repo: https://github.com/stefmolin/docstringify
122
+ rev: 1.1.0
123
+ hooks:
124
+ - id: docstringify
125
+ args: [--make-changes-inplace=numpydoc]
126
+ ```
146
127
 
147
128
  Be sure to check out the [pre-commit documentation](https://pre-commit.com/#pre-commit-configyaml---hooks) for additional configuration options.
148
129
 
@@ -216,7 +197,19 @@ test.say_hello is missing a docstring
216
197
  Docstring templates written to /.../test_docstringify.py
217
198
  ```
218
199
 
219
- If you want to overwrite the file with the edits, pass `overwrite=True` to `DocstringTransformer()`.
200
+ If you want to overwrite the file with the edits, pass `overwrite=True` to `DocstringTransformer()`:
201
+
202
+ ```pycon
203
+ >>> from docstringify.converters import GoogleDocstringConverter
204
+ >>> from docstringify.traversal import DocstringTransformer
205
+ >>> transformer = DocstringTransformer(
206
+ ... 'test.py', converter=GoogleDocstringConverter, overwrite=True
207
+ ... )
208
+ >>> transformer.process_file()
209
+ test is missing a docstring
210
+ test.say_hello is missing a docstring
211
+ Docstring templates written to /.../test.py
212
+ ```
220
213
 
221
214
  ## Contributing
222
215
 
@@ -54,11 +54,11 @@ You can use Docstringify in three modes:
54
54
 
55
55
  ### Pre-commit hook
56
56
 
57
- Add the following to your `.pre-commit-config.yaml` file to block commits with missing docstrings:
57
+ Add the following to your `.pre-commit-config.yaml` file to block commits with missing docstrings before any formatters like `ruff`:
58
58
 
59
59
  ```yaml
60
60
  - repo: https://github.com/stefmolin/docstringify
61
- rev: 0.7.0
61
+ rev: 1.1.0
62
62
  hooks:
63
63
  - id: docstringify
64
64
  ```
@@ -67,17 +67,17 @@ By default, all docstrings are required. If you want to be more lenient, you can
67
67
 
68
68
  ```yaml
69
69
  - repo: https://github.com/stefmolin/docstringify
70
- rev: 0.7.0
70
+ rev: 1.1.0
71
71
  hooks:
72
72
  - id: docstringify
73
73
  args: [--threshold=0.75]
74
74
  ```
75
75
 
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:
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 stub suggestions (just single lines of `"""__description__"""`):
77
77
 
78
78
  ```yaml
79
79
  - repo: https://github.com/stefmolin/docstringify
80
- rev: 0.7.0
80
+ rev: 1.1.0
81
81
  hooks:
82
82
  - id: docstringify
83
83
  args: [--suggest-changes=numpydoc]
@@ -87,13 +87,21 @@ Use `--make-changes` to create a copy of each file with docstring templates. Her
87
87
 
88
88
  ```yaml
89
89
  - repo: https://github.com/stefmolin/docstringify
90
- rev: 0.7.0
90
+ rev: 1.1.0
91
91
  hooks:
92
92
  - id: docstringify
93
93
  args: [--make-changes=google]
94
94
  ```
95
95
 
96
- If you want the changes to be made in place, change `--make-changes` to `--make-changes-inplace` &ndash; make sure you only operate on files that are in version control with this setting. Note that the resulting format of your file may be a little different (spacing, newlines, *etc.*).
96
+ If you want the changes to be made in place, change `--make-changes` to `--make-changes-inplace` &ndash; make sure you only operate on files that are in version control with this setting. Here, we ask for [numpydoc-style docstring](https://numpydoc.readthedocs.io/en/latest/format.html#) suggestions:
97
+
98
+ ```yaml
99
+ - repo: https://github.com/stefmolin/docstringify
100
+ rev: 1.1.0
101
+ hooks:
102
+ - id: docstringify
103
+ args: [--make-changes-inplace=numpydoc]
104
+ ```
97
105
 
98
106
  Be sure to check out the [pre-commit documentation](https://pre-commit.com/#pre-commit-configyaml---hooks) for additional configuration options.
99
107
 
@@ -167,7 +175,19 @@ test.say_hello is missing a docstring
167
175
  Docstring templates written to /.../test_docstringify.py
168
176
  ```
169
177
 
170
- If you want to overwrite the file with the edits, pass `overwrite=True` to `DocstringTransformer()`.
178
+ If you want to overwrite the file with the edits, pass `overwrite=True` to `DocstringTransformer()`:
179
+
180
+ ```pycon
181
+ >>> from docstringify.converters import GoogleDocstringConverter
182
+ >>> from docstringify.traversal import DocstringTransformer
183
+ >>> transformer = DocstringTransformer(
184
+ ... 'test.py', converter=GoogleDocstringConverter, overwrite=True
185
+ ... )
186
+ >>> transformer.process_file()
187
+ test is missing a docstring
188
+ test.say_hello is missing a docstring
189
+ Docstring templates written to /.../test.py
190
+ ```
171
191
 
172
192
  ## Contributing
173
193
 
@@ -1,13 +1,11 @@
1
1
  [build-system]
2
- build-backend = "setuptools.build_meta"
2
+ build-backend = "hatchling.build"
3
3
 
4
- requires = [
5
- "setuptools",
6
- "setuptools-scm",
7
- ]
4
+ requires = [ "hatchling" ]
8
5
 
9
6
  [project]
10
7
  name = "docstringify"
8
+ version = "1.1.0"
11
9
  description = "Flag missing docstrings and, optionally, generate them from signatures and type annotations."
12
10
  readme = "README.md"
13
11
  keywords = [
@@ -17,13 +15,14 @@ keywords = [
17
15
  "numpydoc-style docstring",
18
16
  "pre-commit",
19
17
  ]
20
- license = { file = "LICENSE" }
18
+ license = "MIT"
19
+ license-files = [ "LICENSE" ]
21
20
  authors = [
22
21
  { name = "Stefanie Molin", email = "docstringify@stefaniemolin.com" },
23
22
  ]
24
23
  requires-python = ">=3.9"
25
24
  classifiers = [
26
- "Development Status :: 3 - Alpha",
25
+ "Development Status :: 4 - Beta",
27
26
  "Programming Language :: Python",
28
27
  "Programming Language :: Python :: 3 :: Only",
29
28
  "Programming Language :: Python :: 3.9",
@@ -32,29 +31,21 @@ classifiers = [
32
31
  "Programming Language :: Python :: 3.12",
33
32
  "Programming Language :: Python :: 3.13",
34
33
  ]
35
- dynamic = [
36
- "version",
37
- ]
38
34
 
39
35
  dependencies = [ ]
40
- optional-dependencies.dev = [
41
- "pre-commit",
42
- "pytest",
43
- "pytest-cov",
44
- "pytest-randomly",
45
- ]
46
36
  urls.Documentation = "https://github.com/stefmolin/docstringify"
47
37
  urls.Homepage = "https://github.com/stefmolin/docstringify"
48
38
  urls.Source = "https://github.com/stefmolin/docstringify"
49
39
  scripts.docstringify = "docstringify.cli:main"
50
40
 
51
- [tool.setuptools.packages.find]
52
- where = [
53
- "src",
54
- ]
41
+ [dependency-groups]
42
+ dev = [ "pre-commit", { include-group = "test" } ]
55
43
 
56
- [tool.setuptools.dynamic]
57
- version = { attr = "docstringify.__version__" }
44
+ test = [
45
+ "pytest>=8.3.4",
46
+ "pytest-cov>=4.1.0",
47
+ "pytest-randomly>=3.12.0",
48
+ ]
58
49
 
59
50
  [tool.ruff]
60
51
  line-length = 88
@@ -96,6 +87,7 @@ lint.isort.known-first-party = [
96
87
  [tool.numpydoc_validation]
97
88
  checks = [
98
89
  "all", # report on all checks
90
+ "GL08", # use Docstringify to test for docstring presence instead
99
91
  "ES01", # but don't require an extended summary
100
92
  "EX01", # or examples
101
93
  "SA01", # or a see also section
@@ -106,6 +98,6 @@ exclude = [ # don't report on checks for these
106
98
  '\.__repr__$',
107
99
  '\.__str__$',
108
100
  ]
109
- override_SS05 = [ # override SS05 to allow docstrings starting with these words
110
- '^Process ',
101
+ override_SS05 = [ # allow docstrings to start with these words
102
+ '^Unambiguous ',
111
103
  ]
@@ -0,0 +1,10 @@
1
+ """
2
+ Flag missing docstrings and, optionally, generate them from signatures and
3
+ type annotations.
4
+ """
5
+
6
+ import importlib.metadata
7
+
8
+ __version__ = importlib.metadata.version(__name__)
9
+
10
+ __all__ = ['__version__']
@@ -0,0 +1,47 @@
1
+ """Docstring components."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Final, NamedTuple
6
+
7
+ NO_DEFAULT: Final = object()
8
+ """Sentinel value indicating that there is no default value. This is necessary because
9
+ ``None`` has a different meaning in certain contexts of the AST traversal."""
10
+
11
+ DESCRIPTION_PLACEHOLDER: Final[str] = '__description__'
12
+ """Docstring placeholder for description fields."""
13
+
14
+ PARAMETER_TYPE_PLACEHOLDER: Final[str] = '__type__'
15
+ """Docstring placeholder for the parameter type, when Docstringify can't determine it."""
16
+
17
+ RETURN_TYPE_PLACEHOLDER: Final[str] = '__return_type__'
18
+ """Docstring placeholder for the return type, when Docstringify can't determine it."""
19
+
20
+
21
+ class Parameter(NamedTuple):
22
+ """Information about an individual parameter to a function."""
23
+
24
+ name: str
25
+ """The name of the parameter."""
26
+
27
+ type_: str
28
+ """The parameter's data type."""
29
+
30
+ category: str | None
31
+ """The parameter's category, if there is one. This is used to pass additional
32
+ information, for example, to indicate that an argument is positional-only."""
33
+
34
+ default: str | NO_DEFAULT
35
+ """A string specifying the default value, if there is one, otherwise,
36
+ :const:`.NO_DEFAULT`."""
37
+
38
+
39
+ class Function(NamedTuple):
40
+ """Information about a function."""
41
+
42
+ parameters: tuple[Parameter, ...]
43
+ """Information about the function's parameters stored as :class:`.Parameter`
44
+ instances."""
45
+
46
+ return_type: str | None
47
+ """The return type for the function as a string, if present, otherwise, ``None``."""
@@ -184,7 +184,9 @@ class DocstringConverter(ABC):
184
184
  return self._returns_section_template.format(returns=return_text)
185
185
  return ''
186
186
 
187
- def format_docstring(self, docstring: str | list[str], indent: int) -> str:
187
+ def format_docstring(
188
+ self, docstring: str | list[str], indent: int, quote: bool | None = None
189
+ ) -> str:
188
190
  """
189
191
  Format the docstring with the requested level of indentation and surrounding
190
192
  triple quotes, if requested.
@@ -195,23 +197,28 @@ class DocstringConverter(ABC):
195
197
  The docstring as a string or list of strings that form lines in the docstring.
196
198
  indent : int
197
199
  The number of spaces by which to indent the docstring.
200
+ quote : bool | None, default=None
201
+ Whether to surround the docstring in triple quotes, passing this overrides
202
+ the ``quote`` setting specified upon initialization.
198
203
 
199
204
  Returns
200
205
  -------
201
206
  str
202
207
  The docstring indented to ``indent`` spaces will be returned either as a
203
- quoted docstring if the converter was initialized with ``quote=True``, or an
204
- unquoted docstring otherwise.
208
+ quoted docstring if the converter was initialized with ``quote=True`` or
209
+ ``quote=True`` was passed when calling this method, or an unquoted
210
+ docstring otherwise.
205
211
  """
206
- quote = '"""' if self._quote else ''
212
+ quote_char = '"""' if quote or self._quote else ''
207
213
  prefix = sep = ''
214
+
208
215
  if isinstance(docstring, str):
209
- docstring = [quote, docstring, quote]
216
+ docstring = [quote_char, docstring, quote_char]
210
217
  elif isinstance(docstring, list):
211
218
  if len(docstring) > 1:
212
219
  prefix = ' ' * indent if indent else ''
213
220
  sep = '\n'
214
- docstring = [quote, *docstring, f'{prefix}{quote}']
221
+ docstring = [quote_char, *docstring, quote_char or f'{prefix}']
215
222
  else:
216
223
  raise InvalidDocstringError(type(docstring).__name__)
217
224
 
@@ -0,0 +1,25 @@
1
+ """Docstringify exceptions."""
2
+
3
+
4
+ class EmptyDocstringError(ValueError):
5
+ """
6
+ Raised when trying to write the modified source code back to a file if a docstring
7
+ is somehow empty.
8
+ """
9
+
10
+ def __init__(self) -> None:
11
+ super().__init__('Docstring is empty')
12
+
13
+
14
+ class InvalidDocstringError(TypeError):
15
+ """
16
+ Raised when trying to create a docstring with invalid input.
17
+
18
+ Parameters
19
+ ----------
20
+ docstring_class : str
21
+ The class of the docstring object. This is used for the error message.
22
+ """
23
+
24
+ def __init__(self, docstring_class: str) -> None:
25
+ super().__init__(f'Expected str or list[str] docstring, got {docstring_class}')
@@ -1,3 +1,5 @@
1
+ """Base Docstringify traversal node."""
2
+
1
3
  from __future__ import annotations
2
4
 
3
5
  import ast
@@ -6,6 +8,21 @@ from typing import Callable, overload
6
8
 
7
9
 
8
10
  class DocstringNode:
11
+ """
12
+ Base class for Docstringify traversal nodes.
13
+
14
+ Parameters
15
+ ----------
16
+ node : ast.Module | ast.ClassDef | ast.FunctionDef | ast.AsyncFunctionDef
17
+ AST node representing a module, class, or function.
18
+ module_name : str
19
+ The name of the module to which the node belongs.
20
+ source_code : str
21
+ The source code for the module from which ``node`` originates.
22
+ parent : DocstringNode | None, default=None
23
+ The parent node, if there is one.
24
+ """
25
+
9
26
  @overload
10
27
  def __init__(
11
28
  self,
@@ -32,25 +49,50 @@ class DocstringNode:
32
49
  parent: DocstringNode | None = None,
33
50
  ) -> None:
34
51
  self.module_name: str = module_name
52
+ """The name of the module to which the node belongs."""
53
+
35
54
  self.parent: DocstringNode | None = parent
55
+ """The parent node, if there is one."""
56
+
36
57
  self.docstring_required: bool = True
58
+ """Whether the node requires a docstring."""
37
59
 
38
60
  self.ast_node: (
39
61
  ast.Module | ast.ClassDef | ast.FunctionDef | ast.AsyncFunctionDef
40
62
  ) = node
63
+ """The AST node that represents the source code."""
64
+
41
65
  self.name: str = getattr(node, 'name', self.module_name)
66
+ """The name of the node."""
42
67
 
43
68
  self.get_source_segment: Callable[[ast.AST], str | None] = partial(
44
69
  ast.get_source_segment, source_code
45
70
  )
71
+ """Callable to get the source code for the AST node (:attr:`.ast_node`)."""
46
72
 
47
73
  @property
48
74
  def docstring(self) -> str | None:
75
+ """
76
+ Get the docstring from the AST node.
77
+
78
+ Returns
79
+ -------
80
+ str | None
81
+ The docstring, if it exists.
82
+ """
49
83
  docstring = ast.get_docstring(self.ast_node)
50
84
  return docstring if docstring is None else docstring.strip()
51
85
 
52
86
  @property
53
87
  def fully_qualified_name(self) -> str:
88
+ """
89
+ Unambiguous name for the node.
90
+
91
+ Returns
92
+ -------
93
+ str
94
+ The fully-qualified name.
95
+ """
54
96
  return (
55
97
  f'{self.parent.fully_qualified_name}.{self.name}'
56
98
  if self.parent