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 2, 2022
1 parent 6f707a0 commit 616f112
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -30,6 +30,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 @@ -2871,7 +2871,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
12 changes: 6 additions & 6 deletions tests/test_ext_autodoc.py
Expand Up @@ -1009,7 +1009,7 @@ def test_autodoc_inner_class(app):
'',
'.. py:class:: InnerChild()',
' :module: target', '',
' Bases: :py:class:`target.Outer.Inner`',
' Bases: :py:class:`~target.Outer.Inner`',
'',
' InnerChild docstring',
'',
Expand Down Expand Up @@ -1705,7 +1705,7 @@ def test_autodoc_typed_instance_variables(app):
'.. py:attribute:: Alias',
' :module: target.typed_vars',
'',
' alias of :py:class:`target.typed_vars.Derived`',
' alias of :py:class:`~target.typed_vars.Derived`',
'',
'.. py:class:: Class()',
' :module: target.typed_vars',
Expand Down Expand Up @@ -1910,7 +1910,7 @@ def test_autodoc_GenericAlias(app):
' A list of Class',
'',
' alias of :py:class:`~typing.List`\\ '
'[:py:class:`target.genericalias.Class`]',
'[:py:class:`~target.genericalias.Class`]',
'',
'',
'.. py:data:: T',
Expand Down Expand Up @@ -1950,7 +1950,7 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :py:class:`datetime.date`',
' alias of :py:class:`~datetime.date`',
'',
'',
'.. py:data:: T1',
Expand Down Expand Up @@ -1990,7 +1990,7 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :py:class:`datetime.date`',
' alias of :py:class:`~datetime.date`',
'',
'',
'.. py:data:: T7',
Expand Down Expand Up @@ -2322,7 +2322,7 @@ def test_autodoc(app, status, warning):
my_name
alias of bug2437.autodoc_dummy_foo.Foo"""
alias of Foo"""
assert warning.getvalue() == ''


Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_autodoc_autoattribute.py
Expand Up @@ -183,7 +183,7 @@ def test_autoattribute_NewType(app):
'',
' T6',
'',
' alias of :py:class:`datetime.date`',
' alias of :py:class:`~datetime.date`',
'',
]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_ext_autodoc_autoclass.py
Expand Up @@ -284,7 +284,7 @@ def test_show_inheritance_for_decendants_of_generic_type(app):
'.. py:class:: Corge(iterable=(), /)',
' :module: target.classes',
'',
' Bases: :py:class:`target.classes.Quux`',
' Bases: :py:class:`~target.classes.Quux`',
'',
]

Expand Down Expand Up @@ -391,7 +391,7 @@ def autodoc_process_docstring(*args):
'.. py:attribute:: Alias',
' :module: target.classes',
'',
' alias of :py:class:`target.classes.Foo`',
' alias of :py:class:`~target.classes.Foo`',
]


Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_autodoc_autodata.py
Expand Up @@ -111,7 +111,7 @@ def test_autodata_NewType(app):
'',
' T6',
'',
' alias of :py:class:`datetime.date`',
' alias of :py:class:`~datetime.date`',
'',
]

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
38 changes: 19 additions & 19 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,29 +1224,29 @@ 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',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_class_alias(app):
confoverrides={'autodoc_typehints_format': "fully-qualified"})
def test_autodoc_typehints_format_fully_qualified_for_class_alias(app):
actual = do_autodoc(app, 'class', 'target.classes.Alias')
assert list(actual) == [
'',
'.. py:attribute:: Alias',
' :module: target.classes',
'',
' alias of :py:class:`~target.classes.Foo`',
' alias of :py:class:`target.classes.Foo`',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_generic_alias(app):
confoverrides={'autodoc_typehints_format': "fully-qualified"})
def test_autodoc_typehints_format_fully_qualified_for_generic_alias(app):
actual = do_autodoc(app, 'data', 'target.genericalias.L')
if sys.version_info < (3, 7):
assert list(actual) == [
Expand All @@ -1266,14 +1266,14 @@ def test_autodoc_typehints_format_short_for_generic_alias(app):
'',
' A list of Class',
'',
' alias of :py:class:`~typing.List`\\ [:py:class:`~target.genericalias.Class`]',
' alias of :py:class:`~typing.List`\\ [:py:class:`target.genericalias.Class`]',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_newtype_alias(app):
confoverrides={'autodoc_typehints_format': "fully-qualified"})
def test_autodoc_typehints_format_fully_qualified_for_newtype_alias(app):
actual = do_autodoc(app, 'data', 'target.typevar.T6')
assert list(actual) == [
'',
Expand All @@ -1282,7 +1282,7 @@ def test_autodoc_typehints_format_short_for_newtype_alias(app):
'',
' T6',
'',
' alias of :py:class:`~datetime.date`',
' alias of :py:class:`datetime.date`',
'',
]

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 616f112

Please sign in to comment.