sphinx.ext.inheritance_diagram – 包含继承图

0.6 新版功能.

此扩展允许您包含继承图,通过 Graphviz extension 呈现。


.. inheritance-diagram::

该指令有一个或多个参数,每个参数都给出一个模块或类名。班级名称可以是不合格的;在这种情况下,它们被存在于当前描述的模块中(参见 py:module )。

For each given class, and each class in each given module, the base classes are determined. Then, from all classes and their base classes, a graph is generated which is then rendered via the graphviz extension to a directed graph.

This directive supports an option called parts that, if given, must be an integer, advising the directive to keep that many dot-separated parts in the displayed names (from right to left). For example, parts=1 will only display class names, without the names of the modules that contain them.

在 2.0 版更改: The value of for parts can also be negative, indicating how many parts to drop from the left. For example, if all your class names start with lib., you can give :parts: -1 to remove that prefix from the displayed node names.

The directive also supports a private-bases flag option; if given, private base classes (those whose name starts with _) will be included.

You can use caption option to give a caption to the diagram.

在 1.1 版更改: Added private-bases option; previously, all bases were always included.

在 1.5 版更改: Added caption option

It also supports a top-classes option which requires one or more class names separated by comma. If specified inheritance traversal will stop at the specified class names. Given the following Python module:

      / \
     B   C
    / \ / \
   E   D   F

class A:

class B(A):

class C(A):

class D(B, C):

class E(B):

class F(C):

If you have specified a module in the inheritance diagram like this:

.. inheritance-diagram:: dummy.test
   :top-classes: dummy.test.B, dummy.test.C

any base classes which are ancestors to top-classes and are also defined in the same module will be rendered as stand alone nodes. In this example class A will be rendered as stand alone node in the graph. This is a known issue due to how this extension works internally.

If you don’t want class A (or any other ancestors) to be visible then specify only the classes you would like to generate the diagram for like this:

.. inheritance-diagram:: dummy.test.D dummy.test.E dummy.test.F
   :top-classes: dummy.test.B, dummy.test.C

在 1.7 版更改: Added top-classes option to limit the scope of inheritance graphs.


以下是实现该指令的内部 InheritanceDiagram 类的不同继承图。


.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram
Inheritance diagram of sphinx.ext.inheritance_diagram.InheritanceDiagram


.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram
   :parts: 1
Inheritance diagram of sphinx.ext.inheritance_diagram.InheritanceDiagram

Stopping the diagram at sphinx.util.docutils.SphinxDirective (the highest superclass still part of Sphinx), and dropping the common left-most part (sphinx) from all names:

.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram
   :top-classes: sphinx.util.docutils.SphinxDirective
   :parts: -1



A dictionary of graphviz graph attributes for inheritance diagrams.

For example:

inheritance_graph_attrs = dict(rankdir="LR", size='"6.0, 8.0"',
                               fontsize=14, ratio='compress')

A dictionary of graphviz node attributes for inheritance diagrams.

For example:

inheritance_node_attrs = dict(shape='ellipse', fontsize=14, height=0.75,
                              color='dodgerblue1', style='filled')

A dictionary of graphviz edge attributes for inheritance diagrams.


Allows mapping the full qualified name of the class to custom values (useful when exposing the underlying path of a class is not desirable, e.g. it’s a private class and should not be instantiated by the user).

For example:

inheritance_alias = {'_pytest.Magic': 'pytest.Magic'}