Skip to content

Commit

Permalink
autodoc: The default of autodoc_typehints_format becomes to 'smart'
Browse files Browse the repository at this point in the history
The default value of autodoc_typehints_format configuration is changed
to `'smart'`.  It will suppress the leading module names of typehints
(ex. `io.StringIO` -> `StringIO`).

refs: sphinx-doc#9075
  • Loading branch information
tk0miya committed Jan 1, 2022
1 parent 6ee3dd1 commit 01e6436
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -25,6 +25,10 @@ Deprecated
Features added
--------------

* #9075: autodoc: The default value of :confval:`autodoc_typehints_format` is
changed to ``'smart'``. It will suppress the leading module names of
typehints (ex. ``io.StringIO`` -> ``StringIO``).

Bugs fixed
----------

Expand Down
7 changes: 5 additions & 2 deletions doc/usage/extensions/autodoc.rst
Expand Up @@ -668,12 +668,15 @@ There are also config values that you can set:
following values:

* ``'fully-qualified'`` -- Show the module name and its name of typehints
(default)
* ``'short'`` -- Suppress the leading module names of the typehints
(ex. ``io.StringIO`` -> ``StringIO``)
(ex. ``io.StringIO`` -> ``StringIO``) (default)

.. versionadded:: 4.4

.. versionchanged:: 5.0

The default setting was changed to ``'short'``

.. confval:: autodoc_preserve_defaults

If True, the default argument values of functions will be not evaluated on
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/autodoc/__init__.py
Expand Up @@ -2850,7 +2850,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value('autodoc_typehints_description_target', 'all', True,
ENUM('all', 'documented'))
app.add_config_value('autodoc_type_aliases', {}, True)
app.add_config_value('autodoc_typehints_format', "fully-qualified", 'env',
app.add_config_value('autodoc_typehints_format', "short", 'env',
ENUM("fully-qualified", "short"))
app.add_config_value('autodoc_warningiserror', True, True)
app.add_config_value('autodoc_inherit_docstrings', True, True)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_autodoc_autofunction.py
Expand Up @@ -162,7 +162,7 @@ def test_wrapped_function_contextmanager(app):
actual = do_autodoc(app, 'function', 'target.wrappedfunction.feeling_good')
assert list(actual) == [
'',
'.. py:function:: feeling_good(x: int, y: int) -> typing.Generator',
'.. py:function:: feeling_good(x: int, y: int) -> ~typing.Generator',
' :module: target.wrappedfunction',
'',
" You'll feel better in this context!",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_autodoc_automodule.py
Expand Up @@ -130,4 +130,4 @@ def test_subclass_of_mocked_object(app):

options = {'members': None}
actual = do_autodoc(app, 'module', 'target.need_mocks', options)
assert '.. py:class:: Inherited(*args: typing.Any, **kwargs: typing.Any)' in actual
assert '.. py:class:: Inherited(*args: ~typing.Any, **kwargs: ~typing.Any)' in actual
20 changes: 10 additions & 10 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -612,7 +612,7 @@ def test_autodoc_typehints_signature(app):
' :type: int',
'',
'',
'.. py:class:: Math(s: str, o: typing.Optional[typing.Any] = None)',
'.. py:class:: Math(s: str, o: ~typing.Optional[~typing.Any] = None)',
' :module: target.typehints',
'',
'',
Expand Down Expand Up @@ -677,8 +677,8 @@ def test_autodoc_typehints_signature(app):
' :module: target.typehints',
'',
'',
'.. py:function:: tuple_args(x: typing.Tuple[int, typing.Union[int, str]]) '
'-> typing.Tuple[int, int]',
'.. py:function:: tuple_args(x: ~typing.Tuple[int, ~typing.Union[int, str]]) '
'-> ~typing.Tuple[int, int]',
' :module: target.typehints',
'',
]
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def test_autodoc_type_aliases(app):
' docstring',
'',
'',
'.. py:function:: read(r: _io.BytesIO) -> _io.StringIO',
'.. py:function:: read(r: ~_io.BytesIO) -> ~_io.StringIO',
' :module: target.autodoc_type_aliases',
'',
' docstring',
Expand Down Expand Up @@ -1092,7 +1092,7 @@ def test_autodoc_type_aliases(app):
' docstring',
'',
'',
'.. py:function:: read(r: _io.BytesIO) -> my.module.StringIO',
'.. py:function:: read(r: ~_io.BytesIO) -> my.module.StringIO',
' :module: target.autodoc_type_aliases',
'',
' docstring',
Expand Down Expand Up @@ -1144,8 +1144,8 @@ def test_autodoc_typehints_description_and_type_aliases(app):


@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short(app):
confoverrides={'autodoc_typehints_format': "fully-qualified"})
def test_autodoc_typehints_format_fully_qualified(app):
options = {"members": None,
"undoc-members": None}
actual = do_autodoc(app, 'module', 'target.typehints', options)
Expand All @@ -1159,7 +1159,7 @@ def test_autodoc_typehints_format_short(app):
' :type: int',
'',
'',
'.. py:class:: Math(s: str, o: ~typing.Optional[~typing.Any] = None)',
'.. py:class:: Math(s: str, o: typing.Optional[typing.Any] = None)',
' :module: target.typehints',
'',
'',
Expand Down Expand Up @@ -1224,8 +1224,8 @@ def test_autodoc_typehints_format_short(app):
' :module: target.typehints',
'',
'',
'.. py:function:: tuple_args(x: ~typing.Tuple[int, ~typing.Union[int, str]]) '
'-> ~typing.Tuple[int, int]',
'.. py:function:: tuple_args(x: typing.Tuple[int, typing.Union[int, str]]) '
'-> typing.Tuple[int, int]',
' :module: target.typehints',
'',
]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ext_autodoc_preserve_defaults.py
Expand Up @@ -36,15 +36,15 @@ def test_preserve_defaults(app):
' docstring',
'',
'',
' .. py:method:: Class.meth(name: str = CONSTANT, sentinel: typing.Any = '
'SENTINEL, now: datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
' .. py:method:: Class.meth(name: str = CONSTANT, sentinel: ~typing.Any = '
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
'.. py:function:: foo(name: str = CONSTANT, sentinel: typing.Any = SENTINEL, '
'now: datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
'.. py:function:: foo(name: str = CONSTANT, sentinel: ~typing.Any = SENTINEL, '
'now: ~datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
' :module: target.preserve_defaults',
'',
' docstring',
Expand Down

0 comments on commit 01e6436

Please sign in to comment.