Textualize/rich · error · ReprError

Failed to auto generate __rich_repr__; {error}

Error message

Failed to auto generate __rich_repr__; {error}

What it means

The @rich.repr.auto decorator generates __rich_repr__ by reflecting over the class __init__ signature and doing getattr(self, param_name) for each parameter. If that introspection fails — a parameter with no matching attribute, a stale signature, getattr raising — rich wraps the cause in ReprError('Failed to auto generate __rich_repr__; {error}'). The message includes the underlying error, and the original exception is suppressed (from None).

Source

Thrown at rich/repr.py:85

        def auto_rich_repr(self: Type[T]) -> Result:
            """Auto generate __rich_rep__ from signature of __init__"""
            try:
                import inspect

                signature = inspect.signature(self.__init__)
                for name, param in signature.parameters.items():
                    if param.kind == param.POSITIONAL_ONLY:
                        yield getattr(self, name)
                    elif param.kind in (
                        param.POSITIONAL_OR_KEYWORD,
                        param.KEYWORD_ONLY,
                    ):
                        if param.default is param.empty:
                            yield getattr(self, param.name)
                        else:
                            yield param.name, getattr(self, param.name), param.default
            except Exception as error:
                raise ReprError(
                    f"Failed to auto generate __rich_repr__; {error}"
                ) from None

        if not hasattr(cls, "__rich_repr__"):
            auto_rich_repr.__doc__ = "Build a rich repr"
            cls.__rich_repr__ = auto_rich_repr  # type: ignore[attr-defined]

        auto_repr.__doc__ = "Return repr(self)"
        cls.__repr__ = auto_repr  # type: ignore[assignment]
        if angular is not None:
            cls.__rich_repr__.angular = angular  # type: ignore[attr-defined]
        return cls

    if cls is None:
        return partial(do_replace, angular=angular)
    else:
        return do_replace(cls, angular=angular)

View on GitHub (pinned to 9d8f9a372c)

Solutions

  1. Check the appended {error} detail: it names the attribute that failed (usually an AttributeError) — align attribute names with __init__ parameter names (self.size = size, not self._size = size).
  2. Write an explicit __rich_repr__ method instead of using @rich.repr.auto when attribute names intentionally differ.
  3. If another decorator mutates the signature, apply @rich.repr.auto to the innermost/actual class or drop it.
  4. Ensure every __init__ parameter is stored on self under exactly that name.

Example fix

# before
@rich.repr.auto
class Box:
    def __init__(self, size):
        self._size = size  # ReprError: no attribute 'size'

# after
@rich.repr.auto
class Box:
    def __init__(self, size):
        self.size = size
Defensive patterns

Strategy: try-catch

Validate before calling

import inspect

def safe_rich_repr(cls):
    sig = inspect.signature(cls.__init__)
    missing = [n for n, p in sig.parameters.items()
               if p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY) and not hasattr(cls, n)]
    # note: checks the class, not instances; best-effort
    return not missing

Try / catch

try:
    repr(obj)
except ReprError:
    return f'<{type(obj).__name__}>'  # plain fallback

Prevention

When it happens

Trigger: Decorating a class whose __init__ takes parameters that are never stored as same-named attributes (e.g. def __init__(self, size): self._size = size); a class using __slots__ missing an attribute; a dynamically-generated or signature-obscuring __init__ (functools.wraps, C extension, *args swallowing); getattr raising a property exception.

Common situations: Applying @rich.repr.auto as a shortcut during refactoring and later renaming stored attributes with a leading underscore; decorating dataclasses or attrs classes with custom init signatures; decorator stacking where another decorator replaces __init__ after @rich.repr.auto captured the signature.

Related errors


AI-assisted analysis of Textualize/rich@9d8f9a372c (2026-08-15). Data as JSON: /api/errors/7bcabddc8c23f27a. Report an issue: GitHub.