Skip to content

Commit 7431750

Browse files
committed
doc: have tighter control on what autodoc shows
New versions of sphinx starting showing `__init__` parameters even when we don't want them to show because they are private (have `_ispytest` argument). The only working solution I found was to switch to `autodoc_typehints_description_target = "documented"` and explicitly document parameters for which we want to show the types. It's a little tedious and repetitive in some simple cases, but overall it results in nicer API docs.
1 parent cb7f5ed commit 7431750

File tree

16 files changed

+335
-168
lines changed

16 files changed

+335
-168
lines changed

doc/en/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
autodoc_member_order = "bysource"
4040
autodoc_typehints = "description"
41+
autodoc_typehints_description_target = "documented"
4142
todo_include_todos = 1
4243

4344
latex_engine = "lualatex"

doc/en/reference/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pytest.deprecated_call
102102

103103
**Tutorial**: :ref:`ensuring_function_triggers`
104104

105-
.. autofunction:: pytest.deprecated_call()
105+
.. autofunction:: pytest.deprecated_call([match])
106106
:with:
107107

108108
pytest.register_assert_rewrite

src/_pytest/assertion/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def register_assert_rewrite(*names: str) -> None:
5353
actually imported, usually in your __init__.py if you are a plugin
5454
using a package.
5555
56-
:raises TypeError: If the given module names are not strings.
56+
:param names: The module names to register.
5757
"""
5858
for name in names:
5959
if not isinstance(name, str):

src/_pytest/config/argparsing.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ def getgroup(
6666
) -> "OptionGroup":
6767
"""Get (or create) a named option Group.
6868
69-
:name: Name of the option group.
70-
:description: Long description for --help output.
71-
:after: Name of another group, used for ordering --help output.
69+
:param name: Name of the option group.
70+
:param description: Long description for --help output.
71+
:param after: Name of another group, used for ordering --help output.
72+
:returns: The option group.
7273
7374
The returned group object has an ``addoption`` method with the same
7475
signature as :func:`parser.addoption <pytest.Parser.addoption>` but
7576
will be shown in the respective group in the output of
76-
``pytest. --help``.
77+
``pytest --help``.
7778
"""
7879
for group in self._groups:
7980
if group.name == name:
@@ -89,10 +90,11 @@ def getgroup(
8990
def addoption(self, *opts: str, **attrs: Any) -> None:
9091
"""Register a command line option.
9192
92-
:opts: Option names, can be short or long options.
93-
:attrs: Same attributes which the ``add_argument()`` function of the
94-
`argparse library <https://docs.python.org/library/argparse.html>`_
95-
accepts.
93+
:param opts:
94+
Option names, can be short or long options.
95+
:param attrs:
96+
Same attributes as the argparse library's :py:func:`add_argument()
97+
<argparse.ArgumentParser.add_argument>` function accepts.
9698
9799
After command line parsing, options are available on the pytest config
98100
object via ``config.option.NAME`` where ``NAME`` is usually set
@@ -148,16 +150,24 @@ def parse_known_args(
148150
args: Sequence[Union[str, "os.PathLike[str]"]],
149151
namespace: Optional[argparse.Namespace] = None,
150152
) -> argparse.Namespace:
151-
"""Parse and return a namespace object with known arguments at this point."""
153+
"""Parse the known arguments at this point.
154+
155+
:returns: An argparse namespace object.
156+
"""
152157
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
153158

154159
def parse_known_and_unknown_args(
155160
self,
156161
args: Sequence[Union[str, "os.PathLike[str]"]],
157162
namespace: Optional[argparse.Namespace] = None,
158163
) -> Tuple[argparse.Namespace, List[str]]:
159-
"""Parse and return a namespace object with known arguments, and
160-
the remaining arguments unknown at this point."""
164+
"""Parse the known arguments at this point, and also return the
165+
remaining unknown arguments.
166+
167+
:returns:
168+
A tuple containing an argparse namespace object for the known
169+
arguments, and a list of the unknown arguments.
170+
"""
161171
optparser = self._getparser()
162172
strargs = [os.fspath(x) for x in args]
163173
return optparser.parse_known_args(strargs, namespace=namespace)
@@ -173,9 +183,9 @@ def addini(
173183
) -> None:
174184
"""Register an ini-file option.
175185
176-
:name:
186+
:param name:
177187
Name of the ini-variable.
178-
:type:
188+
:param type:
179189
Type of the variable. Can be:
180190
181191
* ``string``: a string
@@ -189,7 +199,7 @@ def addini(
189199
The ``paths`` variable type.
190200
191201
Defaults to ``string`` if ``None`` or not passed.
192-
:default:
202+
:param default:
193203
Default value if no ini-file option exists but is queried.
194204
195205
The value of ini-variables can be retrieved via a call to
@@ -354,24 +364,30 @@ def __init__(
354364
self.options: List[Argument] = []
355365
self.parser = parser
356366

357-
def addoption(self, *optnames: str, **attrs: Any) -> None:
367+
def addoption(self, *opts: str, **attrs: Any) -> None:
358368
"""Add an option to this group.
359369
360370
If a shortened version of a long option is specified, it will
361371
be suppressed in the help. ``addoption('--twowords', '--two-words')``
362372
results in help showing ``--two-words`` only, but ``--twowords`` gets
363373
accepted **and** the automatic destination is in ``args.twowords``.
374+
375+
:param opts:
376+
Option names, can be short or long options.
377+
:param attrs:
378+
Same attributes as the argparse library's :py:func:`add_argument()
379+
<argparse.ArgumentParser.add_argument>` function accepts.
364380
"""
365-
conflict = set(optnames).intersection(
381+
conflict = set(opts).intersection(
366382
name for opt in self.options for name in opt.names()
367383
)
368384
if conflict:
369385
raise ValueError("option names %s already added" % conflict)
370-
option = Argument(*optnames, **attrs)
386+
option = Argument(*opts, **attrs)
371387
self._addoption_instance(option, shortupper=False)
372388

373-
def _addoption(self, *optnames: str, **attrs: Any) -> None:
374-
option = Argument(*optnames, **attrs)
389+
def _addoption(self, *opts: str, **attrs: Any) -> None:
390+
option = Argument(*opts, **attrs)
375391
self._addoption_instance(option, shortupper=True)
376392

377393
def _addoption_instance(self, option: "Argument", shortupper: bool = False) -> None:

src/_pytest/fixtures.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ def session(self) -> "Session":
513513
return self._pyfuncitem.session # type: ignore[no-any-return]
514514

515515
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
516-
"""Add finalizer/teardown function to be called after the last test
517-
within the requesting test context finished execution."""
516+
"""Add finalizer/teardown function to be called without arguments after
517+
the last test within the requesting test context finished execution."""
518518
# XXX usually this method is shadowed by fixturedef specific ones.
519519
self._addfinalizer(finalizer, scope=self.scope)
520520

@@ -529,13 +529,16 @@ def applymarker(self, marker: Union[str, MarkDecorator]) -> None:
529529
on all function invocations.
530530
531531
:param marker:
532-
A :class:`pytest.MarkDecorator` object created by a call
533-
to ``pytest.mark.NAME(...)``.
532+
An object created by a call to ``pytest.mark.NAME(...)``.
534533
"""
535534
self.node.add_marker(marker)
536535

537536
def raiseerror(self, msg: Optional[str]) -> NoReturn:
538-
"""Raise a FixtureLookupError with the given message."""
537+
"""Raise a FixtureLookupError exception.
538+
539+
:param msg:
540+
An optional custom error message.
541+
"""
539542
raise self._fixturemanager.FixtureLookupError(None, self, msg)
540543

541544
def _fillfixtures(self) -> None:
@@ -557,6 +560,8 @@ def getfixturevalue(self, argname: str) -> Any:
557560
phase, but during the test teardown phase a fixture's value may not
558561
be available.
559562
563+
:param argname:
564+
The fixture name.
560565
:raises pytest.FixtureLookupError:
561566
If the given fixture could not be found.
562567
"""
@@ -768,8 +773,8 @@ def __repr__(self) -> str:
768773
return f"<SubRequest {self.fixturename!r} for {self._pyfuncitem!r}>"
769774

770775
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
771-
"""Add finalizer/teardown function to be called after the last test
772-
within the requesting test context finished execution."""
776+
"""Add finalizer/teardown function to be called without arguments after
777+
the last test within the requesting test context finished execution."""
773778
self._fixturedef.addfinalizer(finalizer)
774779

775780
def _schedule_finalizers(

0 commit comments

Comments
 (0)