diff --git a/pyproject.toml b/pyproject.toml
index f1ff7334..0eccf7fe 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -29,8 +29,8 @@ classifiers = [
"Typing :: Typed",
]
dependencies = [
- "mkdocstrings>=0.25",
- "mkdocs-autorefs>=1.0",
+ "mkdocstrings>=0.26",
+ "mkdocs-autorefs>=1.2",
"griffe>=0.49",
]
diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py
index a4a5b47c..eb1d73c7 100644
--- a/src/mkdocstrings_handlers/python/handler.py
+++ b/src/mkdocstrings_handlers/python/handler.py
@@ -426,6 +426,7 @@ def update_env(self, md: Markdown, config: dict) -> None:
self.env.filters["as_functions_section"] = rendering.do_as_functions_section
self.env.filters["as_classes_section"] = rendering.do_as_classes_section
self.env.filters["as_modules_section"] = rendering.do_as_modules_section
+ self.env.globals["AutorefsHook"] = rendering.AutorefsHook
self.env.tests["existing_template"] = lambda template_name: template_name in self.env.list_templates()
def get_anchors(self, data: CollectorItem) -> tuple[str, ...]: # noqa: D102 (ignore missing docstring)
diff --git a/src/mkdocstrings_handlers/python/rendering.py b/src/mkdocstrings_handlers/python/rendering.py
index d5a88699..2c4a4893 100644
--- a/src/mkdocstrings_handlers/python/rendering.py
+++ b/src/mkdocstrings_handlers/python/rendering.py
@@ -22,6 +22,7 @@
)
from jinja2 import TemplateNotFound, pass_context, pass_environment
from markupsafe import Markup
+from mkdocs_autorefs.references import AutorefsHookInterface
from mkdocstrings.loggers import get_logger
if TYPE_CHECKING:
@@ -571,3 +572,60 @@ def do_as_modules_section(
A modules docstring section.
"""
return DocstringSectionModules([])
+
+
+class AutorefsHook(AutorefsHookInterface):
+ """Autorefs hook.
+
+ With this hook, we're able to add context to autorefs (cross-references),
+ such as originating file path and line number, to improve error reporting.
+ """
+
+ def __init__(self, current_object: Object | Alias, config: dict[str, Any]) -> None:
+ """Initialize the hook.
+
+ Parameters:
+ current_object: The object being rendered.
+ config: The configuration dictionary.
+ """
+ self.current_object = current_object
+ self.config = config
+
+ def expand_identifier(self, identifier: str) -> str:
+ """Expand an identifier.
+
+ Parameters:
+ identifier: The identifier to expand.
+
+ Returns:
+ The expanded identifier.
+ """
+ return identifier
+
+ def get_context(self) -> AutorefsHookInterface.Context:
+ """Get the context for the current object.
+
+ Returns:
+ The context.
+ """
+ role = {
+ "attribute": "data" if self.current_object.parent and self.current_object.parent.is_module else "attr",
+ "class": "class",
+ "function": "meth" if self.current_object.parent and self.current_object.parent.is_class else "func",
+ "module": "mod",
+ }.get(self.current_object.kind.value.lower(), "obj")
+ origin = self.current_object.path
+ try:
+ filepath = self.current_object.docstring.parent.filepath # type: ignore[union-attr]
+ lineno = self.current_object.docstring.lineno or 0 # type: ignore[union-attr]
+ except AttributeError:
+ filepath = self.current_object.filepath
+ lineno = 0
+
+ return AutorefsHookInterface.Context(
+ domain="py",
+ role=role,
+ origin=origin,
+ filepath=str(filepath),
+ lineno=lineno,
+ )
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring.html.jinja
index bc561b14..14d11d03 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring.html.jinja
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring.html.jinja
@@ -19,35 +19,37 @@ Context:
-#}
{{ log.debug("Rendering docstring") }}
{% endblock logs %}
- {% for section in docstring_sections %}
- {% if config.show_docstring_description and section.kind.value == "text" %}
- {{ section.value|convert_markdown(heading_level, html_id) }}
- {% elif config.show_docstring_attributes and section.kind.value == "attributes" %}
- {% include "docstring/attributes"|get_template with context %}
- {% elif config.show_docstring_functions and section.kind.value == "functions" %}
- {% include "docstring/functions"|get_template with context %}
- {% elif config.show_docstring_classes and section.kind.value == "classes" %}
- {% include "docstring/classes"|get_template with context %}
- {% elif config.show_docstring_modules and section.kind.value == "modules" %}
- {% include "docstring/modules"|get_template with context %}
- {% elif config.show_docstring_parameters and section.kind.value == "parameters" %}
- {% include "docstring/parameters"|get_template with context %}
- {% elif config.show_docstring_other_parameters and section.kind.value == "other parameters" %}
- {% include "docstring/other_parameters"|get_template with context %}
- {% elif config.show_docstring_raises and section.kind.value == "raises" %}
- {% include "docstring/raises"|get_template with context %}
- {% elif config.show_docstring_warns and section.kind.value == "warns" %}
- {% include "docstring/warns"|get_template with context %}
- {% elif config.show_docstring_yields and section.kind.value == "yields" %}
- {% include "docstring/yields"|get_template with context %}
- {% elif config.show_docstring_receives and section.kind.value == "receives" %}
- {% include "docstring/receives"|get_template with context %}
- {% elif config.show_docstring_returns and section.kind.value == "returns" %}
- {% include "docstring/returns"|get_template with context %}
- {% elif config.show_docstring_examples and section.kind.value == "examples" %}
- {% include "docstring/examples"|get_template with context %}
- {% elif config.show_docstring_description and section.kind.value == "admonition" %}
- {% include "docstring/admonition"|get_template with context %}
- {% endif %}
- {% endfor %}
+ {% with autoref_hook = AutorefsHook(obj, config) %}
+ {% for section in docstring_sections %}
+ {% if config.show_docstring_description and section.kind.value == "text" %}
+ {{ section.value|convert_markdown(heading_level, html_id, autoref_hook=autoref_hook) }}
+ {% elif config.show_docstring_attributes and section.kind.value == "attributes" %}
+ {% include "docstring/attributes"|get_template with context %}
+ {% elif config.show_docstring_functions and section.kind.value == "functions" %}
+ {% include "docstring/functions"|get_template with context %}
+ {% elif config.show_docstring_classes and section.kind.value == "classes" %}
+ {% include "docstring/classes"|get_template with context %}
+ {% elif config.show_docstring_modules and section.kind.value == "modules" %}
+ {% include "docstring/modules"|get_template with context %}
+ {% elif config.show_docstring_parameters and section.kind.value == "parameters" %}
+ {% include "docstring/parameters"|get_template with context %}
+ {% elif config.show_docstring_other_parameters and section.kind.value == "other parameters" %}
+ {% include "docstring/other_parameters"|get_template with context %}
+ {% elif config.show_docstring_raises and section.kind.value == "raises" %}
+ {% include "docstring/raises"|get_template with context %}
+ {% elif config.show_docstring_warns and section.kind.value == "warns" %}
+ {% include "docstring/warns"|get_template with context %}
+ {% elif config.show_docstring_yields and section.kind.value == "yields" %}
+ {% include "docstring/yields"|get_template with context %}
+ {% elif config.show_docstring_receives and section.kind.value == "receives" %}
+ {% include "docstring/receives"|get_template with context %}
+ {% elif config.show_docstring_returns and section.kind.value == "returns" %}
+ {% include "docstring/returns"|get_template with context %}
+ {% elif config.show_docstring_examples and section.kind.value == "examples" %}
+ {% include "docstring/examples"|get_template with context %}
+ {% elif config.show_docstring_description and section.kind.value == "admonition" %}
+ {% include "docstring/admonition"|get_template with context %}
+ {% endif %}
+ {% endfor %}
+ {% endwith %}
{% endif %}
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/admonition.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/admonition.html.jinja
index 70f462db..ff6e9cbc 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/admonition.html.jinja
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/admonition.html.jinja
@@ -15,6 +15,6 @@ Context:
{% endblock logs %}
{{ section.title|convert_markdown(heading_level, html_id, strip_paragraph=True) }}
- {{ section.value.contents|convert_markdown(heading_level, html_id) }}
+ {{ section.title|convert_markdown(heading_level, html_id, strip_paragraph=True, autoref_hook=autoref_hook) }}
+ {{ section.value.contents|convert_markdown(heading_level, html_id, autoref_hook=autoref_hook) }}
{{ attribute.name }}
{% if attribute.annotation %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/classes.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/classes.html.jinja index a3b494c9..82bae98c 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/classes.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/classes.html.jinja @@ -35,7 +35,7 @@ Context:
{{ class.name }}
{{ class.name }}
–
{{ class.name }}
{{ section.title or lang.t("Examples:") }}
{% for section_type, sub_section in section.value %} {% if section_type.value == "text" %} - {{ sub_section|convert_markdown(heading_level, html_id) }} + {{ sub_section|convert_markdown(heading_level, html_id, autoref_hook=autoref_hook) }} {% elif section_type.value == "examples" %} {{ sub_section|highlight(language="pycon", linenums=False) }} {% endif %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/functions.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/functions.html.jinja index efaf9a37..f979f4e5 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/functions.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/functions.html.jinja @@ -36,7 +36,7 @@ Context:{{ function.name }}
{{ function.name }}
–
{{ function.name }}
{{ module.name }}
{{ module.name }}
–
{{ module.name }}
{{ parameter.name }}
{% if parameter.annotation %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja index a2db4900..8b0556f3 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/parameters.html.jinja @@ -44,7 +44,7 @@ Context:
{{ parameter.name }}
{% if parameter.annotation %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html.jinja index 21490f4e..f796494b 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/raises.html.jinja @@ -41,7 +41,7 @@ Context:
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html.jinja index d402369a..cab2ca77 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html.jinja @@ -44,7 +44,7 @@ Context:
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html.jinja index 702b9351..a892244a 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html.jinja @@ -41,7 +41,7 @@ Context:
diff --git a/src/mkdocstrings_handlers/python/templates/readthedocs/_base/docstring/attributes.html.jinja b/src/mkdocstrings_handlers/python/templates/readthedocs/_base/docstring/attributes.html.jinja index f02e0b9c..8df3f9f3 100644 --- a/src/mkdocstrings_handlers/python/templates/readthedocs/_base/docstring/attributes.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/readthedocs/_base/docstring/attributes.html.jinja @@ -38,7 +38,7 @@ Context: {% endif %} –