Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP, BUG: Implement custom role for numpydoc parameters. #484

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,45 @@ def _str_examples(self):
else:
return self._str_section("Examples")

def _apply_param_role(self, rendered):
"""Replace every instance of parameters in single backticks with the
parameter role specified in the configuration.

Parameters
----------
rendered : str
The rendered string template as computed in __str__
"""
# TODO: the parameter role should be configured, use :emphasis: for testing
role = ":emphasis:"

# Construct a regex for finding parameter names enclosed in single
# backticks
# TODO: Add [^_] to the end of the expression to filter out sphinx
# external link syntax?
regex = re.compile(
"|".join(
f"`{p.name}`[^_]"
for p in self["Parameters"]
if not p.name.startswith("*") # Ignore *args, **kwargs
)
)

# Define a function to replace single-backticked params with the
# default role
# TODO: Simplify this with smarter regex?
def repl(m):
s = m.group()
start_idx = m.start()
# If the opening backtick is preceded by a ":" or "`", it's already
# a role or literal markup, respectively, and should not be changed
if start_idx != 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit less fragile?

Suggested change
if start_idx != 0:
if start_idx > 0:

if m.string[start_idx - 1] in {":", "`"}:
return s
return f"{role}{s}"

return regex.sub(repl, rendered)

def __str__(self, indent=0, func_role="obj"):
ns = {
"signature": self._str_signature(),
Expand All @@ -381,6 +420,10 @@ def __str__(self, indent=0, func_role="obj"):
ns = {k: "\n".join(v) for k, v in ns.items()}

rendered = self.template.render(**ns)
# Only attempt parameter role mapping if the docstring object *has*
# parameters
if self["Parameters"]:
rendered = self._apply_param_role(rendered)
return "\n".join(self._str_indent(rendered.split("\n"), indent))


Expand Down
Loading