Skip to content

Don't add :rtype: None #538

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

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The following configuration options are accepted:
or `Optional[int]`)
- `typehints_document_rtype` (default: `True`): If `False`, never add an `:rtype:` directive. If `True`, add the
`:rtype:` directive if no existing `:rtype:` is found.
- `typehints_document_rtype_none` (default: `True`): If `False`, never add an `:rtype: None` directive. If `True`, add the `:rtype: None`.
- `typehints_use_rtype` (default: `True`): Controls behavior when `typehints_document_rtype` is set to `True`. If
`True`, document return type in the `:rtype:` directive. If `False`, document return type as part of the `:return:`
directive, if present, otherwise fall back to using `:rtype:`. Use in conjunction with
Expand Down
5 changes: 4 additions & 1 deletion src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
return InsertIndexInfo(insert_index=len(lines))


def _inject_rtype( # noqa: PLR0913, PLR0917
def _inject_rtype( # noqa: C901, PLR0913, PLR0917
type_hints: dict[str, Any],
original_obj: Any,
app: Sphinx,
Expand All @@ -919,6 +919,8 @@ def _inject_rtype( # noqa: PLR0913, PLR0917
return
if not app.config.typehints_document_rtype:
return
if not app.config.typehints_document_rtype_none and type_hints["return"] is types.NoneType:
return

r = get_insert_index(app, lines)
if r is None:
Expand Down Expand Up @@ -1004,6 +1006,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
app.add_config_value("always_document_param_types", False, "html") # noqa: FBT003
app.add_config_value("typehints_fully_qualified", False, "env") # noqa: FBT003
app.add_config_value("typehints_document_rtype", True, "env") # noqa: FBT003
app.add_config_value("typehints_document_rtype_none", True, "env") # noqa: FBT003
app.add_config_value("typehints_use_rtype", True, "env") # noqa: FBT003
app.add_config_value("typehints_defaults", None, "env")
app.add_config_value("simplify_optional_unions", True, "env") # noqa: FBT003
Expand Down
13 changes: 13 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,19 @@ def typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator:
return a


@expected(
"""
mod.typehints_no_rtype_none()

Do something.

""",
typehints_document_rtype_none=False,
)
def typehints_no_rtype_none() -> None:
"""Do something."""


prolog = """
.. |test_node_start| replace:: {test_node_start}
""".format(test_node_start="test_start")
Expand Down