sphinx.ext.inheritance_diagram -- 継承関係図の追加

Added in version 0.6.

この拡張機能を使うと、継承関係図をドキュメントに挿入できます。図は、 Graphviz拡張 を使ってレンダリングされます。

この拡張機能は次のディレクティブを追加します。

.. inheritance-diagram::

このディレクティブは1つ以上の引数を持ちます。モジュールかクラス名を与えます。現在説明中のモジュールの中であれば(py:module 参照)、クラス名の名前には完全修飾名以外も使えます。

与えられたクラス、もしくは与えられたモジュールに含まれるクラスごとに、ベースクラスが決定され、すべてのクラスから、有向性グラフとして、graphviz拡張を利用して図がレンダリングされます。

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.

ダイアグラムに見出しをつけるには、``caption``オプションを利用できます。

バージョン 1.1 で変更: private-bases オプションを追加しました。以前は常に全てのベースクラスが含まれていました。

バージョン 1.5 で変更: 追加された``caption``オプション

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:

"""
       A
      / \
     B   C
    / \ / \
   E   D   F
"""

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

class E(B):
    pass

class F(C):
    pass

継承図で次のようにモジュールを指定した場合:

.. 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.

サンプル

The following are different inheritance diagrams for the internal InheritanceDiagram class that implements the directive.

With full names:

.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram
digraph inheritance02ef59160f { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "docutils.parsers.rst.Directive" [URL="../../extdev/markupapi.html#docutils.parsers.rst.Directive",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Base class for reStructuredText directives."]; "sphinx.ext.inheritance_diagram.InheritanceDiagram" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Run when the inheritance_diagram directive is first encountered."]; "sphinx.util.docutils.SphinxDirective" -> "sphinx.ext.inheritance_diagram.InheritanceDiagram" [arrowsize=0.5,style="setlinewidth(0.5)"]; "sphinx.util.docutils.SphinxDirective" [URL="../../extdev/utils.html#sphinx.util.docutils.SphinxDirective",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A base class for Sphinx directives."]; "docutils.parsers.rst.Directive" -> "sphinx.util.docutils.SphinxDirective" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

Showing class names only:

.. inheritance-diagram:: sphinx.ext.inheritance_diagram.InheritanceDiagram
   :parts: 1
digraph inheritanceec46a4ed0d { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "Directive" [URL="../../extdev/markupapi.html#docutils.parsers.rst.Directive",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Base class for reStructuredText directives."]; "InheritanceDiagram" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Run when the inheritance_diagram directive is first encountered."]; "SphinxDirective" -> "InheritanceDiagram" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SphinxDirective" [URL="../../extdev/utils.html#sphinx.util.docutils.SphinxDirective",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A base class for Sphinx directives."]; "Directive" -> "SphinxDirective" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

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
digraph inheritance834ccf9cfa { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "ext.inheritance_diagram.InheritanceDiagram" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Run when the inheritance_diagram directive is first encountered."]; "util.docutils.SphinxDirective" -> "ext.inheritance_diagram.InheritanceDiagram" [arrowsize=0.5,style="setlinewidth(0.5)"]; "util.docutils.SphinxDirective" [URL="../../extdev/utils.html#sphinx.util.docutils.SphinxDirective",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A base class for Sphinx directives."]; }
class sphinx.ext.inheritance_diagram.InheritanceDiagram

The internal class that implements the inheritance-diagram directive.

設定

inheritance_graph_attrs

継承関係図を出力する際の、graphvizのgraphの属性の辞書です。

例えば:

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

継承関係図を出力する際の、graphvizのnodeの属性の辞書です。

例えば:

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

継承関係図を出力する際の、graphvizのedgeの属性の辞書です。

inheritance_alias

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).

例えば:

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