High-level description
This code provides a Sphinx extension that customizes the parsing of the “Returns” section in NumPy-style docstrings. It specifically formats the return values to include hyperlinks to their types using the:class: role.
References
This code references theNumpyDocstring class from the sphinx.ext.napoleon module.
Symbols
process_return
Description
This function iterates through a list of lines (presumably from a docstring’s “Returns” section) and reformats lines that match a specific pattern.Inputs
Outputs
Internal Logic
- Iterates through each line in the input
lines. - Attempts to match the line with the regular expression
r"(?P<param>\w+)\s+:\s+(?P<type>[\w.]+)". This pattern captures a “parameter” and its “type” separated by a colon. - If a match is found:
- It reformats the line to
f'-{m["param"]} (:class:~)', which creates a hyperlink for the type using the:class:role.
- It reformats the line to
- If no match is found, it yields the original line.
scanpy_parse_returns_section
Description
This function overrides the_parse_returns_section method of the NumpyDocstring class. It processes the “Returns” section of a docstring to format return values with hyperlinks to their types.
Inputs
Outputs
Internal Logic
- Calls
process_returnto reformat the lines of the “Returns” section. - Formats the processed lines using
_format_blockwith “:returns: ” as the prefix. - Appends an empty line if the formatted lines are not empty.
- Returns the formatted lines.
setup
Description
This function is the entry point for the Sphinx extension. It modifies the behavior of theNumpyDocstring class to use the custom scanpy_parse_returns_section method.
Inputs
Internal Logic
Sets the_parse_returns_section attribute of the NumpyDocstring class to the custom scanpy_parse_returns_section function.
