Skip to content

Commit

Permalink
Avoid "Explicit markup ends without a blank line" when the decorated …
Browse files Browse the repository at this point in the history
…function has no docstring.
  • Loading branch information
tantale committed Feb 4, 2021
1 parent a02525b commit f1ad5b4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ v1.2.12 (unreleased)

Bug fix release

Fix
---

- Avoid "Explicit markup ends without a blank line" when the decorated function has no docstring.


Other
-----

Expand Down
3 changes: 3 additions & 0 deletions deprecated/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def __call__(self, wrapped):
if docstring:
# An empty line must separate the original docstring and the directive.
docstring = re.sub(r"\n+$", "", docstring, flags=re.DOTALL) + "\n\n"
else:
# Avoid "Explicit markup ends without a blank line" when the decorated function has no docstring
docstring = "\n"

# -- append the directive division to the docstring
docstring += "".join("{}\n".format(line) for line in div_lines)
Expand Down
52 changes: 29 additions & 23 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
:return: sum = *x* + *y*
""",
],
ids=["no_docstring", "short_docstring", "long_docstring"]
ids=["no_docstring", "short_docstring", "long_docstring"],
)
def docstring(request):
return request.param
Expand All @@ -44,32 +44,32 @@ def directive(request):
'1.2.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
{reason}
"""
.. {directive}:: {version}
{reason}
"""
),
),
(
None,
'1.2.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
"""
.. {directive}:: {version}
"""
),
),
(
'A good reason',
None,
textwrap.dedent(
"""\
.. {directive}::
{reason}
"""
.. {directive}::
{reason}
"""
),
),
],
ids=["reason&version", "version", "reason"]
ids=["reason&version", "version", "reason"],
)
def test_has_sphinx_docstring(docstring, directive, reason, version, expected):
# The function:
Expand All @@ -90,10 +90,13 @@ def foo(x, y):
current = textwrap.dedent(foo.__doc__)
assert current.endswith(expected)

# An empty line must separate the original docstring and the directive.
current = current.replace(expected, '')
if current:
if docstring:
# An empty line must separate the original docstring and the directive.
assert re.search("\n[ ]*\n$", current, flags=re.DOTALL)
else:
# Avoid "Explicit markup ends without a blank line" when the decorated function has no docstring
assert current == "\n"

with warnings.catch_warnings(record=True) as warns:
foo(1, 2)
Expand All @@ -118,32 +121,32 @@ def foo(x, y):
'1.2.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
{reason}
"""
.. {directive}:: {version}
{reason}
"""
),
),
(
None,
'1.2.0',
textwrap.dedent(
"""\
.. {directive}:: {version}
"""
.. {directive}:: {version}
"""
),
),
(
'A good reason',
None,
textwrap.dedent(
"""\
.. {directive}::
{reason}
"""
.. {directive}::
{reason}
"""
),
),
],
ids=["reason&version", "version", "reason"]
ids=["reason&version", "version", "reason"],
)
def test_cls_has_sphinx_docstring(docstring, directive, reason, version, expected):
# The class:
Expand All @@ -164,10 +167,13 @@ class Foo(object):
current = textwrap.dedent(Foo.__doc__)
assert current.endswith(expected)

# An empty line must separate the original docstring and the directive.
current = current.replace(expected, '')
if current:
if docstring:
# An empty line must separate the original docstring and the directive.
assert re.search("\n[ ]*\n$", current, flags=re.DOTALL)
else:
# Avoid "Explicit markup ends without a blank line" when the decorated function has no docstring
assert current == "\n"

with warnings.catch_warnings(record=True) as warns:
Foo()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sphinx_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def foo():

wrapped = adapter.__call__(foo)
expected = textwrap.dedent(
"""\
"""
.. {directive}:: 1.2.3
foo has changed in this version
Expand Down Expand Up @@ -117,7 +117,7 @@ def foo():
foo = decorator(foo)

expected = textwrap.dedent(
"""\
"""
.. {directive}:: 1.2.3
bar bar bar bar bar bar bar bar bar bar bar
bar bar bar bar bar bar bar bar bar bar bar
Expand Down

0 comments on commit f1ad5b4

Please sign in to comment.