[autodoc] Fix uncatched exception in get type hints #9509
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Subject: Fix uncatched exception in get type hints
Feature or Bugfix
Purpose
get_type_hints
byautodoc
from not runtime checkableForwardRef
may causeAttributeError
instead ofNameError
Detail
Some modules like
loguru
provide a stub file include types defined not at runtime. As mentioned inmypy
stub file guide, these types may cause errors if used in runtime.Error occured because of the following code in autodoc:
sphinx/sphinx/ext/autodoc/__init__.py
Lines 1981 to 2005 in f590268
Following Code will raise
AttributeError
when building:Following Code will be processed successfully (
NameError
has been caught):If
AttributeError
is catched inget_type_hints
, theseForwardRef
will be treated as raw string and building will be successful. But, i dont know if this change may cause some unexpected building success.Full Example
Repo: https://github.com/yanyongyu/loguru-issue480
Build Log: https://github.com/yanyongyu/loguru-issue480/runs/3177938743
Relates