Skip to content

Commit

Permalink
Test autodoc_typehint_undoc
Browse files Browse the repository at this point in the history
Add new tests to exercise the new autodoc_typehint_undoc option. Where
an existing test would have a meaningful behavior change with the new
option set to False, that test is copied, the new option is set to False
in the copy, and the assertions reflect the new expected behavior.

The new test test_autodoc_typehints_description_with_documented_init
illustrates the problem reported in #7329, and the new test
test_autodoc_typehints_description_with_documented_init_no_undoc
illustrates that this issue no longer occurs when the new
autodoc_typehint_undoc option is set to False.
  • Loading branch information
godlygeek committed Feb 8, 2021
1 parent 7468db9 commit b209f4e
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/roots/test-ext-autodoc/target/typehints.py
Expand Up @@ -68,3 +68,13 @@ def missing_attr(c,
):
# type: (...) -> str
return a + (b or "")


class _ClassWithDocumentedInit:
"""Class docstring."""

def __init__(self, x: int) -> None:
"""Init docstring.
:param x: Some integer
"""
122 changes: 122 additions & 0 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -686,6 +686,128 @@ def test_autodoc_typehints_description(app):
in context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description",
'autodoc_typehint_undoc': False})
def test_autodoc_typehints_description_no_undoc(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'
' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) -- arg\n'
'\n'
' Returns:\n'
' another tuple\n'
'\n'
' Return type:\n'
' Tuple[int, int]\n'
in context)


@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'
' * **s** (*str*) -- some string\n'
'\n'
' * **o** (*Any*) --\n'
'\n'
' Return type:\n'
' None\n' == context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description",
'autodoc_typehint_undoc': False})
def test_autodoc_typehints_description_in_init_without_undoc(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'
' **s** (*str*) -- some string\n' == context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description"})
def test_autodoc_typehints_description_with_documented_init(app):
(app.srcdir / 'index.rst').write_text(
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n'
)
app.build()
context = (app.outdir / 'index.txt').read_text()
assert ('class target.typehints._ClassWithDocumentedInit(x)\n'
'\n'
' Class docstring.\n'
'\n'
' Parameters:\n'
' **x** (*int*) --\n'
'\n'
' Return type:\n'
' None\n'
'\n'
' __init__(x)\n'
'\n'
' Init docstring.\n'
'\n'
' Parameters:\n'
' **x** (*int*) -- Some integer\n'
'\n'
' Return type:\n'
' None\n' == context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description",
'autodoc_typehint_undoc': False})
def test_autodoc_typehints_description_with_documented_init_no_undoc(app):
(app.srcdir / 'index.rst').write_text(
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n'
)
app.build()
context = (app.outdir / 'index.txt').read_text()
assert ('class target.typehints._ClassWithDocumentedInit(x)\n'
'\n'
' Class docstring.\n'
'\n'
' __init__(x)\n'
'\n'
' Init docstring.\n'
'\n'
' Parameters:\n'
' **x** (*int*) -- Some integer\n' == context)


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description"})
def test_autodoc_typehints_description_for_invalid_node(app):
Expand Down

0 comments on commit b209f4e

Please sign in to comment.