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

Restore support for cut_lines() with no object type #13015

Merged
merged 2 commits into from
Oct 12, 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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Release 8.1.3 (in development)
Bugs fixed
----------

* #13013: Restore support for :func:`!cut_lines` with no object type.
Patch by Adam Turner.

Release 8.1.2 (released Oct 12, 2024)
=====================================
Expand Down
11 changes: 8 additions & 3 deletions sphinx/ext/autodoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def merge_members_option(options: dict) -> None:
# Some useful event listener factories for autodoc-process-docstring.

def cut_lines(
pre: int, post: int = 0, what: str | list[str] | None = None
pre: int, post: int = 0, what: Sequence[str] | None = None
) -> _AutodocProcessDocstringListener:
"""Return a listener that removes the first *pre* and last *post*
lines of every docstring. If *what* is a sequence of strings,
Expand All @@ -199,7 +199,12 @@ def cut_lines(

This can (and should) be used in place of :confval:`automodule_skip_lines`.
"""
what_unique = frozenset(what or ())
if not what:
what_unique: frozenset[str] = frozenset()
elif isinstance(what, str): # strongly discouraged
what_unique = frozenset({what})
else:
what_unique = frozenset(what)

def process(
app: Sphinx,
Expand All @@ -209,7 +214,7 @@ def process(
options: dict[str, bool],
lines: list[str],
) -> None:
if what_ not in what_unique:
if what_unique and what_ not in what_unique:
return
del lines[:pre]
if post:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_extensions/test_ext_autodoc_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def test_cut_lines(app):
]


def test_cut_lines_no_objtype():
docstring_lines = [
'first line',
'---',
'second line',
'---',
'third line ',
'',
]
process = cut_lines(2)

process(None, 'function', 'func', None, {}, docstring_lines) # type: ignore[arg-type]
assert docstring_lines == [
'second line',
'---',
'third line ',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_between(app):
app.connect('autodoc-process-docstring', between('---', ['function']))
Expand Down
Loading