Skip to content

3.3.0+ Crashes when assigning __init__ rather than defining it #488

Closed
@Daverball

Description

@Daverball

If you do something like the following:

class Base:
    def __init__(self) -> None:
        pass


class SomeMixin:
    if TYPE_CHECKING:
        # forward declare __init__
        __init__ = Base.__init__  # type:ignore[assignment]

Autoapi will crash in the mapping step when trying to access overloads on the class node, this is presumably because the following check does not guard against non-method nodes:

if self.constructor:
overload_data = self.constructor.obj["overloads"]
autodoc_typehints = getattr(
self.app.config, "autodoc_typehints", "signature"
)
show_annotations = autodoc_typehints not in ("none", "description")
overloads = [
(
_format_args(args, show_annotations, ignore_self="self"),
return_annotation,
)
for args, return_annotation in overload_data
]

I couldn't really figure out why this didn't cause a crash prior to 3.3.0, but at the very least it should be fairly simple to avoid a crash here, by adding an additional guard. Doing something more sophisticated would require more work. But I don't think it is necessary, considering this is a really rare hacky workaround and in fact I no longer needed to use it in the places that caused the crash.

But it still can't hurt to avoid this crash in the future, since it was a pain to debug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions