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

pdoc should display which dostring line is causing the issue if a ValueError is raised on re.split #758

Open
dhermangh opened this issue Nov 18, 2024 · 2 comments

Comments

@dhermangh
Copy link

dhermangh commented Nov 18, 2024

Problem Description

In Returns section, if the numpy docstring is missing a type description, a ValueError is raised. It is fine, however a message displaing which line raised the error should be displayed, helping catch which docstring lines are incorrect. Sometimes linters miss such errors.
For example such docstring:

def foobar(x: str) -> Y:
    """
    I am a docstring in a numpy format

    Parameters
    ----------
    x: str
        I'm a parameter of a str type.

    Returns
    -------
        Tuple containing status message string and CPE, time delta or None
    """

Will raise said ValueError:

Traceback (most recent call last):
  File "/home/qabot/venvs/qabot_venv/bin/pdoc", line 8, in <module>
    sys.exit(cli())
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/__main__.py", line 199, in cli
    pdoc.pdoc(
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/__init__.py", line 529, in pdoc
    out = render.html_module(module, all_modules)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/render.py", line 106, in html_module
    return env.get_template("module.html.jinja2").render(
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/module.html.jinja2", line 311, in top-level template code
    {%- if loop.nextitem -%}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/frame.html.jinja2", line 36, in top-level template code
    {% block body %}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/frame.html.jinja2", line 42, in block 'body'
    {% block content %}{% endblock %}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/module.html.jinja2", line 101, in block 'content'
    {% block module_contents %}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/module.html.jinja2", line 104, in block 'module_contents'
    {{ member(m) }}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/jinja2/runtime.py", line 782, in _invoke
    rv = self._func(*arguments)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/module.html.jinja2", line 208, in template
    {{ docstring(doc) }}
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/jinja2/runtime.py", line 782, in _invoke
    rv = self._func(*arguments)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/templates/default/module.html.jinja2", line 212, in template
    <div class="docstring">{{ var.docstring | replace("@public", "") | to_markdown | to_html | linkify(namespace=var.qualname, shorten=False) }}</div>
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/render_helpers.py", line 193, in to_markdown_with_context
    return to_markdown(docstring, module, docformat)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/render_helpers.py", line 198, in to_markdown
    return docstrings.convert(docstring, docformat, module.source_file)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/docstrings.py", line 43, in convert
    docstring = numpy(docstring)
  File "/home/qabot/venvs/qabot_venv/lib/python3.10/site-packages/pdoc/docstrings.py", line 187, in numpy
    content, tail = re.split(r"\n(?![ \n])", content, maxsplit=1)
ValueError: not enough values to unpack (expected 2, got 1)

The traceback is not helpful, it should contain the exception message itself and the offending docstring line. If it is possible - it should also contain path to the offending file.

Proposal

A message with the offending line should be printed (it would be nice to also have file path to the offending file). As a temp fix in my local envirnoment I've added a try...except block:

try:
  content, tail = re.split(r"\n(?![ \n])", content, maxsplit=1)
except ValueError as e:
  print(content)
  raise

Of course, a better logging/printing should be utilized.

@mhils
Copy link
Member

mhils commented Nov 18, 2024 via email

@dhermangh
Copy link
Author

Sure, in that case I'll prepare a PR soon. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants