Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not double-process urls #284

Merged
merged 5 commits into from
Jul 31, 2024
Merged
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
24 changes: 12 additions & 12 deletions src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ def _field_over_url(
field_idx: List[Tuple[int, int]],
url_idx: List[Tuple[int, int]],
):
"""Remove URL indices that overlap with filed list indices.
"""Remove URL indices that overlap with field list indices.

Parameters
----------
Expand All @@ -965,14 +965,14 @@ def _field_over_url(
The url_idx list with any tuples that have indices overlapping with field
list indices removed.
"""
for _fieldl, _fieldu in field_idx:
for _key, _value in enumerate(url_idx):
if (
_value[0] == _fieldl
or _value[0] == _fieldu
or _value[1] == _fieldl
or _value[1] == _fieldu
):
url_idx.pop(_key)

return url_idx
if not field_idx:
return url_idx

nonoverlapping_urls = []

any_param_start = min(e[0] for e in field_idx)
for _key, _value in enumerate(url_idx):
if _value[1] < any_param_start:
nonoverlapping_urls.append(_value)
return nonoverlapping_urls

26 changes: 26 additions & 0 deletions tests/_data/string_files/do_format_docstrings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,29 @@ instring='''"""
eBay kinda suss
"""'''
outstring='''"""eBay kinda suss."""'''

[issue_263]
[issue_263.sphinx]
# the `xx.\n\n` ensures there are a summary and a description sections
# the `:param a:` creates a field
# the `b`s create text that is long enough to trigger a line wrap without being so long that they count as code
# the `s3://cccc.` is a url
instring='''"""xx.

:param a: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb s3://cccc.
"""'''
outstring='''"""xx.

:param a: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
s3://cccc.
"""'''
[issue_263.epytext]
instring='''"""xx.

@param a: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb s3://cccc.
"""'''
outstring='''"""xx.

@param a: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
s3://cccc.
"""'''
21 changes: 21 additions & 0 deletions tests/formatter/test_do_format_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,24 @@ def test_format_docstring_with_non_cap_words(self, test_args, args):
INDENTATION,
instring,
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [["--style", "sphinx", ""], ["--style", "epytext", ""]])
def test_do_not_double_process_urls(self, test_args, args):
"""Do not double-process urls in fields

See issue #263
"""
style = args[1]

uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

instring = self.TEST_STRINGS["issue_263"][style]["instring"]
outstring = self.TEST_STRINGS["issue_263"][style]["outstring"]

assert outstring == uut._do_format_docstring(INDENTATION, instring, )
75 changes: 75 additions & 0 deletions tests/test_syntax_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
do_find_links()
do_skip_link()
"""
import textwrap

# Third Party Imports
import pytest

# docformatter Package Imports
import docformatter
from docformatter import do_split_description


class TestURLHandlers:
Expand Down Expand Up @@ -159,3 +161,76 @@ def test_find_double_backtick_directives(self):
" Since a mail could be ``Cc``'d to two lists with different ``Reply-To`` munging"
"options set."
)


class TestSplitDescription:
"""Class for testing the function to process the description

Includes tests for:
- do_split_description()

"""
med_str = "m"*40 # long enough that 2 won't fit on a line
indent = " "

def do_test(self, text):
return do_split_description(textwrap.dedent(text), self.indent, 72, "sphinx")

def indent_all(self, strs):
return [self.indent + s for s in strs]

@pytest.mark.unit
def test_split_description_url_outside_param(self):
assert self.do_test(
f"""\
{self.med_str} https://{self.med_str}
:param a: {self.med_str}
"""
) == self.indent_all([
self.med_str,
f"https://{self.med_str}",
f":param a: {self.med_str}",
])

@pytest.mark.unit
def test_split_description_single_url_in_param(self):
assert self.do_test(
f"""\
{self.med_str}
:param a: {self.med_str} https://{self.med_str}a
"""
) == self.indent_all([
self.med_str,
f":param a: {self.med_str}",
self.indent + f"https://{self.med_str}a",
])

@pytest.mark.unit
def test_split_description_single_url_in_multiple_params(self):
assert self.do_test(
f"""\
{self.med_str}
:param a: {self.med_str} https://{self.med_str}a
:param b: {self.med_str} https://{self.med_str}b
"""
) == self.indent_all([
self.med_str,
f":param a: {self.med_str}",
self.indent + f"https://{self.med_str}a",
f":param b: {self.med_str}",
self.indent + f"https://{self.med_str}b",
])

@pytest.mark.unit
def test_split_description_multiple_urls_in_param(self):
assert self.do_test(
f"""\
{self.med_str}
:param a: {self.med_str} https://{self.med_str}0 https://{self.med_str}1
"""
) == self.indent_all([
self.med_str,
f":param a: {self.med_str}",
self.indent + f"https://{self.med_str}0",
self.indent + f"https://{self.med_str}1",
])
Loading