Skip to content

Commit 1cc8130

Browse files
authored
Normalize async functions properly (#182)
1 parent 05b31d6 commit 1cc8130

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

sphinx_autodoc_typehints.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,19 @@ def remove_prefix(text, prefix):
170170
for i, l in enumerate(sourcelines):
171171
if l.lstrip().startswith("def"):
172172
idx = i
173+
whitespace_separator = "def"
173174
break
175+
elif l.lstrip().startswith("async def"):
176+
idx = i
177+
whitespace_separator = "async def"
178+
break
179+
174180
else:
175181
return "\n".join(sourcelines)
176182
fn_def = sourcelines[idx]
177183

178184
# Get a string representing the amount of leading whitespace
179-
whitespace = fn_def.split("def")[0]
185+
whitespace = fn_def.split(whitespace_separator)[0]
180186

181187
# Add this leading whitespace to all lines before and after the `def`
182188
aligned_prefix = [whitespace + remove_prefix(s, whitespace) for s in sourcelines[:idx]]

tests/test_sphinx_autodoc_typehints.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from sphinx_autodoc_typehints import (
1414
format_annotation, get_annotation_args, get_annotation_class_name, get_annotation_module,
15-
process_docstring)
15+
normalize_source_lines, process_docstring)
1616

1717
T = TypeVar('T')
1818
U = TypeVar('U', covariant=True)
@@ -526,3 +526,19 @@ class dummy_module.DataClass(x)
526526
''')
527527
expected_contents = expected_contents.format(**format_args).replace('–', '--')
528528
assert text_contents == expected_contents
529+
530+
531+
def test_normalize_source_lines_async_def():
532+
source = textwrap.dedent("""
533+
async def async_function():
534+
class InnerClass:
535+
def __init__(self): pass
536+
""")
537+
538+
expected = textwrap.dedent("""
539+
async def async_function():
540+
class InnerClass:
541+
def __init__(self): pass
542+
""")
543+
544+
assert normalize_source_lines(source) == expected

0 commit comments

Comments
 (0)