Skip to content

Commit

Permalink
Fix broken inheritance-diagram links due to the smart resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ayshih committed Aug 10, 2023
1 parent 62ba0ae commit 5d6ab81
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion sphinx_automodapi/automodsumm.py
Expand Up @@ -90,7 +90,7 @@ class members that are inherited from a base class. This value can be

from sphinx.util import logging
from sphinx.ext.autosummary import Autosummary
from sphinx.ext.inheritance_diagram import InheritanceDiagram
from sphinx.ext.inheritance_diagram import InheritanceDiagram, InheritanceGraph, try_import
from docutils.parsers.rst.directives import flag

from .utils import find_mod_objs, cleanup_whitespace
Expand Down Expand Up @@ -242,6 +242,33 @@ def run(self):
self.arguments = oldargs


# sphinx.ext.inheritance_diagram generates a list of class full names and
# generates a mapping from class full names to documentation URLs. However, the
# smart resolver in sphinx-automodapi causes the generated mapping to be instead
# from class documented name to documentation URLs. The class documented name
# can be different from the class full name if the class is not documented where
# it is defined, but rather at some other location where it is imported. In
# such situations, the code will fail to find the URL that for the class.

# The following code monkey-patches the method that receives the mapping and
# converts the keys from class documented names to class full names.

old_generate_dot = InheritanceGraph.generate_dot


def patched_generate_dot(self, name, urls={}, env=None,
graph_attrs={}, node_attrs={}, edge_attrs={}):
# Make a new mapping dictionary that uses class full names by importing each
# class documented name
fullname_urls = {self.class_name(try_import(name), 0, None): url
for name, url in urls.items() if try_import(name) is not None}
return old_generate_dot(self, name, urls=fullname_urls, env=env,
graph_attrs=graph_attrs, node_attrs=node_attrs, edge_attrs=edge_attrs)


InheritanceGraph.generate_dot = patched_generate_dot


# <---------------------automodsumm generation stuff-------------------------->
def process_automodsumm_generation(app):
env = app.builder.env
Expand Down

0 comments on commit 5d6ab81

Please sign in to comment.