sphinx-autodoc-typehints icon indicating copy to clipboard operation
sphinx-autodoc-typehints copied to clipboard

`IndexError` may be raised if `typehints_defaults = 'braces-after'` is combined with `always_document_param_types = True`

Open christianaguilera-foundry opened this issue 2 months ago • 0 comments

The exception is:

    Traceback (most recent call last):
      File ".../sphinx/events.py", line 404, in emit
        results.append(listener.handler(self.app, *args))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File ".../sphinx_autodoc_typehints/__init__.py", line 743, in process_docstring
        _inject_types_to_docstring(type_hints, signature, original_obj, app, what, name, lines)
      File ".../sphinx_autodoc_typehints/__init__.py", line 799, in _inject_types_to_docstring
        _inject_signature(type_hints, signature, app, lines)
      File ".../sphinx_autodoc_typehints/__init__.py", line 845, in _inject_signature
        type_annotation = _append_default(app, lines, insert_index, type_annotation, formatted_default)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File ".../sphinx_autodoc_typehints/__init__.py", line 864, in _append_default
        lines[append_index] += formatted_default
        ~~~~~^^^^^^^^^^^^^^
    IndexError: list index out of range

This code path is only reached if typehints_defaults is set to 'braces-after'.

It seems that, if always_document_param_types is enabled, insert_index may take a value that is out of bounds:

        if annotation is not None and insert_index is None and app.config.always_document_param_types:
            lines.append(f":param {arg_name}:")
            insert_index = len(lines)

A naive solution would be to subtract 1 so that insert_index stores the index to the last line. EDIT: This suggestion may or may not be accurate.