Skip to content

Commit

Permalink
Fix requires_geos with methods (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Aug 22, 2021
1 parent 677f70b commit 636bbd6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version: 2
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py
fail_on_warning: true

# Build documentation with MkDocs
#mkdocs:
Expand Down
16 changes: 13 additions & 3 deletions pygeos/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ def wrapped(*args, **kwargs):
def wrapped(*args, **kwargs):
raise UnsupportedGEOSOperation(msg)

if wrapped.__doc__:
wrapped.__doc__ = wrapped.__doc__.replace(
"\n\n", "\n\n .. note:: {}\n\n".format(msg), 1
doc = wrapped.__doc__
if doc:
# Insert the message at the first double newline
position = doc.find("\n\n") + 2
# Figure out the indentation level
indent = 2
while True:
if doc[position + indent] == " ":
indent += 1
else:
break
wrapped.__doc__ = doc.replace(
"\n\n", "\n\n{}.. note:: {}\n\n".format(" " * indent, msg), 1
)

return wrapped
Expand Down
29 changes: 23 additions & 6 deletions pygeos/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,22 @@ def func():
"""


class SomeClass:
def func(self):
"""Docstring that will be mocked.
A multiline.
Some description.
"""


expected_docstring = """Docstring that will be mocked.
A multiline.
{indent}A multiline.
.. note:: 'func' requires at least GEOS {}.
{indent}.. note:: 'func' requires at least GEOS {version}.
Some description.
"""
{indent}Some description.
{indent}"""


@pytest.mark.parametrize("version", ["3.7.0", "3.7.1", "3.6.2"])
Expand All @@ -89,15 +98,23 @@ def test_requires_geos_not_ok(version, mocked_geos_version):
with pytest.raises(pygeos.UnsupportedGEOSOperation):
wrapped()

assert wrapped.__doc__ == expected_docstring.format(version)
assert wrapped.__doc__ == expected_docstring.format(version=version, indent=" " * 4)


@pytest.mark.parametrize("version", ["3.6.0", "3.8.0"])
def test_requires_geos_doc_build(version, mocked_geos_version, sphinx_doc_build):
"""The requires_geos decorator always adapts the docstring."""
wrapped = requires_geos(version)(func)

assert wrapped.__doc__ == expected_docstring.format(version)
assert wrapped.__doc__ == expected_docstring.format(version=version, indent=" " * 4)


@pytest.mark.parametrize("version", ["3.6.0", "3.8.0"])
def test_requires_geos_method(version, mocked_geos_version, sphinx_doc_build):
"""The requires_geos decorator adjusts methods docstrings correctly"""
wrapped = requires_geos(version)(SomeClass.func)

assert wrapped.__doc__ == expected_docstring.format(version=version, indent=" " * 8)


@multithreading_enabled
Expand Down

0 comments on commit 636bbd6

Please sign in to comment.