Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[autodoc] Fix uncatched exception in get type hints #9509

Merged
merged 2 commits into from Jul 30, 2021

Conversation

yanyongyu
Copy link
Contributor

Subject: Fix uncatched exception in get type hints

Feature or Bugfix

  • Bugfix

Purpose

  • get_type_hints by autodoc from not runtime checkable ForwardRef may cause AttributeError instead of NameError

Detail

Some modules like loguru provide a stub file include types defined not at runtime. As mentioned in mypy stub file guide, these types may cause errors if used in runtime.

Error occured because of the following code in autodoc:

def add_directive_header(self, sig: str) -> None:
super().add_directive_header(sig)
sourcename = self.get_sourcename()
if self.options.annotation is SUPPRESS or self.should_suppress_directive_header():
pass
elif self.options.annotation:
self.add_line(' :annotation: %s' % self.options.annotation,
sourcename)
else:
if self.config.autodoc_typehints != 'none':
# obtain annotation for this data
annotations = get_type_hints(self.parent, None,
self.config.autodoc_type_aliases)
if self.objpath[-1] in annotations:
objrepr = stringify_typehint(annotations.get(self.objpath[-1]))
self.add_line(' :type: ' + objrepr, sourcename)
try:
if self.options.no_value or self.should_suppress_value_header():
pass
else:
objrepr = object_description(self.object)
self.add_line(' :value: ' + objrepr, sourcename)
except ValueError:
pass

Following Code will raise AttributeError when building:

import loguru

# loguru.Logger not exists at runtime actually
my_logger: "loguru.Logger" = loguru.logger

Following Code will be processed successfully (NameError has been caught):

from typing import TYPE_CHECKING

import loguru

if TYPE_CHECKING:
	from loguru import Logger

my_logger: "Logger" = loguru.logger

If AttributeError is catched in get_type_hints, these ForwardRef 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

@tk0miya
Copy link
Member

tk0miya commented Jul 29, 2021

Could merge the HEAD of 4.x branch, please? Then, CI will become passed.

@yanyongyu
Copy link
Contributor Author

Could merge the HEAD of 4.x branch, please? Then, CI will become passed.

Merged now.

@tk0miya tk0miya merged commit e344c3b into sphinx-doc:4.x Jul 30, 2021
@tk0miya
Copy link
Member

tk0miya commented Jul 30, 2021

Merged. Thank you for your work!

tk0miya added a commit to tk0miya/sphinx that referenced this pull request Jul 30, 2021
tk0miya added a commit that referenced this pull request Aug 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 30, 2021
@yanyongyu yanyongyu deleted the bugfix/get-type-hints branch September 23, 2022 12:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants