Skip to content

Add annotation links in signature #65

Closed
@dustinlagoy

Description

@dustinlagoy

Is your feature request related to a problem? Please describe.
I often write functions/classes/etc which (I hope) are self-describing enough to exist without a docstring, or which have a docstring that doesn't describe all of the parameters. In this case the only place the parameters are shown are in the signature but their annotations do not contain links (when the annotation exists somewhere else in the codebase).

Describe the solution you'd like
It would be nice if there were an option to auto-generate these links similarly to how they are handled in the "Parameters" section created from the docstring. For example in the following signature:

def foo(baz: other_module.Baz) -> str:

I would like if the generated html contained a link to other_module.Baz.

Describe alternatives you've considered
Another option could be to create/update the docstring to add parameters based on the signature, similarly to how auto-docstring generators work but in this case leaving the original code untouched. I implemented a quick hack for this in griffe but I think it would require more work to make it a usable feature.

Additional context
I made a proof-of-concept patch (see below) to the function and signature templates to include links in a similar way to the docstring template. If this feature seems useful I could try to complete in a pull request (maybe adding a flag to turn it on/off and applying it to more than just the function template). The current released version makes a signature like:

original

And with my patch it looks like (the blue text has two correctly generated links):

patched

diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/function.html b/src/mkdocstrings_handlers/python/templates/material/_base/function.html
index b9b1696..cc1dacd 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/function.html
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/function.html
@@ -24,10 +24,10 @@
       {% if config.separate_signature %}
         <span class="doc doc-object-name doc-function-name">{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}</span>
       {% else %}
-        {% filter highlight(language="python", inline=True) %}
+        <span class="doc doc-object-name doc-function-name">
           {% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
           {% include "signature.html" with context %}
-        {% endfilter %}
+        </span>
       {% endif %}
 
       {% with labels = function.labels %}
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/signature.html b/src/mkdocstrings_handlers/python/templates/material/_base/signature.html
index bc24ea3..40b7b4b 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/signature.html
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/signature.html
@@ -24,11 +24,21 @@
           {%- endif -%}
 
           {%- if config.show_signature_annotations and parameter.annotation is not none -%}
-            {%- set annotation = ": " + parameter.annotation|safe -%}
+            {%- set annotation -%}
+              {% with expression = parameter.annotation %}
+                {% include "expression.html" with context %}
+              {% endwith %}
+            {%- endset -%}
+            {%- set annotation = ": " + annotation -%}
           {%- endif -%}
 
           {%- if parameter.default is not none and parameter.kind.value != "variadic positional" and parameter.kind.value != "variadic keyword" -%}
-            {%- set default = ns.equal + parameter.default|safe -%}
+            {%- set default -%}
+              {% with expression = parameter.default %}
+                {% include "expression.html" with context %}
+              {% endwith %}
+            {%- endset -%}
+            {%- set default = ns.equal + default -%}
           {%- endif -%}
 
           {%- if parameter.kind.value == "variadic positional" -%}
@@ -42,7 +52,11 @@
         {%- endif -%}
       {%- endfor -%}
     )
-    {%- if config.show_signature_annotations and function.annotation %} -> {{ function.annotation|safe }}{%- endif -%}
+    {%- if config.show_signature_annotations and function.annotation %}
+      {% with expression = function.annotation %}
+        -> {% include "expression.html" with context %}
+      {% endwith %}
+    {%- endif -%}
 
   {%- endwith -%}
 {%- endif -%}
\ No newline at end of file

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions