Skip to content

Commit 1e25828

Browse files
committed
Fix crash when inspect moduel returns invalid Python code
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 6b86a6a commit 1e25828

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
# Changelog
22

3+
## dev
4+
5+
- Fix crash when the `inspect` module returns an invalid python syntax source
6+
37
## 1.14.1
48

5-
- Fixed `normalize_source_lines()` messing with the indentation of methods with decorators that have parameters starting with `def`.
9+
- Fixed `normalize_source_lines()` messing with the indentation of methods with decorators that have parameters starting
10+
with `def`.
611
- Handle `ValueError` or `TypeError` being raised when signature of an object cannot be determined
7-
- Fix `KeyError` being thrown when argument is not documented (e.g. `cls` argument for class methods, and `self` for methods)
12+
- Fix `KeyError` being thrown when argument is not documented (e.g. `cls` argument for class methods, and `self` for
13+
methods)
814

915
## 1.14.0
1016

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

1319
## 1.13.1
1420

15-
- Fixed ``NewType`` inserts a reference as first argument instead of a string
21+
- Fixed `NewType` inserts a reference as first argument instead of a string
1622

1723
## 1.13.0
1824

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The following configuration options are accepted:
6363
`:rtype:` directive if no existing `:rtype:` is found.
6464
- `typehints_defaults` (default: `None`): If `None`, defaults are not added. Otherwise adds a default annotation:
6565

66-
- `'comma'` adds it after the type, changing Sphinx’ default look to “**param** (*int*, default: `1`) -- text”.
66+
- `'comma'` adds it after the type, changing Sphinx’ default look to “**param** (_int_, default: `1`) -- text”.
6767
- `'braces'` adds `(default: ...)` after the type (useful for numpydoc like styles).
6868
- `'braces-after'` adds `(default: ...)` at the end of the parameter documentation text instead.
6969

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _one_child(module: Module) -> stmt | None:
315315
try:
316316
code = textwrap.dedent(normalize_source_lines(inspect.getsource(obj)))
317317
obj_ast = ast.parse(code, **parse_kwargs) # type: ignore # dynamic kwargs
318-
except (OSError, TypeError):
318+
except (OSError, TypeError, SyntaxError):
319319
return {}
320320

321321
obj_ast = _one_child(obj_ast)

tests/test_sphinx_autodoc_typehints.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from sphobjinv import Inventory
3636

3737
from sphinx_autodoc_typehints import (
38+
backfill_type_hints,
3839
format_annotation,
3940
get_annotation_args,
4041
get_annotation_class_name,
@@ -751,3 +752,12 @@ def test_bound_class_method(method: FunctionType) -> None:
751752
)
752753
app: Sphinx = create_autospec(Sphinx, config=config)
753754
process_docstring(app, "class", method.__qualname__, method, None, [])
755+
756+
757+
def test_syntax_error_backfill() -> None:
758+
# Regression test for #188
759+
# fmt: off
760+
func = ( # Note: line break here is what previously led to SyntaxError in process_docstring
761+
lambda x: x)
762+
# fmt: on
763+
backfill_type_hints(func, "func")

0 commit comments

Comments
 (0)