Skip to content

Commit

Permalink
Add the autodoc_use_type_comments option (#13269)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored Jan 26, 2025
1 parent 44891a4 commit bde37be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ Features added
and rename the :rst:dir:`autosummary` directive's ``nosignatures``option
to :rst:dir:`no-signatures``.
Patch by Adam Turner.
* #13269: Added the option to disable the use of type comments in
via the new :confval:`autodoc_use_type_comments` option,
which defaults to ``True`` for backwards compatibility.
The default will change to ``False`` in Sphinx 10.
Patch by Adam Turner.

Bugs fixed
----------
Expand Down
20 changes: 20 additions & 0 deletions doc/usage/extensions/autodoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,26 @@ There are also config values that you can set:
Added as an experimental feature. This will be integrated into autodoc core
in the future.

.. confval:: autodoc_use_type_comments
:type: :code-py:`bool`
:default: :code-py:`True`

Attempt to read ``# type: ...`` comments from source code
to supplement missing type annotations, if True.

This can be disabled if your source code does not use type comments,
for example if it exclusively uses type annotations or
does not use type hints of any kind.

.. versionadded:: 8.2

Added the option to disable the use of type comments in
via the new :confval:`!autodoc_use_type_comments` option,
which defaults to :code-py:`True` for backwards compatibility.
The default will change to :code-py:`False` in Sphinx 10.

.. xref RemovedInSphinx10Warning
.. confval:: autodoc_warningiserror
:type: :code-py:`bool`
:default: :code-py:`True`
Expand Down
6 changes: 6 additions & 0 deletions sphinx/ext/autodoc/type_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def update_annotations_using_type_comments(
app: Sphinx, obj: Any, bound_method: bool
) -> None:
"""Update annotations info of *obj* using type_comments."""
if not app.config.autodoc_use_type_comments:
return

try:
type_sig = get_type_comment(obj, bound_method)
if type_sig:
Expand All @@ -152,6 +155,9 @@ def update_annotations_using_type_comments(


def setup(app: Sphinx) -> ExtensionMetadata:
app.add_config_value(
'autodoc_use_type_comments', True, 'env', types=frozenset({bool})
)
app.connect(
'autodoc-before-process-signature', update_annotations_using_type_comments
)
Expand Down

0 comments on commit bde37be

Please sign in to comment.