Skip to content

Commit cb7bf8d

Browse files
authored
feat: wrap Sphinx style long parameter descriptions (#201)
* feat: add new --style argument * test: for new --style argument * doc: for new --style argument * fix: long description before list not wrapping * Merge branch `master` * Merge branch release/v1.7.0 * fix: not finding literal blocks * chore: add pre-commit-config.yaml * chore: add pre-commit-config.yaml * refactor: change FIELD_REGEX to SPHINX_REGEX
1 parent 6e06c6a commit cb7bf8d

12 files changed

Lines changed: 738 additions & 129 deletions

File tree

.github/workflows/do-release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ jobs:
6767
verbose: true
6868

6969
- name: Check if diff
70+
if: ${{ env.do_release == 1 }}
7071
continue-on-error: true
7172
run: >
7273
git diff --exit-code CHANGELOG.md &&
7374
(echo "### No update" && exit 1) || (echo "### Commit update")
7475
7576
- uses: EndBug/add-and-commit@v9
77+
if: ${{ env.do_release == 1 }}
7678
name: Commit and push if diff
7779
if: success()
7880
with:

.pre-commit-config.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-docstring-first
6+
- id: check-merge-conflict
7+
- id: check-toml
8+
- id: check-yaml
9+
- id: debug-statements
10+
- id: end-of-file-fixer
11+
- id: no-commit-to-branch
12+
- id: trailing-whitespace
13+
- repo: https://github.com/psf/black
14+
rev: '23.3.0'
15+
hooks:
16+
- id: black
17+
types_or: [python, pyi]
18+
language_version: python3
19+
- repo: https://github.com/PyCQA/isort
20+
rev: 5.12.0
21+
hooks:
22+
- id: isort
23+
args: [--settings-file, ./pyproject.toml]
24+
- repo: https://github.com/PyCQA/docformatter
25+
rev: v1.6.5
26+
hooks:
27+
- id: docformatter
28+
additional_dependencies: [tomli]
29+
args: [--in-place, --config, ./pyproject.toml]
30+
- repo: https://github.com/charliermarsh/ruff-pre-commit
31+
rev: 'v0.0.267'
32+
hooks:
33+
- id: ruff
34+
args: [ --select, "PL", --select, "F" ]
35+
- repo: https://github.com/pycqa/pydocstyle
36+
rev: 6.3.0
37+
hooks:
38+
- id: pydocstyle
39+
additional_dependencies: [toml]
40+
args: [--config, ./pyproject.toml]
41+
- repo: https://github.com/pre-commit/mirrors-mypy
42+
rev: v1.3.0
43+
hooks:
44+
- id: mypy
45+
additional_dependencies: [types-python-dateutil]
46+
args: [--config-file, ./pyproject.toml]
47+
- repo: https://github.com/myint/eradicate
48+
rev: '2.2.0'
49+
hooks:
50+
- id: eradicate
51+
args: []
52+
- repo: https://github.com/rstcheck/rstcheck
53+
rev: 'v6.1.2'
54+
hooks:
55+
- id: rstcheck
56+
additional_dependencies: [tomli]
57+
args: [-r, --config, ./pyproject.toml]

docs/source/configuration.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,38 @@ And you invoke docformatter as follows:
5151
$ docformatter --config ~/.secret/path/to/pyproject.toml --wrap-summaries 68
5252
5353
Summaries will be wrapped at 68, not 82.
54+
55+
A Note on Options to Control Styles
56+
-----------------------------------
57+
There are various ``docformatter`` options that can be used to control the
58+
style of the docstring. These options can be passed on the command line or
59+
set in a configuration file. Currently, the style options are:
60+
61+
* ``--black``
62+
* ``-s`` or ``--style``
63+
64+
When passing the ``--black`` option, the following arguments are set
65+
automatically:
66+
67+
* ``--pre-summary-space`` is set to True
68+
* ``--wrap-descriptions`` is set to 88
69+
* ``--wrap-summaries`` is set to 88
70+
71+
All of these options can be overridden from the command line. Further, the
72+
``--pre-summary-space`` option only inserts a space before the summary when
73+
the summary begins with a double quote ("). For example:
74+
75+
``"""This summary gets no space."""`` becomes ``"""This summary gets no space."""``
76+
77+
and
78+
79+
``""""This" summary does get a space."""`` becomes ``""" "This" summary does get a space."""``
80+
81+
The ``--style`` argument takes a string which is the name of the parameter
82+
list style you are using. Currently, only ``sphinx`` is recognized, but
83+
``epydoc``, ``numpy``, and ``google`` are future styles. For the selected
84+
style, each line in the parameter lists will be wrapped at the
85+
``--wrap-descriptions`` length as well as any portion of the elaborate
86+
description preceding the parameter list. Parameter lists that don't follow the
87+
passed style will cause the entire elaborate description to be ignored and
88+
remain unwrapped.

docs/source/requirements.rst

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,31 @@ Thus, an autoformatting tool:
5151
* Would be nice to produce as much output that satisfies the *methodology* requirements.
5252
* Would be nice to provide arguments to allow the user to turn on/off each *methodology* requirement the tool supports.
5353

54-
Docstring Syntax
55-
----------------
54+
Docstring Style
55+
---------------
5656

57-
There are at least three "flavors" of docstrings in common use today; Sphinx,
58-
NumPy, and Google. Each of these docstring flavors follow the PEP 257
59-
*convention* requirements. What differs between the three docstring flavors
60-
is the reST syntax used in the elaborate description of the multi-line
57+
There are at least four "flavors" of docstrings in common use today;
58+
Epydoc, Sphinx, NumPy, and Google. Each of these docstring flavors follow the
59+
PEP 257 *convention* requirements. What differs between the three docstring
60+
flavors is the reST syntax used in the parameter description of the multi-line
6161
docstring.
6262

6363
For example, here is how each syntax documents function arguments.
6464

65+
Epydoc syntax:
66+
67+
.. code-block::
68+
69+
@type num_dogs: int
70+
@param num_dogs: the number of dogs
71+
72+
Sphinx syntax:
73+
74+
.. code-block::
75+
76+
:param param1: The first parameter, defaults to 1.
77+
:type: int
78+
6579
Google syntax:
6680

6781
.. code-block::
@@ -78,13 +92,6 @@ NumPy syntax:
7892
param1 : int
7993
The first parameter.
8094
81-
Sphinx syntax:
82-
83-
.. code-block::
84-
85-
:param param1: The first parameter, defaults to 1.
86-
:type: int
87-
8895
Syntax is also important to ``Docutils``. An autoformatter should be aware of
8996
syntactical directives so they can be placed properly in the structure of the
9097
docstring. To accommodate the various syntax flavors used in docstrings, a
@@ -201,8 +208,14 @@ the requirement falls in, the type of requirement, and whether
201208
' docformatter_10.1.3.1', ' Shall maintain in-line links on one line even if the resulting line exceeds wrap length.', ' Derived', ' Shall', ' Yes [*PR #152*]'
202209
' docformatter_10.1.3.2', ' Shall not place a newline between description text and a wrapped link.', ' Derived', ' Shall', ' Yes [PR #182]'
203210
' docformatter_10.2', ' Should format docstrings using NumPy style.', ' Style', ' Should', ' No'
211+
' docformatter_10.2.1', ' Shall ignore docstrings in other styles when using NumPy style.', ' Shall', ' Yes'
212+
' docformatter_10.2.2', ' Shall wrap NumPy-style parameter descriptions that exceed wrap length when using NumPy style.', ' Shall', ' No'
204213
' docformatter_10.3', ' Should format docstrings using Google style.', ' Style', ' Should', ' No'
205-
' docformatter_10.4', ' Should format docstrings using Sphinx style.', ' Style', ' Should', ' No'
214+
' docformatter_10.3.1', ' Shall ignore docstrings in other styles when using Google style.', ' Shall', ' Yes'
215+
' docformatter_10.3.2', ' Shall wrap Google-style parameter descriptions that exceed wrap length when using Google style.', ' Shall', ' No'
216+
' docformatter_10.4', ' Should format docstrings using Sphinx style.', ' Style', ' Should', ' Yes'
217+
' docformatter_10.4.1', ' Shall ignore docstrings in other styles when using Sphinx style.', ' Shall', ' Yes'
218+
' docformatter_10.4.2', ' Shall wrap Sphinx-style parameter descriptions that exceed wrap length when using Sphinx style.', ' Shall', ' Yes'
206219
' docformatter_10.5', ' Should format docstrings compatible with black.', ' Style', ' Should', ' Yes [PR #192]'
207220
' docformatter_10.5.1', ' Should wrap summaries at 88 characters by default in black mode.', ' Style', ' Should', ' Yes'
208221
' docformatter_10.5.2', ' Should wrap descriptions at 88 characters by default in black mode.', ' Style', ' Should', ' Yes'
@@ -244,6 +257,9 @@ with *convention* requirements.
244257
``docformatter`` currently provides these arguments for *style* requirements.
245258
::
246259

260+
-s, --style [string, default sphinx]
261+
name of the docstring syntax style to use for formatting parameter
262+
lists.
247263
--black [boolean, default False]
248264
Boolean to indicate whether to format docstrings to be compatible
249265
with black.
@@ -306,12 +322,6 @@ The following are new arguments that are needed to implement **should** or
306322
Boolean to indicate whether to wrap one-line docstrings. Provides
307323
option for requirement PEP_257_4.1.
308324

309-
The following are new *style* arguments needed to accommodate the various style options:
310-
::
311-
312-
--syntax [string, default "sphinx"]
313-
One of sphinx, numpy, or google
314-
315325
Issue and Version Management
316326
----------------------------
317327

docs/source/usage.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ help output provides a summary of these options:
4343
-n, --non-cap list of words not to capitalize when they appear as the
4444
first word in the summary
4545
46+
-s style, --style style
47+
the docstring style to use when formatting parameter
48+
lists (default: sphinx)
49+
--black
50+
make formatting compatible with standard black options
51+
(default: False)
4652
--wrap-summaries length
4753
wrap long summary lines at this length; set
4854
to 0 to disable wrapping
@@ -51,11 +57,12 @@ help output provides a summary of these options:
5157
wrap descriptions at this length; set to 0 to
5258
disable wrapping
5359
(default: 72)
54-
--black
55-
make formatting consistent with black, setting
56-
wrap-summaries and wrap-descriptions to a default 88
57-
if not otherwise specified
58-
(default: False)
60+
--force-wrap
61+
force descriptions to be wrapped even if it may result
62+
in a mess (default: False)
63+
--tab_width width
64+
tabs in indentation are this many characters when
65+
wrapping lines (default: 1)
5966
--blank
6067
add blank line after elaborate description
6168
(default: False)
@@ -75,21 +82,14 @@ help output provides a summary of these options:
7582
place closing triple quotes on a new-line when a
7683
one-line docstring wraps to two or more lines
7784
(default: False)
78-
--force-wrap
79-
force descriptions to be wrapped even if it may result
80-
in a mess (default: False)
81-
--tab_width width
82-
tabs in indentation are this many characters when
83-
wrapping lines (default: 1)
8485
--range start_line end_line
8586
apply docformatter to docstrings between these lines;
8687
line numbers are indexed at 1
8788
--docstring-length min_length max_length
8889
apply docformatter to docstrings of given length range
8990
--non-strict
9091
do not strictly follow reST syntax to identify lists
91-
(see issue #67)
92-
(default: False)
92+
(see issue #67) (default: False)
9393
--config CONFIG
9494
path to file containing docformatter options
9595
(default: ./pyproject.toml)

src/docformatter/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def _help():
7171
list of words not to capitalize when they appear as the
7272
first word in the summary
7373
74+
-s style, --style style
75+
the docstring style to use when formatting parameter
76+
lists (default: sphinx)
7477
--black make formatting compatible with standard black options
7578
(default: False)
7679
--wrap-summaries length

src/docformatter/configuration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ def do_parse_arguments(self) -> None:
158158
_default_wrap_descriptions = 72
159159
_default_pre_summary_space = "false"
160160

161+
self.parser.add_argument(
162+
"-s",
163+
"--style",
164+
default=self.flargs_dct.get("style", "sphinx"),
165+
help="name of the docstring style to use when formatting "
166+
"parameter lists (default: sphinx)",
167+
)
161168
self.parser.add_argument(
162169
"--wrap-summaries",
163170
default=int(

src/docformatter/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def _do_format_docstring(
489489
_syntax.is_some_sort_of_list(
490490
summary,
491491
self.args.non_strict,
492+
self.args.style,
492493
)
493494
or _syntax.do_find_directives(summary)
494495
):
@@ -613,6 +614,7 @@ def _do_format_multiline_docstring(
613614
wrap_length=self.args.wrap_descriptions,
614615
force_wrap=self.args.force_wrap,
615616
strict=self.args.non_strict,
617+
style=self.args.style,
616618
)
617619
post_description = "\n" if self.args.post_description_blank else ""
618620
return f'''\

0 commit comments

Comments
 (0)