1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#!/usr/bin/env python3
#
# generic_bases.py
r"""
Modifies :class:`sphinx.ext.autodoc.ClassDocumenter`\'s ``:show-inheritence:`` option
to show generic base classes.
This requires a relatively new version of the :mod:`typing` module that implements ``__orig_bases__``.
.. versionadded:: 1.5.0
.. extensions:: sphinx_toolbox.more_autodoc.generic_bases
Configuration
----------------
.. confval:: generic_bases_fully_qualified
:type: :class:`bool`
:required: False
:default: :py:obj:`False`
Determines whether the fully qualified name should be shown for bases.
If :py:obj:`False` (the default):
.. class:: Foo
:noindex:
**Bases**: :class:`~typing.List`\[:class:`str`\]
If :py:obj:`True`:
.. class:: Foo
:noindex:
**Bases**: :class:`typing.List`\[:class:`str`\]
Corresponds to the ``fully_qualified`` argument to
:func:`sphinx_toolbox.more_autodoc.typehints.format_annotation`.
.. versionadded:: 2.13.0
Example
--------
.. autoclass:: sphinx_toolbox.more_autodoc.generic_bases.Example
API Reference
-----------------
""" # noqa: D400
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
import sys
from typing import List, Tuple
# 3rd party
from sphinx.application import Sphinx
from sphinx.ext.autodoc import Documenter
from sphinx.locale import _
from typing_extensions import final
# this package
from sphinx_toolbox.more_autodoc.typehints import format_annotation
from sphinx_toolbox.more_autosummary import PatchedAutoSummClassDocumenter
from sphinx_toolbox.utils import SphinxExtMetadata, allow_subclass_add, metadata_add_version
if sys.version_info >= (3, 8): # pragma: no cover (<py38)
# stdlib
from typing import get_origin
else: # pragma: no cover (py38+)
# 3rd party
from typing_inspect import get_origin
__all__ = ("GenericBasesClassDocumenter", "setup")
class GenericBasesClassDocumenter(PatchedAutoSummClassDocumenter):
"""
Class documenter that adds inheritance info, with support for generics.
"""
def add_directive_header(self, sig: str) -> None:
"""
Add the directive header.
:param sig:
"""
sourcename = self.get_sourcename()
if self.doc_as_attr:
self.directivetype = "attribute"
Documenter.add_directive_header(self, sig)
if self.analyzer and '.'.join(self.objpath) in self.analyzer.finals:
self.add_line(" :final:", sourcename)
# add inheritance info, if wanted
if not self.doc_as_attr and self.options.show_inheritance:
_add_generic_bases(self)
def _add_generic_bases(documenter: Documenter) -> None:
"""
Add the generic bases to the output of the given Documenter.
.. versionadded:: 2.13.0 (undocumented)
:param documenter:
"""
sourcename = documenter.get_sourcename()
# add inheritance info, if wanted
fully_qualified = getattr(documenter.env.config, "generic_bases_fully_qualified", False)
documenter.add_line('', sourcename)
bases = [] # pylint: disable=W8301
if (
hasattr(documenter.object, "__orig_bases__") and len(documenter.object.__orig_bases__)
and get_origin(documenter.object.__orig_bases__[0]) is documenter.object.__bases__[0]
):
# Last condition guards against classes that don't directly subclass a Generic.
bases = [format_annotation(b, fully_qualified) for b in documenter.object.__orig_bases__]
elif hasattr(documenter.object, "__bases__") and len(documenter.object.__bases__):
bases = [format_annotation(b, fully_qualified) for b in documenter.object.__bases__]
if bases:
bases_string = ", ".join(bases).replace("typing_extensions.", "typing.")
documenter.add_line(" " + _("Bases: %s") % bases_string, sourcename)
@metadata_add_version
def setup(app: Sphinx) -> SphinxExtMetadata:
"""
Setup :mod:`sphinx_toolbox.more_autodoc.generic_bases`.
.. versionadded:: 1.5.0
:param app: The Sphinx application.
"""
allow_subclass_add(app, GenericBasesClassDocumenter)
app.add_config_value(
"generic_bases_fully_qualified",
default=False,
rebuild="env",
types=[bool],
)
return {"parallel_read_safe": True}
class Example(List[Tuple[str, float, List[str]]]):
"""
An example of :mod:`sphinx_toolbox.more_autodoc.generic_bases`.
"""
def __init__(self, iterable=()): # pragma: no cover # noqa: MAN001
pass
class Example2(Example):
"""
An example of :mod:`sphinx_toolbox.more_autodoc.generic_bases`.
This one does not directly subclass a Generic.
"""
@final
class FinalExample(List[Tuple[str, float, List[str]]]):
"""
An example of :mod:`sphinx_toolbox.more_autodoc.generic_bases` decorated with ``@final``.
"""
def __init__(self, iterable=()): # pragma: no cover # noqa: MAN001
pass
|