Skip to content

Commit

Permalink
Can link to parameters of generic base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
AWhetter committed Mar 20, 2021
1 parent 643fed0 commit 7872ed0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions autoapi/mappers/python/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import operator
import os
import re

import sphinx.environment
import sphinx.util
Expand Down Expand Up @@ -196,6 +197,28 @@ def _resolve_placeholder(placeholder, original):
placeholder.update(new)


def _link_objs(value):
result = ""

delims = r"(\s*[\[\]\(\),]\s*)"
delims_re = re.compile(delims)
sub_targets = re.split(delims, value.strip())

for sub_target in sub_targets:
sub_target = sub_target.strip()
if delims_re.match(sub_target):
result += f"{sub_target}"
if sub_target.endswith(","):
result += " "
else:
result += "\\ "
elif sub_target:
result += f":py:obj:`{sub_target}`\ "

# Strip off the extra "\ "
return result[:-2]


class PythonSphinxMapper(SphinxMapperBase):

"""Auto API domain handler for Python
Expand Down Expand Up @@ -223,6 +246,7 @@ class PythonSphinxMapper(SphinxMapperBase):
def __init__(self, app, template_dir=None, url_root=None):
super(PythonSphinxMapper, self).__init__(app, template_dir, url_root)

self.jinja_env.filters["link_objs"] = _link_objs
self._use_implicit_namespace = (
self.app.config.autoapi_python_use_implicit_namespaces
)
Expand Down
2 changes: 1 addition & 1 deletion autoapi/templates/python/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

{% if obj.bases %}
{% if "show-inheritance" in autoapi_options %}
Bases: {% for base in obj.bases %}:class:`{{ base }}`{% if not loop.last %}, {% endif %}{% endfor %}
Bases: {% for base in obj.bases %}{{ base|link_objs }}{% if not loop.last %}, {% endif %}{% endfor %}
{% endif %}


Expand Down

0 comments on commit 7872ed0

Please sign in to comment.