Skip to content

Commit 62e9606

Browse files
authored
Restore support for cut_lines() with no object type (#13015)
1 parent 5ae32ce commit 62e9606

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Release 8.1.3 (in development)
44
Bugs fixed
55
----------
66

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

810
Release 8.1.2 (released Oct 12, 2024)
911
=====================================

sphinx/ext/autodoc/__init__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def merge_members_option(options: dict) -> None:
186186
# Some useful event listener factories for autodoc-process-docstring.
187187

188188
def cut_lines(
189-
pre: int, post: int = 0, what: str | list[str] | None = None
189+
pre: int, post: int = 0, what: Sequence[str] | None = None
190190
) -> _AutodocProcessDocstringListener:
191191
"""Return a listener that removes the first *pre* and last *post*
192192
lines of every docstring. If *what* is a sequence of strings,
@@ -199,7 +199,12 @@ def cut_lines(
199199
200200
This can (and should) be used in place of :confval:`automodule_skip_lines`.
201201
"""
202-
what_unique = frozenset(what or ())
202+
if not what:
203+
what_unique: frozenset[str] = frozenset()
204+
elif isinstance(what, str): # strongly discouraged
205+
what_unique = frozenset({what})
206+
else:
207+
what_unique = frozenset(what)
203208

204209
def process(
205210
app: Sphinx,
@@ -209,7 +214,7 @@ def process(
209214
options: dict[str, bool],
210215
lines: list[str],
211216
) -> None:
212-
if what_ not in what_unique:
217+
if what_unique and what_ not in what_unique:
213218
return
214219
del lines[:pre]
215220
if post:

tests/test_extensions/test_ext_autodoc_events.py

+20
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ def test_cut_lines(app):
5858
]
5959

6060

61+
def test_cut_lines_no_objtype():
62+
docstring_lines = [
63+
'first line',
64+
'---',
65+
'second line',
66+
'---',
67+
'third line ',
68+
'',
69+
]
70+
process = cut_lines(2)
71+
72+
process(None, 'function', 'func', None, {}, docstring_lines) # type: ignore[arg-type]
73+
assert docstring_lines == [
74+
'second line',
75+
'---',
76+
'third line ',
77+
'',
78+
]
79+
80+
6181
@pytest.mark.sphinx('html', testroot='ext-autodoc')
6282
def test_between(app):
6383
app.connect('autodoc-process-docstring', between('---', ['function']))

0 commit comments

Comments
 (0)