Skip to content

Linking to param in docstring of function itself #11

@Bibo-Joshi

Description

@Bibo-Joshi

Hi. thanks for this awesome extension!

I was very happy to find that it also works with sphinx.ext.napoleon by now :) - learned about this extension via https://stackoverflow.com/a/20845306/10606962 where it said that it didn't support napoleon at the time.
However, I noticed that references are not resolved if you link to a parameter from within the docstring of the function itself, i.e. if you have something like

def foo(arg):
    """:paramref:`arg`.

    Args:
        arg: stuff
    """

TBH I'm not sure, if this specific to napoleon, as I haven't tried with the default docstrings. It is rather easy to work around this, though. Instead of using the built-in PyXRefRole for the paramref role, I used a subclass along the lines

class ParamXRefRole(PyXRefRole):

    def process_link(self, env, refnode, has_explicit_title, title, target):
        title, target = super().process_link(env, refnode, has_explicit_title, title, target)
        try:
            # if we just have :paramref:`arg` and not :paramref:`namespace.arg`, we must assume that
            # the current namespace is meant. Let's extract it from the refnode and prepend it
            if '.' not in target:
                target = f"{refnode.source.rsplit('.', maxsplit=1)[-1]}.{target}"
            return title, target
        except Exception:
            return title, target

For now this does the trick as workaround, but I thought that it could be maybe added to this lib :) By the readme, it's important to you to rely on as few sphinx internals as possible, so I can't judge if this would be acceptable for you. But IISC you use some attributes of nodes already …

Anyway, let me know what you think :) If you like the solution, I could send a PR.

Metadata

Metadata

Assignees

No one assigned

    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