docstringify 1.0.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.
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/dependabot.yml +4 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/workflows/ci.yml +4 -2
- {docstringify-1.0.0 → docstringify-1.1.0}/.pre-commit-config.yaml +8 -8
- docstringify-1.1.0/.python-version +1 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/CONTRIBUTING.md +1 -1
- {docstringify-1.0.0 → docstringify-1.1.0}/PKG-INFO +9 -36
- {docstringify-1.0.0 → docstringify-1.1.0}/README.md +5 -5
- {docstringify-1.0.0 → docstringify-1.1.0}/pyproject.toml +15 -23
- docstringify-1.1.0/src/docstringify/__init__.py +10 -0
- docstringify-1.1.0/src/docstringify/components.py +47 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/nodes/base.py +42 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/nodes/function.py +145 -3
- docstringify-1.1.0/uv.lock +423 -0
- docstringify-1.0.0/setup.cfg +0 -4
- docstringify-1.0.0/src/docstringify/__init__.py +0 -6
- docstringify-1.0.0/src/docstringify/components.py +0 -22
- docstringify-1.0.0/src/docstringify.egg-info/PKG-INFO +0 -243
- docstringify-1.0.0/src/docstringify.egg-info/SOURCES.txt +0 -38
- docstringify-1.0.0/src/docstringify.egg-info/dependency_links.txt +0 -1
- docstringify-1.0.0/src/docstringify.egg-info/entry_points.txt +0 -2
- docstringify-1.0.0/src/docstringify.egg-info/requires.txt +0 -6
- docstringify-1.0.0/src/docstringify.egg-info/top_level.txt +0 -1
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/pull_request_template.md +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/release.yml +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/workflows/greetings.yml +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.github/workflows/pypi-publish.yml +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.gitignore +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/.pre-commit-hooks.yaml +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/CODE_OF_CONDUCT.md +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/LICENSE +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/__main__.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/cli.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/converters/__init__.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/converters/base.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/converters/google.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/converters/numpydoc.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/converters/stub.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/exceptions.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/nodes/__init__.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/traversal/__init__.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/traversal/transformer.py +0 -0
- {docstringify-1.0.0 → docstringify-1.1.0}/src/docstringify/traversal/visitor.py +0 -0
|
@@ -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
|
|
64
|
-
run: python -m pip install
|
|
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,14 +11,20 @@ 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.
|
|
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.
|
|
27
|
+
rev: v0.12.2
|
|
22
28
|
hooks:
|
|
23
29
|
- id: ruff-check
|
|
24
30
|
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
|
|
@@ -29,9 +35,3 @@ repos:
|
|
|
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
|
|
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,33 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: docstringify
|
|
3
|
-
Version: 1.
|
|
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
12
|
Classifier: Development Status :: 4 - Beta
|
|
33
13
|
Classifier: Programming Language :: Python
|
|
@@ -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.
|
|
@@ -107,7 +80,7 @@ Add the following to your `.pre-commit-config.yaml` file to block commits with m
|
|
|
107
80
|
|
|
108
81
|
```yaml
|
|
109
82
|
- repo: https://github.com/stefmolin/docstringify
|
|
110
|
-
rev: 1.
|
|
83
|
+
rev: 1.1.0
|
|
111
84
|
hooks:
|
|
112
85
|
- id: docstringify
|
|
113
86
|
```
|
|
@@ -116,7 +89,7 @@ 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: 1.
|
|
92
|
+
rev: 1.1.0
|
|
120
93
|
hooks:
|
|
121
94
|
- id: docstringify
|
|
122
95
|
args: [--threshold=0.75]
|
|
@@ -126,7 +99,7 @@ If you would like to see suggested docstring templates (inferred from type annot
|
|
|
126
99
|
|
|
127
100
|
```yaml
|
|
128
101
|
- repo: https://github.com/stefmolin/docstringify
|
|
129
|
-
rev: 1.
|
|
102
|
+
rev: 1.1.0
|
|
130
103
|
hooks:
|
|
131
104
|
- id: docstringify
|
|
132
105
|
args: [--suggest-changes=numpydoc]
|
|
@@ -136,7 +109,7 @@ 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: 1.
|
|
112
|
+
rev: 1.1.0
|
|
140
113
|
hooks:
|
|
141
114
|
- id: docstringify
|
|
142
115
|
args: [--make-changes=google]
|
|
@@ -146,7 +119,7 @@ If you want the changes to be made in place, change `--make-changes` to `--make-
|
|
|
146
119
|
|
|
147
120
|
```yaml
|
|
148
121
|
- repo: https://github.com/stefmolin/docstringify
|
|
149
|
-
rev: 1.
|
|
122
|
+
rev: 1.1.0
|
|
150
123
|
hooks:
|
|
151
124
|
- id: docstringify
|
|
152
125
|
args: [--make-changes-inplace=numpydoc]
|
|
@@ -58,7 +58,7 @@ Add the following to your `.pre-commit-config.yaml` file to block commits with m
|
|
|
58
58
|
|
|
59
59
|
```yaml
|
|
60
60
|
- repo: https://github.com/stefmolin/docstringify
|
|
61
|
-
rev: 1.
|
|
61
|
+
rev: 1.1.0
|
|
62
62
|
hooks:
|
|
63
63
|
- id: docstringify
|
|
64
64
|
```
|
|
@@ -67,7 +67,7 @@ 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: 1.
|
|
70
|
+
rev: 1.1.0
|
|
71
71
|
hooks:
|
|
72
72
|
- id: docstringify
|
|
73
73
|
args: [--threshold=0.75]
|
|
@@ -77,7 +77,7 @@ If you would like to see suggested docstring templates (inferred from type annot
|
|
|
77
77
|
|
|
78
78
|
```yaml
|
|
79
79
|
- repo: https://github.com/stefmolin/docstringify
|
|
80
|
-
rev: 1.
|
|
80
|
+
rev: 1.1.0
|
|
81
81
|
hooks:
|
|
82
82
|
- id: docstringify
|
|
83
83
|
args: [--suggest-changes=numpydoc]
|
|
@@ -87,7 +87,7 @@ 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: 1.
|
|
90
|
+
rev: 1.1.0
|
|
91
91
|
hooks:
|
|
92
92
|
- id: docstringify
|
|
93
93
|
args: [--make-changes=google]
|
|
@@ -97,7 +97,7 @@ If you want the changes to be made in place, change `--make-changes` to `--make-
|
|
|
97
97
|
|
|
98
98
|
```yaml
|
|
99
99
|
- repo: https://github.com/stefmolin/docstringify
|
|
100
|
-
rev: 1.
|
|
100
|
+
rev: 1.1.0
|
|
101
101
|
hooks:
|
|
102
102
|
- id: docstringify
|
|
103
103
|
args: [--make-changes-inplace=numpydoc]
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
build-backend = "
|
|
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,7 +15,8 @@ keywords = [
|
|
|
17
15
|
"numpydoc-style docstring",
|
|
18
16
|
"pre-commit",
|
|
19
17
|
]
|
|
20
|
-
license =
|
|
18
|
+
license = "MIT"
|
|
19
|
+
license-files = [ "LICENSE" ]
|
|
21
20
|
authors = [
|
|
22
21
|
{ name = "Stefanie Molin", email = "docstringify@stefaniemolin.com" },
|
|
23
22
|
]
|
|
@@ -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
|
-
[
|
|
52
|
-
|
|
53
|
-
"src",
|
|
54
|
-
]
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = [ "pre-commit", { include-group = "test" } ]
|
|
55
43
|
|
|
56
|
-
[
|
|
57
|
-
|
|
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 = [ #
|
|
110
|
-
'^
|
|
101
|
+
override_SS05 = [ # allow docstrings to start with these words
|
|
102
|
+
'^Unambiguous ',
|
|
111
103
|
]
|
|
@@ -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``."""
|
|
@@ -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
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"""Docstringify traversal node for functions."""
|
|
2
|
+
|
|
1
3
|
from __future__ import annotations
|
|
2
4
|
|
|
3
5
|
import ast
|
|
@@ -15,6 +17,21 @@ from .base import DocstringNode
|
|
|
15
17
|
|
|
16
18
|
|
|
17
19
|
class FunctionDocstringNode(DocstringNode):
|
|
20
|
+
"""
|
|
21
|
+
Class for Docstringify function traversal nodes.
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
node : ast.FunctionDef | ast.AsyncFunctionDef
|
|
26
|
+
AST node representing a function.
|
|
27
|
+
module_name : str
|
|
28
|
+
The name of the module to which the node belongs.
|
|
29
|
+
source_code : str
|
|
30
|
+
The source code for the module from which ``node`` originates.
|
|
31
|
+
parent : DocstringNode
|
|
32
|
+
The parent node.
|
|
33
|
+
"""
|
|
34
|
+
|
|
18
35
|
def __init__(
|
|
19
36
|
self,
|
|
20
37
|
node: ast.FunctionDef | ast.AsyncFunctionDef,
|
|
@@ -29,26 +46,45 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
29
46
|
for decorator_node in node.decorator_list
|
|
30
47
|
if (decorator := self.get_source_segment(decorator_node))
|
|
31
48
|
]
|
|
49
|
+
"""List of decorators applied to this function."""
|
|
32
50
|
|
|
33
51
|
self.is_overload: bool = (
|
|
34
52
|
'overload' in self.decorators or 'typing.overload' in self.decorators
|
|
35
53
|
)
|
|
54
|
+
"""Whether this function definition is actually just a typing overload
|
|
55
|
+
definition, and, therefore, doesn't need a docstring."""
|
|
56
|
+
|
|
36
57
|
self.is_method: bool = self.parent is not None and isinstance(
|
|
37
58
|
self.parent.ast_node, ast.ClassDef
|
|
38
59
|
)
|
|
60
|
+
"""Whether this function is actually a method, meaning that its parent node
|
|
61
|
+
is a class."""
|
|
62
|
+
|
|
39
63
|
self.is_abstract_method: bool = self.is_method and (
|
|
40
64
|
'abstractmethod' in self.decorators
|
|
41
65
|
or 'abc.abstractmethod' in self.decorators
|
|
42
66
|
)
|
|
67
|
+
"""Whether this function is an abstract method, meaning it is a method
|
|
68
|
+
(:attr:`.is_method`) and has an ``abstractmethod`` decorator."""
|
|
69
|
+
|
|
43
70
|
self.is_class_method: bool = self.is_method and 'classmethod' in self.decorators
|
|
71
|
+
"""Whether this function is a class method, meaning it is a method
|
|
72
|
+
(:attr:`.is_method`) and has a ``classmethod`` decorator."""
|
|
73
|
+
|
|
44
74
|
self.is_static_method: bool = (
|
|
45
75
|
self.is_method and 'staticmethod' in self.decorators
|
|
46
76
|
)
|
|
77
|
+
"""Whether this function is a static method, meaning it is a method
|
|
78
|
+
(:attr:`.is_method`) and has a ``staticmethod`` decorator."""
|
|
79
|
+
|
|
47
80
|
self.is_instance_method: bool = (
|
|
48
81
|
self.is_method
|
|
49
82
|
and (not self.is_class_method)
|
|
50
83
|
and (not self.is_static_method)
|
|
51
84
|
)
|
|
85
|
+
"""Whether this function is an instance method, meaning it is a method
|
|
86
|
+
(:attr:`.is_method`), but it is not a class method (:attr:`.is_class_method`)
|
|
87
|
+
or static method (:attr:`.is_static_method`)."""
|
|
52
88
|
|
|
53
89
|
# don't require docstring for the __init__ method if the class has a docstring
|
|
54
90
|
# or if the function/method is just a typing.overload signature
|
|
@@ -61,16 +97,69 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
61
97
|
)
|
|
62
98
|
and not self.is_overload
|
|
63
99
|
)
|
|
100
|
+
"""Whether this node should have a docstring. Docstrings are currently required
|
|
101
|
+
for all functions unless they are a typing overload. Note that ``__init__()``
|
|
102
|
+
methods don't require a docstring if the parent (the class) has a docstring."""
|
|
64
103
|
|
|
65
104
|
self.arguments: ast.arguments = node.args
|
|
105
|
+
"""The function arguments in their AST representation."""
|
|
66
106
|
|
|
67
107
|
self.returns: ast.AST | None = node.returns
|
|
108
|
+
"""The function's return annotation, as an AST node, if there is a return
|
|
109
|
+
annotation, otherwise ``None``."""
|
|
110
|
+
|
|
68
111
|
self.return_annotation: str | None = self._extract_return_annotation()
|
|
112
|
+
"""The function's return annotation, as a string, if there is a return
|
|
113
|
+
annotation, otherwise ``None``."""
|
|
114
|
+
|
|
69
115
|
self.return_statements: list[ast.Return] = []
|
|
116
|
+
"""All ``return`` calls in the function itself (not any inner functions, for
|
|
117
|
+
example), represented as AST nodes. Note that this will be populated by the
|
|
118
|
+
traversal logic when those nodes are visited."""
|
|
119
|
+
|
|
120
|
+
def _extract_parameter_type(self, arg: ast.arg) -> str:
|
|
121
|
+
"""
|
|
122
|
+
Extract the parameter's type.
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
----------
|
|
126
|
+
arg : ast.arg
|
|
127
|
+
The AST node for the function argument.
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
-------
|
|
131
|
+
str
|
|
132
|
+
The parameter type for use in the docstring.
|
|
133
|
+
"""
|
|
134
|
+
if arg.annotation is None:
|
|
135
|
+
return PARAMETER_TYPE_PLACEHOLDER
|
|
136
|
+
|
|
137
|
+
try:
|
|
138
|
+
# if `arg.annotation` is of type `ast.Name`, we can just get the `id`
|
|
139
|
+
return arg.annotation.id
|
|
140
|
+
except AttributeError:
|
|
141
|
+
return self.get_source_segment(arg.annotation) or PARAMETER_TYPE_PLACEHOLDER
|
|
70
142
|
|
|
71
143
|
def _extract_default_values(
|
|
72
144
|
self, default: ast.Constant | None | Literal[NO_DEFAULT], is_keyword_only: bool
|
|
73
145
|
) -> str | Literal[NO_DEFAULT]:
|
|
146
|
+
"""
|
|
147
|
+
Extract the argument's default value.
|
|
148
|
+
|
|
149
|
+
Parameters
|
|
150
|
+
----------
|
|
151
|
+
default : ast.Constant | None | Literal[NO_DEFAULT]
|
|
152
|
+
The representation of the default value for further processing.
|
|
153
|
+
is_keyword_only : bool
|
|
154
|
+
Whether the default value being extracted corresponds to a keyword-only
|
|
155
|
+
argument.
|
|
156
|
+
|
|
157
|
+
Returns
|
|
158
|
+
-------
|
|
159
|
+
str | Literal[NO_DEFAULT]
|
|
160
|
+
The default value as a string, if there is one, otherwise,
|
|
161
|
+
:const:`.NO_DEFAULT`.
|
|
162
|
+
"""
|
|
74
163
|
if (not is_keyword_only and default is not NO_DEFAULT) or (
|
|
75
164
|
is_keyword_only and default
|
|
76
165
|
):
|
|
@@ -101,7 +190,7 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
101
190
|
return [
|
|
102
191
|
Parameter(
|
|
103
192
|
name=f'*{arg.arg}' if arg_type == 'vararg' else f'**{arg.arg}',
|
|
104
|
-
type_=
|
|
193
|
+
type_=self._extract_parameter_type(arg),
|
|
105
194
|
category=None,
|
|
106
195
|
default=NO_DEFAULT,
|
|
107
196
|
)
|
|
@@ -112,6 +201,15 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
112
201
|
]
|
|
113
202
|
|
|
114
203
|
def _extract_positional_args(self) -> list[Parameter]:
|
|
204
|
+
"""
|
|
205
|
+
Extract the function's positional arguments.
|
|
206
|
+
|
|
207
|
+
Returns
|
|
208
|
+
-------
|
|
209
|
+
list[Parameter]
|
|
210
|
+
A list of :class:`.Parameter` instances, representing the function's
|
|
211
|
+
positional arguments.
|
|
212
|
+
"""
|
|
115
213
|
if (default_count := len(positional_defaults := self.arguments.defaults)) < (
|
|
116
214
|
positional_arguments_count := (
|
|
117
215
|
len(self.arguments.posonlyargs) + len(self.arguments.args)
|
|
@@ -124,7 +222,7 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
124
222
|
return [
|
|
125
223
|
Parameter(
|
|
126
224
|
name=arg.arg,
|
|
127
|
-
type_=
|
|
225
|
+
type_=self._extract_parameter_type(arg),
|
|
128
226
|
category=category,
|
|
129
227
|
default=self._extract_default_values(default, False),
|
|
130
228
|
)
|
|
@@ -140,10 +238,19 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
140
238
|
]
|
|
141
239
|
|
|
142
240
|
def _extract_keyword_args(self) -> list[Parameter]:
|
|
241
|
+
"""
|
|
242
|
+
Extract the function's keyword arguments.
|
|
243
|
+
|
|
244
|
+
Returns
|
|
245
|
+
-------
|
|
246
|
+
list[Parameter]
|
|
247
|
+
A list of :class:`.Parameter` instances, representing the function's
|
|
248
|
+
keyword arguments.
|
|
249
|
+
"""
|
|
143
250
|
return [
|
|
144
251
|
Parameter(
|
|
145
252
|
name=arg.arg,
|
|
146
|
-
type_=
|
|
253
|
+
type_=self._extract_parameter_type(arg),
|
|
147
254
|
category='keyword-only',
|
|
148
255
|
default=self._extract_default_values(default, True),
|
|
149
256
|
)
|
|
@@ -153,6 +260,15 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
153
260
|
]
|
|
154
261
|
|
|
155
262
|
def extract_arguments(self) -> tuple[Parameter, ...]:
|
|
263
|
+
"""
|
|
264
|
+
Extract the function's arguments.
|
|
265
|
+
|
|
266
|
+
Returns
|
|
267
|
+
-------
|
|
268
|
+
tuple[Parameter, ...]
|
|
269
|
+
A tuple of :class:`.Parameter` instances, representing all of the
|
|
270
|
+
function's arguments.
|
|
271
|
+
"""
|
|
156
272
|
params = self._extract_positional_args()
|
|
157
273
|
|
|
158
274
|
varargs, kwargs = self._extract_star_args()
|
|
@@ -174,6 +290,14 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
174
290
|
return params
|
|
175
291
|
|
|
176
292
|
def _extract_return_annotation(self) -> str | None:
|
|
293
|
+
"""
|
|
294
|
+
Extract the function's return annotation, if present.
|
|
295
|
+
|
|
296
|
+
Returns
|
|
297
|
+
-------
|
|
298
|
+
str | None
|
|
299
|
+
The return annotation as a string, if present, otherwise, ``None``.
|
|
300
|
+
"""
|
|
177
301
|
if self.returns is None:
|
|
178
302
|
return self.returns
|
|
179
303
|
|
|
@@ -186,6 +310,16 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
186
310
|
return self.get_source_segment(self.returns)
|
|
187
311
|
|
|
188
312
|
def extract_returns(self) -> str | None:
|
|
313
|
+
"""
|
|
314
|
+
Extract the function's return type for use in the docstring.
|
|
315
|
+
|
|
316
|
+
Returns
|
|
317
|
+
-------
|
|
318
|
+
str | None
|
|
319
|
+
The return type as a string, if present. Otherwise, the function will
|
|
320
|
+
be checked for ``return`` calls, and, if present, a placeholder for the
|
|
321
|
+
type will be returned; if it isn't present, ``None``, will be returned.
|
|
322
|
+
"""
|
|
189
323
|
if self.return_annotation:
|
|
190
324
|
return self.return_annotation
|
|
191
325
|
|
|
@@ -199,4 +333,12 @@ class FunctionDocstringNode(DocstringNode):
|
|
|
199
333
|
return None
|
|
200
334
|
|
|
201
335
|
def to_function(self) -> Function:
|
|
336
|
+
"""
|
|
337
|
+
Convert node into a :class:`.Function` instance.
|
|
338
|
+
|
|
339
|
+
Returns
|
|
340
|
+
-------
|
|
341
|
+
Function
|
|
342
|
+
The :class:`.Function` instance.
|
|
343
|
+
"""
|
|
202
344
|
return Function(self.extract_arguments(), self.extract_returns())
|