Skip to content

Commit

Permalink
Test autodoc_typehints="description" changes
Browse files Browse the repository at this point in the history
A few tests need to change to account for the behavior change of not
including a :type: field for a parameter unless the user supplies
a :param: for it (and likewise for :rtype: and :return:).

New tests are introduced to cover the new behavior: the type hints from
the __init__ method are used even when autoclass_content="class", and
they are not duplicated when autoclass_content="both" (so #7329 is still
fixed, in a new way).
  • Loading branch information
godlygeek committed Dec 15, 2020
1 parent 2f15783 commit 67737ba
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -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',
Expand Down Expand Up @@ -784,17 +823,26 @@ 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'
'\n'
' 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)
Expand Down

0 comments on commit 67737ba

Please sign in to comment.