Skip to content

autodoc: IndexError formatting signature for callable data object with annotated class variable #14576

Description

@AndiEcker

Describe the bug

call-object is rendered without its doc-string and sphinx-build outputs the following warning on the console (printed at sphinx.ext.autodoc._dynamic/_loader.py, line 145):

WARNING: error while formatting signature for <module>.<call_object>: list assignment index out of range [autodoc]

this bug happens on build when documenting a module containing a callable data object under the following conditions:

  • the call-object's class implements __call__
  • the class contains at least one annotated class variable
  • the instance is documented using a trailing #: variable doc-string

How to Reproduce

Minimal reproducer

from typing import Any


class SigBug:
    class_var: Any

    def __call__(self):
        pass


sig_bug = SigBug()  #: trigger

Document the module using autosummary / automodule.

Expected behavior

sig_bug should be documented without warnings and should show its docstring ("trigger") in the rendered doc/html/pdf.

Actual behavior

doc-string is not visible in the rendered doc/html/pdf and console shows

WARNING: error while formatting signature for module.sig_bug:
list assignment index out of range [autodoc]

Environment Information

- Sphinx 9.1.0
- Python 3.12.12

Sphinx extensions

autodoc

Additional context

Debugging

The exception originates from

sphinx/ext/autodoc/_dynamic/_signatures.py

at line 128

signatures[0] = (args, retann if isinstance(retann, str) else '')

At this point the local variables are

props.obj_type == "data"
args == "()"
retann is None
signatures == []
result == ("()", None)

signatures is empty because data objects intentionally skip signature extraction earlier in _format_signatures(), but autodoc-process-signature still returns a replacement signature.

The code assumes that signatures already contains an element although this is not true for obj_type == "data".

A possible fix would be to append a new signature when signatures is empty instead of assigning to signatures[0].

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions