sphinx.ext.inheritance_diagram – Inclure les diagrammes d’héritage

Ajouté dans la version 0.6.

Cette extension vous permet d’inclure des diagrammes d’héritage, rendus via l'extension Graphviz.

Il ajoute cette directive :

.. inheritance-diagram::

Cette directive a un ou plusieurs arguments, chacun donnant un nom de module ou de classe. Les noms de classes peuvent être non qualifiés ; dans ce cas, ils sont considérés comme existant dans le module actuellement décrit (voir py:module).

Pour chaque classe donnée, et chaque classe dans chaque module donné, les classes de base sont déterminées. Ensuite, à partir de toutes les classes et de leurs classes de base, un graphique est généré qui est ensuite rendu par l’extension graphviz en un graphique vectoriel.

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.

Modifié dans la version 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.

Vous pouvez utiliser l’option caption pour donner une légende au diagramme.

Modifié dans la version 1.1: Ajout de l’option private-bases ; auparavant, toutes les bases étaient toujours incluses.

Modifié dans la version 1.5: Ajouté l’option 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

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

Modifié dans la version 1.7: Added top-classes option to limit the scope of inheritance graphs.

:include-subclasses: (no value)

Ajouté dans la version 8.2.

If given, any subclass of the classes will be added to the diagram too.

Given the Python module from above, you can specify your inheritance diagram like this:

.. inheritance-diagram:: dummy.test.A
   :include-subclasses:

This will include the classes A, B, C, D, E and F in the inheritance diagram but no other classes in the module dummy.test.

Exemples

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
Inheritance diagram of sphinx.ext.inheritance_diagram.InheritanceDiagram

Showing class names only:

.. 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
Inheritance diagram of sphinx.ext.inheritance_diagram.InheritanceDiagram
class sphinx.ext.inheritance_diagram.InheritanceDiagram

The internal class that implements the inheritance-diagram directive.

Configuration

inheritance_graph_attrs
Type:
dict[str, str | int | float | bool]
Défaut:
{}

A dictionary of graphviz graph attributes for inheritance diagrams.

Pas exemple:

inheritance_graph_attrs = dict(rankdir="LR", size='"6.0, 8.0"',
                               fontsize=14, ratio='compress')
inheritance_node_attrs
Type:
dict[str, str | int | float | bool]
Défaut:
{}

A dictionary of graphviz node attributes for inheritance diagrams.

Pas exemple:

inheritance_node_attrs = dict(shape='ellipse', fontsize=14, height=0.75,
                              color='dodgerblue1', style='filled')
inheritance_edge_attrs
Type:
dict[str, str | int | float | bool]
Défaut:
{}

A dictionary of graphviz edge attributes for inheritance diagrams.

inheritance_alias
Type:
dict[str, str]
Défaut:
{}

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

Pas exemple:

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