diff --git a/README.md b/README.md index 7ddbf09..4da9476 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index 432fdf4..4abf792 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -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, @@ -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: @@ -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 diff --git a/tests/test_integration.py b/tests/test_integration.py index 900d585..2b53940 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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")