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

Add warning type and subtype #433

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ The following configuration options are accepted:
code or `None` to fall back to the default formatter.
- `typehints_use_signature` (default: `False`): If `True`, typehints for parameters in the signature are shown.
- `typehints_use_signature_return` (default: `False`): If `True`, return annotations in the signature are shown.
- `suppress_warnings`: sphinx-autodoc-typehints supports to suppress warning messages via Sphinx's `suppress_warnings`. It allows following additional warning types:
- `sphinx_autodoc_typehints`
- `sphinx_autodoc_typehints.comment`
- `sphinx_autodoc_typehints.forward_reference`
- `sphinx_autodoc_typehints.guarded_import`
- `sphinx_autodoc_typehints.local_function`

## How it works

Expand Down
37 changes: 31 additions & 6 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,12 @@ def process_signature( # noqa: C901, PLR0913, PLR0917
elif what == "method":
# bail if it is a local method as we cannot determine if first argument needs to be deleted or not
if "<locals>" in obj.__qualname__ and not _is_dataclass(name, what, obj.__qualname__):
_LOGGER.warning('Cannot handle as a local function: "%s" (use @functools.wraps)', name)
_LOGGER.warning(
'Cannot handle as a local function: "%s" (use @functools.wraps)',
name,
type="sphinx_autodoc_typehints",
subtype="local_function",
)
return None
outer = inspect.getmodule(obj)
for class_name in obj.__qualname__.split(".")[:-1]:
Expand Down Expand Up @@ -453,7 +458,9 @@ def _execute_guarded_code(autodoc_mock_imports: list[str], obj: Any, module_code
with mock(autodoc_mock_imports):
exec(guarded_code, getattr(obj, "__globals__", obj.__dict__)) # noqa: S102
except Exception as exc: # noqa: BLE001
_LOGGER.warning("Failed guarded type import with %r", exc)
_LOGGER.warning(
"Failed guarded type import with %r", exc, type="sphinx_autodoc_typehints", subtype="guarded_import"
)


def _resolve_type_guarded_imports(autodoc_mock_imports: list[str], obj: Any) -> None:
Expand Down Expand Up @@ -487,7 +494,13 @@ def _get_type_hint(autodoc_mock_imports: list[str], name: str, obj: Any) -> dict
else:
result = {}
except NameError as exc:
_LOGGER.warning('Cannot resolve forward reference in type annotations of "%s": %s', name, exc)
_LOGGER.warning(
'Cannot resolve forward reference in type annotations of "%s": %s',
name,
exc,
type="sphinx_autodoc_typehints",
subtype="forward_reference",
)
result = obj.__annotations__
return result

Expand All @@ -505,7 +518,12 @@ def backfill_type_hints(obj: Any, name: str) -> dict[str, Any]: # noqa: C901, P
def _one_child(module: Module) -> stmt | None:
children = module.body # use the body to ignore type comments
if len(children) != 1:
_LOGGER.warning('Did not get exactly one node from AST for "%s", got %s', name, len(children))
_LOGGER.warning(
'Did not get exactly one node from AST for "%s", got %s',
name,
len(children),
type="sphinx_autodoc_typehints",
)
return None
return children[0]

Expand All @@ -530,7 +548,12 @@ def _one_child(module: Module) -> stmt | None:
try:
comment_args_str, comment_returns = type_comment.split(" -> ")
except ValueError:
_LOGGER.warning('Unparseable type hint comment for "%s": Expected to contain ` -> `', name)
_LOGGER.warning(
'Unparseable type hint comment for "%s": Expected to contain ` -> `',
name,
type="sphinx_autodoc_typehints",
subtype="comment",
)
return {}

rv = {}
Expand All @@ -545,7 +568,9 @@ def _one_child(module: Module) -> stmt | None:
comment_args.insert(0, None) # self/cls may be omitted in type comments, insert blank

if len(args) != len(comment_args):
_LOGGER.warning('Not enough type comments found on "%s"', name)
_LOGGER.warning(
'Not enough type comments found on "%s"', name, type="sphinx_autodoc_typehints", subtype="comment"
)
return rv

for at, arg in enumerate(args):
Expand Down