Skip to content

Commit 8a82cb8

Browse files
authored
Don't add :rtype: None (#538)
1 parent 05b79e3 commit 8a82cb8

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The following configuration options are accepted:
6767
or `Optional[int]`)
6868
- `typehints_document_rtype` (default: `True`): If `False`, never add an `:rtype:` directive. If `True`, add the
6969
`:rtype:` directive if no existing `:rtype:` is found.
70+
- `typehints_document_rtype_none` (default: `True`): If `False`, never add an `:rtype: None` directive. If `True`, add the `:rtype: None`.
7071
- `typehints_use_rtype` (default: `True`): Controls behavior when `typehints_document_rtype` is set to `True`. If
7172
`True`, document return type in the `:rtype:` directive. If `False`, document return type as part of the `:return:`
7273
directive, if present, otherwise fall back to using `:rtype:`. Use in conjunction with

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
912912
return InsertIndexInfo(insert_index=len(lines))
913913

914914

915-
def _inject_rtype( # noqa: PLR0913, PLR0917
915+
def _inject_rtype( # noqa: C901, PLR0913, PLR0917
916916
type_hints: dict[str, Any],
917917
original_obj: Any,
918918
app: Sphinx,
@@ -926,6 +926,8 @@ def _inject_rtype( # noqa: PLR0913, PLR0917
926926
return
927927
if not app.config.typehints_document_rtype:
928928
return
929+
if not app.config.typehints_document_rtype_none and type_hints["return"] is types.NoneType:
930+
return
929931

930932
r = get_insert_index(app, lines)
931933
if r is None:
@@ -1011,6 +1013,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
10111013
app.add_config_value("always_document_param_types", False, "html") # noqa: FBT003
10121014
app.add_config_value("typehints_fully_qualified", False, "env") # noqa: FBT003
10131015
app.add_config_value("typehints_document_rtype", True, "env") # noqa: FBT003
1016+
app.add_config_value("typehints_document_rtype_none", True, "env") # noqa: FBT003
10141017
app.add_config_value("typehints_use_rtype", True, "env") # noqa: FBT003
10151018
app.add_config_value("typehints_defaults", None, "env")
10161019
app.add_config_value("simplify_optional_unions", True, "env") # noqa: FBT003

tests/test_integration.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,19 @@ def typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator:
13191319
return a
13201320

13211321

1322+
@expected(
1323+
"""
1324+
mod.typehints_no_rtype_none()
1325+
1326+
Do something.
1327+
1328+
""",
1329+
typehints_document_rtype_none=False,
1330+
)
1331+
def typehints_no_rtype_none() -> None:
1332+
"""Do something."""
1333+
1334+
13221335
prolog = """
13231336
.. |test_node_start| replace:: {test_node_start}
13241337
""".format(test_node_start="test_start")

0 commit comments

Comments
 (0)