Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/docformatter/__main__.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def _help():
[--wrap-descriptions length] [--force-wrap]
[--tab-width width] [--blank] [--pre-summary-newline]
[--pre-summary-space] [--make-summary-multi-line]
[--close-quotes-on-newline] [--range line line]
[--docstring-length length length] [--non-strict]
[--config CONFIG] [--version] files [files ...]
[--close-quotes-on-newline] [--preserve-module-docstrings]
[--range line line] [--docstring-length length length]
[--non-strict] [--config CONFIG] [--version] files [files ...]

positional arguments:
files files to format or '-' for standard in
Expand Down Expand Up @@ -97,6 +97,9 @@ def _help():
place closing triple quotes on a new-line when a
one-line docstring wraps to two or more lines
(default: False)
--preserve-module-docstrings
preserve module docstrings while formatting other
docstrings
--range line line apply docformatter to docstrings between these lines;
line numbers are indexed at 1 (default: None)
--docstring-length length length
Expand Down
11 changes: 11 additions & 0 deletions src/docformatter/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ def do_parse_arguments(self) -> None: # noqa: PLR0915
"one-line docstring wraps to two or more lines "
"(default: False)",
)
self.parser.add_argument(
"--preserve-module-docstrings",
action="store_true",
default=str(self.flargs.get("preserve-module-docstrings", "false")).lower()
== "true",
help="preserve module docstrings while formatting other docstrings",
)
self.parser.add_argument(
"--range",
metavar="line",
Expand Down Expand Up @@ -358,6 +365,10 @@ def do_parse_arguments(self) -> None: # noqa: PLR0915
if "-" in self.args.files and not _cli_in_place:
self.args.in_place = False

self._do_validate_range_arguments()

def _do_validate_range_arguments(self) -> None:
"""Validate the docstring line and length ranges."""
if self.args.line_range:
if self.args.line_range[0] <= 0:
self.parser.error("--range must be positive numbers")
Expand Down
2 changes: 2 additions & 0 deletions src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@ def _do_rewrite_docstring_blocks(
"""
# print(tokens)
_blocks = _classify.do_find_docstring_blocks(tokens)
if getattr(self.args, "preserve_module_docstrings", False):
_blocks = [block for block in _blocks if block[2] != "module"]
_new_blocks: list[tuple[int, int, str]] = []
_skip_indices: set[int] = set()
self.new_tokens = []
Expand Down
34 changes: 34 additions & 0 deletions tests/test_oss_regression_126.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Regression for preserving a module docstring during formatting."""

import io

import pytest

from docformatter import Configurater, Formatter


@pytest.mark.integration
def test_preserve_module_docstring():
"""Keep the module header while formatting a function docstring."""
header = (
"#!/usr/bin/env python\n"
"# Generated file.\n"
"\n\n"
'"""license header\n'
"\n"
'module description"""\n'
"\n"
)
source = header + 'def example():\n """function summary"""\n'
expected = header + 'def example():\n """Function summary."""\n'
configuration = Configurater(["docformatter", "-"])
configuration.flargs = {"preserve-module-docstrings": "true"}
configuration.do_parse_arguments()
output = io.StringIO()
formatter = Formatter(
configuration.args, io.StringIO(), io.StringIO(source), output
)

formatter.do_format_standard_in(configuration.parser)

assert output.getvalue() == expected