diff --git a/docs/source/conf.py b/docs/source/conf.py index 76f6516..b846699 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -11,7 +11,7 @@ project = "docformatter" copyright = "2022-2023, Steven Myint" author = "Steven Myint" -release = "1.7.2" +release = "1.7.3-alpha" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration diff --git a/pyproject.toml b/pyproject.toml index 681d75f..4fc78ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "docformatter" -version = "1.7.2" +version = "1.7.3-alpha" description = "Formats docstrings to follow PEP 257" authors = ["Steven Myint"] maintainers = [ diff --git a/src/docformatter/__pkginfo__.py b/src/docformatter/__pkginfo__.py index a1bc676..f304d7d 100644 --- a/src/docformatter/__pkginfo__.py +++ b/src/docformatter/__pkginfo__.py @@ -23,4 +23,4 @@ # SOFTWARE. """Package information for docformatter.""" -__version__ = "1.7.2" +__version__ = "1.7.3-alpha" diff --git a/src/docformatter/syntax.py b/src/docformatter/syntax.py index 3f5faa0..8201594 100644 --- a/src/docformatter/syntax.py +++ b/src/docformatter/syntax.py @@ -162,7 +162,7 @@ def description_to_list( - text: str, + description: str, indentation: str, wrap_length: int, ) -> List[str]: @@ -170,7 +170,7 @@ def description_to_list( Parameters ---------- - text : str + description : str The docstring description. indentation : str The indentation (number of spaces or tabs) to place in front of each @@ -180,38 +180,43 @@ def description_to_list( Returns ------- - _lines : list - A list containing each line of the description with any links put - back together. + _wrapped_lines : list + A list containing each line of the description wrapped at wrap_length. """ # This is a description containing only one paragraph. - if len(re.findall(r"\n\n", text)) <= 0: + if len(re.findall(r"\n\n", description)) <= 0: return textwrap.wrap( - textwrap.dedent(text), + textwrap.dedent(description), width=wrap_length, initial_indent=indentation, subsequent_indent=indentation, ) # This is a description containing multiple paragraphs. - _lines = [] - for _line in text.split("\n\n"): - _text = textwrap.wrap( + _wrapped_lines = [] + for _line in description.split("\n\n"): + _wrapped_line = textwrap.wrap( textwrap.dedent(_line), width=wrap_length, initial_indent=indentation, subsequent_indent=indentation, ) - if _text: - _lines.extend(_text) - _lines.append("") + if _wrapped_line: + _wrapped_lines.extend(_wrapped_line) + _wrapped_lines.append("") with contextlib.suppress(IndexError): - if not _lines[-1] and not _lines[-2]: - _lines.pop(-1) + if not _wrapped_lines[-1] and not _wrapped_lines[-2]: + _wrapped_lines.pop(-1) - return _lines + if ( + description[-len(indentation) - 1 : -len(indentation)] == "\n" + and description[-len(indentation) - 2 : -len(indentation)] != "\n\n" + ): + _wrapped_lines.pop(-1) + + return _wrapped_lines def do_clean_url(url: str, indentation: str) -> str: @@ -479,13 +484,10 @@ def do_wrap_field_lists( # noqa: PLR0913 text, field_idx, _idx, - ).strip() + ) - if len(f"{_field_name} {_field_body}") <= (wrap_length - len(indentation)): - if _field_body.startswith("`") or not _field_body: - _field = f"{_field_name}{_field_body}" - else: - _field = f"{_field_name} {_field_body}" + if len(f"{_field_name}{_field_body}") <= (wrap_length - len(indentation)): + _field = f"{_field_name}{_field_body}" lines.append(f"{indentation}{_field}") else: lines.extend( @@ -855,8 +857,10 @@ def _do_join_field_body(text, field_idx, idx): [_line.strip() for _line in _field_body.splitlines()] ).strip() - if _field_body: + if not _field_body.startswith("`"): _field_body = f" {_field_body}" + if text[field_idx[idx][1] : field_idx[idx][1] + 1] == "\n": + _field_body = "\n" return _field_body @@ -886,7 +890,7 @@ def _do_wrap_field(field_name, field_body, indentation, wrap_length): _subsequent = 2 * indentation _wrapped_field = textwrap.wrap( - textwrap.dedent(f"{field_name} {field_body.strip()}"), + textwrap.dedent(f"{field_name}{field_body}"), width=wrap_length, initial_indent=indentation, subsequent_indent=_subsequent, diff --git a/tests/test_format_docstring.py b/tests/test_format_docstring.py index 3fc0065..7440d13 100644 --- a/tests/test_format_docstring.py +++ b/tests/test_format_docstring.py @@ -2150,7 +2150,7 @@ def test_format_docstring_sphinx_style_field_body_is_a_link( ): """Should not add a space after the field name when the body is a link. - See docformatter_10.4.3.1, issue #229, and issue #230. + See docformatter_10.4.3.1, issue #229, issue #230, issue #234, and issue #235. """ uut = Formatter( test_args, @@ -2199,6 +2199,52 @@ def test_format_docstring_sphinx_style_field_body_is_a_link( ) ) + assert ( + ( + '''\ +"""CC. + + :math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX + """\ +''' + ) + == uut._do_format_docstring( + INDENTATION, + '''\ + """CC. + + :math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX + """\ +''', + ) + ) + + assert ( + ( + '''\ +"""CC. + + C. + + C, + :math:`[0, 1]`. + """\ +''' + ) + == uut._do_format_docstring( + INDENTATION, + '''\ +"""CC. + + C. + + C, + :math:`[0, 1]`. +"""\ +''', + ) + ) + @pytest.mark.unit @pytest.mark.parametrize( "args", @@ -2234,6 +2280,7 @@ def test_format_docstring_sphinx_style_field_body_is_blank( """Add trackers to a torrent. :raises NotFound404Error: + :param torrent_hash: hash for torrent :param urls: tracker URLs to add to torrent :return: None