| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
:mod:`sphinx.ext.inheritance_diagram` -- Include inheritance diagrams |
| 4 |
===================================================================== |
| 5 |
|
| 6 |
.. module:: sphinx.ext.inheritance_diagram |
| 7 |
:synopsis: Support for displaying inheritance diagrams via graphviz. |
| 8 |
|
| 9 |
.. versionadded:: 0.6 |
| 10 |
|
| 11 |
.. role:: code-py(code) |
| 12 |
:language: Python |
| 13 |
|
| 14 |
This extension allows you to include inheritance diagrams, rendered via the |
| 15 |
:mod:`Graphviz extension <sphinx.ext.graphviz>`. |
| 16 |
|
| 17 |
It adds this directive: |
| 18 |
|
| 19 |
.. rst:directive:: inheritance-diagram |
| 20 |
|
| 21 |
This directive has one or more arguments, each giving a module or class |
| 22 |
name. Class names can be unqualified; in that case they are taken to exist |
| 23 |
in the currently described module (see :rst:dir:`py:module`). |
| 24 |
|
| 25 |
For each given class, and each class in each given module, the base classes |
| 26 |
are determined. Then, from all classes and their base classes, a graph is |
| 27 |
generated which is then rendered via the graphviz extension to a directed |
| 28 |
graph. |
| 29 |
|
| 30 |
This directive supports an option called ``parts`` that, if given, must be an |
| 31 |
integer, advising the directive to keep that many dot-separated parts |
| 32 |
in the displayed names (from right to left). For example, ``parts=1`` will |
| 33 |
only display class names, without the names of the modules that contain |
| 34 |
them. |
| 35 |
|
| 36 |
.. versionchanged:: 2.0 |
| 37 |
The value of for ``parts`` can also be negative, indicating how many |
| 38 |
parts to drop from the left. For example, if all your class names start |
| 39 |
with ``lib.``, you can give ``:parts: -1`` to remove that prefix from the |
| 40 |
displayed node names. |
| 41 |
|
| 42 |
The directive also supports a ``private-bases`` flag option; if given, |
| 43 |
private base classes (those whose name starts with ``_``) will be included. |
| 44 |
|
| 45 |
You can use ``caption`` option to give a caption to the diagram. |
| 46 |
|
| 47 |
.. versionchanged:: 1.1 |
| 48 |
Added ``private-bases`` option; previously, all bases were always |
| 49 |
included. |
| 50 |
|
| 51 |
.. versionchanged:: 1.5 |
| 52 |
Added ``caption`` option |
| 53 |
|
| 54 |
It also supports a ``top-classes`` option which requires one or more class |
| 55 |
names separated by comma. If specified inheritance traversal will stop at the |
| 56 |
specified class names. Given the following Python module:: |
| 57 |
|
| 58 |
""" |
| 59 |
A |
| 60 |
/ \ |
| 61 |
B C |
| 62 |
/ \ / \ |
| 63 |
E D F |
| 64 |
""" |
| 65 |
|
| 66 |
class A: |
| 67 |
pass |
| 68 |
|
| 69 |
class B(A): |
| 70 |
pass |
| 71 |
|
| 72 |
class C(A): |
| 73 |
pass |
| 74 |
|
| 75 |
class D(B, C): |
| 76 |
pass |
| 77 |
|
| 78 |
class E(B): |
| 79 |
pass |
| 80 |
|
| 81 |
class F(C): |
| 82 |
pass |
| 83 |
|
| 84 |
If you have specified a module in the inheritance diagram like this:: |
| 85 |
|
| 86 |
.. inheritance-diagram:: dummy.test |
| 87 |
:top-classes: dummy.test.B, dummy.test.C |
| 88 |
|
| 89 |
any base classes which are ancestors to ``top-classes`` and are also defined |
| 90 |
in the same module will be rendered as stand alone nodes. In this example |
| 91 |
class A will be rendered as stand alone node in the graph. This is a known |
| 92 |
issue due to how this extension works internally. |
| 93 |
|
| 94 |
If you don't want class A (or any other ancestors) to be visible then specify |
| 95 |
only the classes you would like to generate the diagram for like this:: |
| 96 |
|
| 97 |
.. inheritance-diagram:: dummy.test.D dummy.test.E dummy.test.F |
| 98 |
:top-classes: dummy.test.B, dummy.test.C |
| 99 |
|
| 100 |
.. versionchanged:: 1.7 |
| 101 |
Added ``top-classes`` option to limit the scope of inheritance graphs. |
| 102 |
|
| 103 |
.. rst:directive:option:: include-subclasses |
| 104 |
:type: no value |
| 105 |
|
| 106 |
.. versionadded:: 8.2 |
| 107 |
|
| 108 |
If given, any subclass of the classes will be added to the diagram too. |
| 109 |
|
| 110 |
Given the Python module from above, you can specify |
| 111 |
your inheritance diagram like this: |
| 112 |
|
| 113 |
.. code-block:: rst |
| 114 |
|
| 115 |
.. inheritance-diagram:: dummy.test.A |
| 116 |
:include-subclasses: |
| 117 |
|
| 118 |
This will include the classes A, B, C, D, E and F in the inheritance diagram |
| 119 |
but no other classes in the module ``dummy.test``. |
| 120 |
|
| 121 |
|
| 122 |
Examples |
| 123 |
-------- |
| 124 |
|
| 125 |
The following are different inheritance diagrams for the internal |
| 126 |
``InheritanceDiagram`` class that implements the directive. |
| 127 |
|
| 128 |
With full names:: |
| 129 |
|
| 130 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 131 |
|
| 132 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 133 |
|
| 134 |
|
| 135 |
Showing class names only:: |
| 136 |
|
| 137 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 138 |
:parts: 1 |
| 139 |
|
| 140 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 141 |
:parts: 1 |
| 142 |
|
| 143 |
Stopping the diagram at :class:`sphinx.util.docutils.SphinxDirective` (the |
| 144 |
highest superclass still part of Sphinx), and dropping the common left-most |
| 145 |
part (``sphinx``) from all names:: |
| 146 |
|
| 147 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 148 |
:top-classes: sphinx.util.docutils.SphinxDirective |
| 149 |
:parts: -1 |
| 150 |
|
| 151 |
.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 152 |
:top-classes: sphinx.util.docutils.SphinxDirective |
| 153 |
:parts: -1 |
| 154 |
|
| 155 |
.. py:class:: sphinx.ext.inheritance_diagram.InheritanceDiagram |
| 156 |
:no-contents-entry: |
| 157 |
:no-index-entry: |
| 158 |
|
| 159 |
The internal class that implements the ``inheritance-diagram`` directive. |
| 160 |
|
| 161 |
|
| 162 |
Configuration |
| 163 |
------------- |
| 164 |
|
| 165 |
.. confval:: inheritance_graph_attrs |
| 166 |
:type: :code-py:`dict[str, str | int | float | bool]` |
| 167 |
:default: :code-py:`{}` |
| 168 |
|
| 169 |
A dictionary of graphviz graph attributes for inheritance diagrams. |
| 170 |
|
| 171 |
For example:: |
| 172 |
|
| 173 |
inheritance_graph_attrs = dict(rankdir="LR", size='"6.0, 8.0"', |
| 174 |
fontsize=14, ratio='compress') |
| 175 |
|
| 176 |
.. confval:: inheritance_node_attrs |
| 177 |
:type: :code-py:`dict[str, str | int | float | bool]` |
| 178 |
:default: :code-py:`{}` |
| 179 |
|
| 180 |
A dictionary of graphviz node attributes for inheritance diagrams. |
| 181 |
|
| 182 |
For example:: |
| 183 |
|
| 184 |
inheritance_node_attrs = dict(shape='ellipse', fontsize=14, height=0.75, |
| 185 |
color='dodgerblue1', style='filled') |
| 186 |
|
| 187 |
.. confval:: inheritance_edge_attrs |
| 188 |
:type: :code-py:`dict[str, str | int | float | bool]` |
| 189 |
:default: :code-py:`{}` |
| 190 |
|
| 191 |
A dictionary of graphviz edge attributes for inheritance diagrams. |
| 192 |
|
| 193 |
.. confval:: inheritance_alias |
| 194 |
:type: :code-py:`dict[str, str]` |
| 195 |
:default: :code-py:`{}` |
| 196 |
|
| 197 |
Allows mapping the full qualified name of the class to custom values |
| 198 |
(useful when exposing the underlying path of a class is not desirable, |
| 199 |
e.g. it's a private class and should not be instantiated by the user). |
| 200 |
|
| 201 |
For example:: |
| 202 |
|
| 203 |
inheritance_alias = {'_pytest.Magic': 'pytest.Magic'} |