Skip to content

Commit

Permalink
Recover from invalid type hint (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-hilden committed Jan 15, 2025
1 parent 9fe1037 commit 39421eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ These release notes are based on
sphinx-codeautolink adheres to
`Semantic Versioning <https://semver.org>`_.

Unreleased
----------
- Fix regression in not handling invalid return type hints (:issue:`158`)

0.16.0 (2025-01-11)
-------------------
- Declare support for Python 3.12 and 3.13 (:issue:`150`)
Expand Down
6 changes: 5 additions & 1 deletion src/sphinx_codeautolink/extension/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def call_value(cursor: Cursor) -> None:

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

# Inner type from typing.Optional or Union[None, T]
origin = getattr(annotation, "__origin__", None)
Expand Down
14 changes: 14 additions & 0 deletions tests/extension/ref/ref_invalid_typehint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
future_project
future_project.invalid_ref
# split
codeautolink_warn_on_failed_resolve = False
# split
Test project
============

.. code:: python

import future_project
future_project.invalid_ref().whatever

.. automodule:: future_project
4 changes: 4 additions & 0 deletions tests/extension/src/future_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ def optional() -> Optional[Foo]: # noqa: UP007

def optional_manual() -> None | Foo:
"""Return manually constructed optional type."""


def invalid_ref() -> NotAClass: # noqa: F821
"""Reference to a nonexistent class."""

0 comments on commit 39421eb

Please sign in to comment.