Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39421eb

Browse files
committed
Recover from invalid type hint (felix-hilden#158)
1 parent 9fe1037 commit 39421eb

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

docs/src/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ These release notes are based on
88
sphinx-codeautolink adheres to
99
`Semantic Versioning <https://semver.org>`_.
1010

11+
Unreleased
12+
----------
13+
- Fix regression in not handling invalid return type hints (:issue:`158`)
14+
1115
0.16.0 (2025-01-11)
1216
-------------------
1317
- Declare support for Python 3.12 and 3.13 (:issue:`150`)

src/sphinx_codeautolink/extension/resolve.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ def call_value(cursor: Cursor) -> None:
118118

119119
def get_return_annotation(func: Callable) -> type | None:
120120
"""Determine the target of a function return type hint."""
121-
annotation = get_type_hints(func).get("return")
121+
try:
122+
annotation = get_type_hints(func).get("return")
123+
except NameError as e:
124+
msg = f"Unable to follow return annotation of {get_name_for_debugging(func)}."
125+
raise CouldNotResolve(msg) from e
122126

123127
# Inner type from typing.Optional or Union[None, T]
124128
origin = getattr(annotation, "__origin__", None)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
future_project
2+
future_project.invalid_ref
3+
# split
4+
codeautolink_warn_on_failed_resolve = False
5+
# split
6+
Test project
7+
============
8+
9+
.. code:: python
10+
11+
import future_project
12+
future_project.invalid_ref().whatever
13+
14+
.. automodule:: future_project

tests/extension/src/future_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ def optional() -> Optional[Foo]: # noqa: UP007
1616

1717
def optional_manual() -> None | Foo:
1818
"""Return manually constructed optional type."""
19+
20+
21+
def invalid_ref() -> NotAClass: # noqa: F821
22+
"""Reference to a nonexistent class."""

0 commit comments

Comments
 (0)