Skip to content

Commit 70ea438

Browse files
authored
Merge pull request #8841 from AWhetter/autodoc_signatures_without_backslash
Overloaded function signatures do not require a separating backslash
2 parents f7a2e08 + 94b5607 commit 70ea438

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

doc/usage/extensions/autodoc.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,19 @@ There are also config values that you can set:
529529
looks like a signature, use the line as the signature and remove it from the
530530
docstring content.
531531

532-
If the signature line ends with backslash, autodoc considers the function has
533-
multiple signatures and look at the next line of the docstring. It is useful
534-
for overloaded function.
532+
autodoc will continue to look for multiple signature lines,
533+
stopping at the first line that does not look like a signature.
534+
This is useful for declaring overloaded function signatures.
535535

536536
.. versionadded:: 1.1
537537
.. versionchanged:: 3.1
538538

539539
Support overloaded signatures
540540

541+
.. versionchanged:: 4.0
542+
543+
Overloaded signatures do not need to be separated by a backslash
544+
541545
.. confval:: autodoc_mock_imports
542546

543547
This value contains a list of modules to be mocked up. This is useful when

sphinx/ext/autodoc/__init__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,20 +1191,17 @@ def _find_signature(self) -> Tuple[str, str]:
11911191
break
11921192

11931193
if line.endswith('\\'):
1194-
multiline = True
11951194
line = line.rstrip('\\').rstrip()
1196-
else:
1197-
multiline = False
11981195

11991196
# match first line of docstring against signature RE
12001197
match = py_ext_sig_re.match(line)
12011198
if not match:
1202-
continue
1199+
break
12031200
exmod, path, base, args, retann = match.groups()
12041201

12051202
# the base name must match ours
12061203
if base not in valid_names:
1207-
continue
1204+
break
12081205

12091206
# re-prepare docstring to ignore more leading indentation
12101207
tab_width = self.directive.state.document.settings.tab_width # type: ignore
@@ -1218,13 +1215,6 @@ def _find_signature(self) -> Tuple[str, str]:
12181215
# subsequent signatures
12191216
self._signatures.append("(%s) -> %s" % (args, retann))
12201217

1221-
if multiline:
1222-
# the signature have multiple signatures on docstring
1223-
continue
1224-
else:
1225-
# don't look any further
1226-
break
1227-
12281218
if result:
12291219
# finish the loop when signature found
12301220
break

tests/roots/test-ext-autodoc/target/docstring_signature.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ class E:
2323
def __init__(self):
2424
"""E(foo: int, bar: int, baz: int) -> None \\
2525
E(foo: str, bar: str, baz: str) -> None"""
26+
27+
28+
class F:
29+
def __init__(self):
30+
"""F(foo: int, bar: int, baz: int) -> None
31+
F(foo: str, bar: str, baz: str) -> None"""

tests/test_ext_autodoc_configs.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ def test_autoclass_content_and_docstring_signature_class(app):
348348
'',
349349
'.. py:class:: E()',
350350
' :module: target.docstring_signature',
351-
''
351+
'',
352+
'',
353+
'.. py:class:: F()',
354+
' :module: target.docstring_signature',
355+
'',
352356
]
353357

354358

@@ -382,7 +386,12 @@ def test_autoclass_content_and_docstring_signature_init(app):
382386
'.. py:class:: E(foo: int, bar: int, baz: int) -> None',
383387
' E(foo: str, bar: str, baz: str) -> None',
384388
' :module: target.docstring_signature',
385-
''
389+
'',
390+
'',
391+
'.. py:class:: F(foo: int, bar: int, baz: int) -> None',
392+
' F(foo: str, bar: str, baz: str) -> None',
393+
' :module: target.docstring_signature',
394+
'',
386395
]
387396

388397

@@ -421,6 +430,11 @@ def test_autoclass_content_and_docstring_signature_both(app):
421430
' E(foo: str, bar: str, baz: str) -> None',
422431
' :module: target.docstring_signature',
423432
'',
433+
'',
434+
'.. py:class:: F(foo: int, bar: int, baz: int) -> None',
435+
' F(foo: str, bar: str, baz: str) -> None',
436+
' :module: target.docstring_signature',
437+
'',
424438
]
425439

426440

0 commit comments

Comments
 (0)