Skip to content

Commit 93ba24d

Browse files
authored
feat: add option to format compatible with black (#196)
* docs: update docs for new --black option * amend! docs: update docs for new --black option * feat: set default options for black * test: for set default options for black * feat: set pre-summary space for black compatibility * test: for black option defaults * chore: set black true in pyproject.toml and format code base * chore: add requirements for --black option * chore: clean-up code smells * style: fix unwrapped docstring * fix: update help message to include non-cap option
1 parent 86b4ea6 commit 93ba24d

File tree

11 files changed

+486
-127
lines changed

11 files changed

+486
-127
lines changed

docs/source/requirements.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ the requirement falls in, the type of requirement, and whether
202202
' docformatter_10.1.3.2', ' Shall not place a newline between description text and a wrapped link.', ' Derived', ' Shall', ' Yes [PR #182]'
203203
' docformatter_10.2', ' Should format docstrings using NumPy style.', ' Style', ' Should', ' No'
204204
' 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'
205+
' docformatter_10.4', ' Should format docstrings using Sphinx style.', ' Style', ' Should', ' No'
206+
' docformatter_10.5', ' Should format docstrings compatible with black.', ' Style', ' Should', ' Yes [PR #192]'
207+
' docformatter_10.5.1', ' Should wrap summaries at 88 characters by default in black mode.', ' Style', ' Should', ' Yes'
208+
' docformatter_10.5.2', ' Should wrap descriptions at 88 characters by default in black mode.', ' Style', ' Should', ' Yes'
209+
' docformatter_10.5.3', ' Should insert a space before the first word in the summary if that word is quoted when in black mode.', ' Style', ' Should', ' Yes'
210+
' docformatter_10.5.4', ' Default black mode options should be over-rideable by passing arguments or using configuration files.', ' Style', ' Should', ' Yes'
206211
' docformatter_11', '**Program Control**'
207212
' docformatter_11.1', ' Should check formatting and report incorrectly documented docstrings.', ' Stakeholder', ' Should', ' Yes [*PR #32*]'
208213
' docformatter_11.2', ' Should fix formatting and save changes to file.', ' Stakeholder', ' Should', ' Yes'
@@ -239,6 +244,9 @@ with *convention* requirements.
239244
``docformatter`` currently provides these arguments for *style* requirements.
240245
::
241246

247+
--black [boolean, default False]
248+
Boolean to indicate whether to format docstrings to be compatible
249+
with black.
242250
--blank [boolean, default False]
243251
Boolean to indicate whether to add a blank line after the
244252
elaborate description.
@@ -303,8 +311,6 @@ The following are new *style* arguments needed to accommodate the various style
303311

304312
--syntax [string, default "sphinx"]
305313
One of sphinx, numpy, or google
306-
--black [boolean, default False]
307-
Formats docstrings to be compatible with black.
308314

309315
Issue and Version Management
310316
----------------------------

docs/source/usage.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ help output provides a summary of these options:
2222
.. code-block:: console
2323
2424
usage: docformatter [-h] [-i | -c] [-r] [--wrap-summaries length]
25-
[--wrap-descriptions length] [--blank]
25+
[--wrap-descriptions length] [--black] [--blank]
2626
[--pre-summary-newline] [--make-summary-multi-line]
2727
[--force-wrap] [--range start_line end_line]
2828
[--docstring-length min_length max_length]
@@ -51,6 +51,11 @@ help output provides a summary of these options:
5151
wrap descriptions at this length; set to 0 to
5252
disable wrapping
5353
(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)
5459
--blank
5560
add blank line after elaborate description
5661
(default: False)

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ disable = [
9292
]
9393

9494
[tool.docformatter]
95-
wrap-summaries = 79
96-
wrap-descriptions = 72
95+
black = true
9796
non-strict = false
9897

9998
[tool.pydocstyle]

src/docformatter/__main__.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,83 @@
4141
import docformatter.format as _format
4242

4343

44+
def _help():
45+
"""Print docformatter's help."""
46+
print(
47+
"""\
48+
usage: docformatter [-h] [-i | -c] [-d] [-r] [-e [EXCLUDE ...]]
49+
[-n [NON-CAP ...]] [--black] [--wrap-summaries length]
50+
[--wrap-descriptions length] [--force-wrap]
51+
[--tab-width width] [--blank] [--pre-summary-newline]
52+
[--pre-summary-space] [--make-summary-multi-line]
53+
[--close-quotes-on-newline] [--range line line]
54+
[--docstring-length length length] [--non-strict]
55+
[--config CONFIG] [--version] files [files ...]
56+
57+
positional arguments:
58+
files files to format or '-' for standard in
59+
60+
options:
61+
-h, --help show this help message and exit
62+
-i, --in-place make changes to files instead of printing diffs
63+
-c, --check only check and report incorrectly formatted files
64+
-d, --diff when used with `--check` or `--in-place`, also what
65+
changes would be made
66+
-r, --recursive drill down directories recursively
67+
-e [EXCLUDE ...], --exclude [EXCLUDE ...]
68+
in recursive mode, exclude directories and files by
69+
names
70+
-n [NON-CAP ...], --non-cap [NON-CAP ...]
71+
list of words not to capitalize when they appear as the
72+
first word in the summary
73+
74+
--black make formatting compatible with standard black options
75+
(default: False)
76+
--wrap-summaries length
77+
wrap long summary lines at this length; set to 0 to
78+
disable wrapping (default: 79, 88 with --black option)
79+
--wrap-descriptions length
80+
wrap descriptions at this length; set to 0 to disable
81+
wrapping (default: 72, 88 with --black option)
82+
--force-wrap force descriptions to be wrapped even if it may result
83+
in a mess (default: False)
84+
--tab-width width tabs in indentation are this many characters when
85+
wrapping lines (default: 1)
86+
--blank add blank line after description (default: False)
87+
--pre-summary-newline
88+
add a newline before the summary of a multi-line
89+
docstring (default: False)
90+
--pre-summary-space add a space after the opening triple quotes
91+
(default: False)
92+
--make-summary-multi-line
93+
add a newline before and after the summary of a
94+
one-line docstring (default: False)
95+
--close-quotes-on-newline
96+
place closing triple quotes on a new-line when a
97+
one-line docstring wraps to two or more lines
98+
(default: False)
99+
--range line line apply docformatter to docstrings between these lines;
100+
line numbers are indexed at 1 (default: None)
101+
--docstring-length length length
102+
apply docformatter to docstrings of given length range
103+
(default: None)
104+
--non-strict don't strictly follow reST syntax to identify lists
105+
(see issue #67) (default: False)
106+
--config CONFIG path to file containing docformatter options
107+
--version show program's version number and exit
108+
"""
109+
)
110+
111+
44112
def _main(argv, standard_out, standard_error, standard_in):
45113
"""Run internal main entry point."""
46114
configurator = _configuration.Configurater(argv)
47-
configurator.do_parse_arguments()
115+
116+
if "--help" in configurator.args_lst:
117+
_help()
118+
sys.exit()
119+
else:
120+
configurator.do_parse_arguments()
48121

49122
formator = _format.Formatter(
50123
configurator.args,

src/docformatter/configuration.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,54 @@ def do_parse_arguments(self) -> None:
133133
action="store",
134134
nargs="*",
135135
default=self.flargs_dct.get("non-cap", None),
136-
help="list of words not to capitalize when they appear as the "
136+
help="list of words not to capitalize "
137+
"when they appear as the "
137138
"first word in the summary",
138139
)
140+
self.parser.add_argument(
141+
"--black",
142+
action="store_true",
143+
default=self.flargs_dct.get("black", "false").lower() == "true",
144+
help="make formatting compatible with standard black options "
145+
"(default: False)",
146+
)
147+
148+
self.args = self.parser.parse_known_args(self.args_lst[1:])[0]
149+
150+
# Default black line length is 88 so use this when not specified
151+
# otherwise use PEP-8 defaults
152+
if self.args.black:
153+
_default_wrap_summaries = 88
154+
_default_wrap_descriptions = 88
155+
_default_pre_summary_space = "true"
156+
else:
157+
_default_wrap_summaries = 79
158+
_default_wrap_descriptions = 72
159+
_default_pre_summary_space = "false"
160+
139161
self.parser.add_argument(
140162
"--wrap-summaries",
141-
default=int(self.flargs_dct.get("wrap-summaries", 79)),
163+
default=int(
164+
self.flargs_dct.get("wrap-summaries", _default_wrap_summaries)
165+
),
142166
type=int,
143167
metavar="length",
144168
help="wrap long summary lines at this length; "
145-
"set to 0 to disable wrapping (default: 79)",
169+
"set to 0 to disable wrapping (default: 79, 88 with --black "
170+
"option)",
146171
)
147172
self.parser.add_argument(
148173
"--wrap-descriptions",
149-
default=int(self.flargs_dct.get("wrap-descriptions", 72)),
174+
default=int(
175+
self.flargs_dct.get(
176+
"wrap-descriptions", _default_wrap_descriptions
177+
)
178+
),
150179
type=int,
151180
metavar="length",
152181
help="wrap descriptions at this length; "
153-
"set to 0 to disable wrapping (default: 72)",
182+
"set to 0 to disable wrapping (default: 72, 88 with --black "
183+
"option)",
154184
)
155185
self.parser.add_argument(
156186
"--force-wrap",
@@ -187,7 +217,9 @@ def do_parse_arguments(self) -> None:
187217
self.parser.add_argument(
188218
"--pre-summary-space",
189219
action="store_true",
190-
default=self.flargs_dct.get("pre-summary-space", "false").lower()
220+
default=self.flargs_dct.get(
221+
"pre-summary-space", _default_pre_summary_space
222+
).lower()
191223
== "true",
192224
help="add a space after the opening triple quotes "
193225
"(default: False)",
@@ -258,6 +290,7 @@ def do_parse_arguments(self) -> None:
258290
)
259291

260292
self.args = self.parser.parse_args(self.args_lst[1:])
293+
261294
if self.args.line_range:
262295
if self.args.line_range[0] <= 0:
263296
self.parser.error("--range must be positive numbers")

0 commit comments

Comments
 (0)