|
| 1 | +# stdlib |
| 2 | +import re |
| 3 | +from typing import Any, Dict, List, Tuple |
| 4 | + |
| 5 | +# 3rd party |
| 6 | +import sphinx |
| 7 | +from sphinx.application import Sphinx |
| 8 | +from sphinx.ext.autosummary import Autosummary |
| 9 | + |
| 10 | + |
| 11 | +class PatchedAutosummary(Autosummary): |
| 12 | + """ |
| 13 | + Pretty table containing short signatures and summaries of functions etc. |
| 14 | +
|
| 15 | + Patched version of :class:`sphinx.ext.autosummary.Autosummary` to fix issue where |
| 16 | + the module name is sometimes duplicated. |
| 17 | +
|
| 18 | + I.e. ``foo.bar.baz()`` became ``foo.bar.foo.bar.baz()``, which of course doesn't exist |
| 19 | + and so resulted in a broken link. |
| 20 | + """ |
| 21 | + |
| 22 | + def import_by_name(self, name: str, prefixes: List[str]) -> Tuple[str, Any, Any, str]: |
| 23 | + real_name, obj, parent, modname = super().import_by_name(name=name, prefixes=prefixes) |
| 24 | + real_name = re.sub(rf"((?:{modname}\.)+)", f"{modname}.", real_name) |
| 25 | + return real_name, obj, parent, modname |
| 26 | + |
| 27 | + |
| 28 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 29 | + app.setup_extension('sphinx.ext.autosummary') |
| 30 | + app.add_directive('autosummary', PatchedAutosummary, override=True) |
| 31 | + |
| 32 | + return { |
| 33 | + 'version': f"{sphinx.__display_version__}-patched-autosummary-0", |
| 34 | + 'parallel_read_safe': True, |
| 35 | + } |
0 commit comments