Skip to content

Make default_role aware of aliases. #9453

@randolf-scholz

Description

@randolf-scholz

Is your feature request related to a problem? Please describe.
When we want to make use of the fantastic intersphinx extension, we unfortunately often end up having to write very verbose docstrings such as

Takes the input :class:`~module.submodule.subsubmodule.SomeClass` and ...

This has 3 major disadvantages:

  1. It is very annoying to type.
  2. It is annoying to maintain. If one moves a class definition to some other submodule, all occurrences of :class:`~module.submodule.subsubmodule.SomeClass ` need to be refactored.
  3. It greatly reduces legibility of help() strings when executed from a terminal or in a Jupyter Notebook.

Describe the solution you'd like
My proposal is to add some additional logic to the default_role option. Currently, if we use something like default_role="py:obj", all it seems to do is replace occurrences of `object` with :py:obj:`object` .

Instead, I would suggest that first, a check should be performed to see if `object` is contained in either autodoc_type_aliases (or napoleon_type_aliases if napoleon is enabled).

If the object is found there, then `object` should be replaced with the found alias instead of the "default" default_role. This would elegantly solve all 3 problems outlined above.

Describe alternatives you've considered
None so far. The additional default_role logic should probably come with a boolean switch to turn it off and on.

Additional context

So for instance, if we had

default_role = "py:obj"
napoleon_type_aliases = {
    "Tensor" : ":py:class:`~torch.Tensor`",
}

Then the docstring

"Takes as input an `array_like` and outputs a `Tensor`."

Should be translated to

"Takes as input an :py:obj:`array_like` and outputs a :py:class:`~torch.Tensor`."

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:enhancementenhance or introduce a new feature

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions