diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index d76ed25e3f4..83fc6bb2f70 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -661,26 +661,65 @@ def test_autodoc_typehints_none_for_overload(app): @pytest.mark.sphinx('text', testroot='ext-autodoc', confoverrides={'autodoc_typehints': "description"}) def test_autodoc_typehints_description(app): + # No :type: or :rtype: will be injected for `incr`, which does not have + # a description for its parameters or its return. `tuple_args` does + # describe them, so :type: and :rtype: will be added. + (app.srcdir / 'index.rst').write_text( + '.. autofunction:: target.typehints.incr\n' + '\n' + '.. autofunction:: target.typehints.tuple_args\n' + '\n' + ' :param x: arg\n' + ' :return: another tuple\n' + ) app.build() context = (app.outdir / 'index.txt').read_text() assert ('target.typehints.incr(a, b=1)\n' + '\n' + 'target.typehints.tuple_args(x)\n' '\n' ' Parameters:\n' - ' * **a** (*int*) --\n' + ' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) -- arg\n' '\n' - ' * **b** (*int*) --\n' + ' Returns:\n' + ' another tuple\n' '\n' ' Return type:\n' - ' int\n' + ' Tuple[int, int]\n' in context) - assert ('target.typehints.tuple_args(x)\n' + + +@pytest.mark.sphinx('text', testroot='ext-autodoc', + confoverrides={'autodoc_typehints': "description"}) +def test_autodoc_typehints_description_in_init(app): + (app.srcdir / 'index.rst').write_text( + '.. autoclass:: target.typehints.Math\n' + '\n' + ' :param s: some string\n' + ) + app.build() + context = (app.outdir / 'index.txt').read_text() + assert ('class target.typehints.Math(s, o=None)\n' '\n' ' Parameters:\n' - ' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) --\n' + ' **s** (*str*) -- some string\n' == context) + + +@pytest.mark.sphinx('text', testroot='ext-autodoc', + confoverrides={'autodoc_typehints': "description", + 'autoclass_content': "both"}) +def test_autodoc_typehints_description_with_autoclass_content_both(app): + (app.srcdir / 'index.rst').write_text( + '.. autoclass:: target.typehints.Math\n' + '\n' + ' :param s: some string\n' + ) + app.build() + context = (app.outdir / 'index.txt').read_text() + assert ('class target.typehints.Math(s, o=None)\n' '\n' - ' Return type:\n' - ' Tuple[int, int]\n' - in context) + ' Parameters:\n' + ' **s** (*str*) -- some string\n' == context) @pytest.mark.sphinx('text', testroot='ext-autodoc', @@ -784,7 +823,13 @@ def test_autodoc_type_aliases(app): confoverrides={'autodoc_typehints': "description", 'autodoc_type_aliases': {'myint': 'myint'}}) def test_autodoc_typehints_description_and_type_aliases(app): - (app.srcdir / 'annotations.rst').write_text('.. autofunction:: target.annotations.sum') + (app.srcdir / 'annotations.rst').write_text( + '.. autofunction:: target.annotations.sum\n' + '\n' + ' :param x: summand 1\n' + ' :param y: summand 2\n' + ' :return: the sum\n' + ) app.build() context = (app.outdir / 'annotations.txt').read_text() assert ('target.annotations.sum(x, y)\n' @@ -792,9 +837,12 @@ def test_autodoc_typehints_description_and_type_aliases(app): ' docstring\n' '\n' ' Parameters:\n' - ' * **x** (*myint*) --\n' + ' * **x** (*myint*) -- summand 1\n' + '\n' + ' * **y** (*myint*) -- summand 2\n' '\n' - ' * **y** (*myint*) --\n' + ' Returns:\n' + ' the sum\n' '\n' ' Return type:\n' ' myint\n' == context)