Skip to content

Commit aa97f01

Browse files
authored
Fixed normalize_source_lines messing with the indentation of method (#198)
1 parent d9e5c32 commit aa97f01

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## dev
4+
5+
- Fixed `normalize_source_lines()` messing with the indentation of methods with decorators that have parameters starting with `def`.
6+
37
## 1.14.0
48

59
- Added `typehints_defaults` config option allowing to automatically annotate parameter defaults.

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def remove_prefix(text: str, prefix: str) -> str:
176176

177177
# Find the line and line number containing the function definition
178178
for i, l in enumerate(lines):
179-
if l.lstrip().startswith("def"):
179+
if l.lstrip().startswith("def "):
180180
idx = i
181181
whitespace_separator = "def"
182182
break

tests/test_sphinx_autodoc_typehints.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,39 @@ def __init__(self): pass
679679
"""
680680

681681
assert normalize_source_lines(dedent(source)) == dedent(expected)
682+
683+
684+
def test_normalize_source_lines_def_starting_decorator_parameter() -> None:
685+
source = """
686+
@_with_parameters(
687+
_Parameter("self", _Parameter.POSITIONAL_OR_KEYWORD),
688+
*_proxy_instantiation_parameters,
689+
_project_id,
690+
_Parameter(
691+
"node_numbers",
692+
_Parameter.POSITIONAL_OR_KEYWORD,
693+
default=None,
694+
annotation=Optional[Iterable[int]],
695+
),
696+
)
697+
def __init__(bound_args): # noqa: N805
698+
pass
699+
"""
700+
701+
expected = """
702+
@_with_parameters(
703+
_Parameter("self", _Parameter.POSITIONAL_OR_KEYWORD),
704+
*_proxy_instantiation_parameters,
705+
_project_id,
706+
_Parameter(
707+
"node_numbers",
708+
_Parameter.POSITIONAL_OR_KEYWORD,
709+
default=None,
710+
annotation=Optional[Iterable[int]],
711+
),
712+
)
713+
def __init__(bound_args): # noqa: N805
714+
pass
715+
"""
716+
717+
assert normalize_source_lines(dedent(source)) == dedent(expected)

0 commit comments

Comments
 (0)