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
2 changes: 1 addition & 1 deletion .github/workflows/runtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ['3.10', '3.11', '3.12']
python-version: ['3.10', '3.11', '3.12', "3.13"]


# only run if CI isn't turned off
Expand Down
9 changes: 7 additions & 2 deletions src/obsplus/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def compose_docstring(**kwargs: str | Sequence[str]):

Examples
--------
```{python}
from dascore.utils.docs import compose_docstring

@compose_docstring(some_value='10')
def example_function():
'''
Expand All @@ -33,23 +36,25 @@ def example_function():
The following line will be the string '10':
{some_value}
'''
```
"""

def _wrap(func):
docstring = func.__doc__
assert isinstance(docstring, str)
# iterate each provided value and look for it in the docstring
for key, value in kwargs.items():
value = value if isinstance(value, str) else "\n".join(value)
# strip out first line if needed
value = value.lstrip()
value = textwrap.dedent(value).lstrip()
search_value = f"{{{key}}}"
# find all lines that match values
lines = [x for x in docstring.split("\n") if search_value in x]
for line in lines:
# determine number of spaces used before matching character
spaces = line.split(search_value)[0]
# ensure only spaces precede search value
assert set(spaces) == {" "}
assert set(spaces) == {" "} or not len(spaces)
new = textwrap.indent(textwrap.dedent(value), spaces)
docstring = docstring.replace(line, new)

Expand Down
12 changes: 8 additions & 4 deletions tests/test_utils/test_doc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from obsplus.utils.docs import compose_docstring, format_dtypes


class TestDocsting:
class TestDocstring:
"""tests for obsplus' simple docstring substitution function."""

def count_white_space(self, some_str):
Expand All @@ -19,23 +19,27 @@ def test_docstring(self):
"""
Parameters
----------
a: int
a
a
b int
b
b
"""
)

@compose_docstring(params=params)
def testfun1():
"""
A simple test function.

{params}
"""

assert "Parameters" in testfun1.__doc__
line = next(x for x in testfun1.__doc__.split("\n") if "Parameters" in x)
base_spaces = line.split("Parameters")[0]
assert len(base_spaces) == 12
# py3.13+ automatically strips white space from docstrings so 12
# and 0 are valid lengths.
assert len(base_spaces) in {12, 0}

def test_list_indent(self):
"""Ensure lists are indented equally."""
Expand Down
Loading