{"record":{"id":"7bcabddc8c23f27a","repo":"Textualize/rich","slug":"failed-to-auto-generate-rich-repr-error","errorCode":null,"errorMessage":"Failed to auto generate __rich_repr__; {error}","messagePattern":"Failed to auto generate __rich_repr__; (.+?)","errorType":"exception","errorClass":"ReprError","httpStatus":null,"severity":"error","filePath":"rich/repr.py","lineNumber":85,"sourceCode":"        def auto_rich_repr(self: Type[T]) -> Result:\n            \"\"\"Auto generate __rich_rep__ from signature of __init__\"\"\"\n            try:\n                import inspect\n\n                signature = inspect.signature(self.__init__)\n                for name, param in signature.parameters.items():\n                    if param.kind == param.POSITIONAL_ONLY:\n                        yield getattr(self, name)\n                    elif param.kind in (\n                        param.POSITIONAL_OR_KEYWORD,\n                        param.KEYWORD_ONLY,\n                    ):\n                        if param.default is param.empty:\n                            yield getattr(self, param.name)\n                        else:\n                            yield param.name, getattr(self, param.name), param.default\n            except Exception as error:\n                raise ReprError(\n                    f\"Failed to auto generate __rich_repr__; {error}\"\n                ) from None\n\n        if not hasattr(cls, \"__rich_repr__\"):\n            auto_rich_repr.__doc__ = \"Build a rich repr\"\n            cls.__rich_repr__ = auto_rich_repr  # type: ignore[attr-defined]\n\n        auto_repr.__doc__ = \"Return repr(self)\"\n        cls.__repr__ = auto_repr  # type: ignore[assignment]\n        if angular is not None:\n            cls.__rich_repr__.angular = angular  # type: ignore[attr-defined]\n        return cls\n\n    if cls is None:\n        return partial(do_replace, angular=angular)\n    else:\n        return do_replace(cls, angular=angular)\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/repr.py#L67-L103","documentation":"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).","triggerScenarios":" 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.","commonSituations":"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.","solutions":["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).","Write an explicit __rich_repr__ method instead of using @rich.repr.auto when attribute names intentionally differ.","If another decorator mutates the signature, apply @rich.repr.auto to the innermost/actual class or drop it.","Ensure every __init__ parameter is stored on self under exactly that name."],"exampleFix":"# before\n@rich.repr.auto\nclass Box:\n    def __init__(self, size):\n        self._size = size  # ReprError: no attribute 'size'\n\n# after\n@rich.repr.auto\nclass Box:\n    def __init__(self, size):\n        self.size = size","handlingStrategy":"try-catch","validationCode":"import inspect\n\ndef safe_rich_repr(cls):\n    sig = inspect.signature(cls.__init__)\n    missing = [n for n, p in sig.parameters.items()\n               if p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY) and not hasattr(cls, n)]\n    # note: checks the class, not instances; best-effort\n    return not missing","typeGuard":null,"tryCatchPattern":"try:\n    repr(obj)\nexcept ReprError:\n    return f'<{type(obj).__name__}>'  # plain fallback","preventionTips":["Ensure every __init__ parameter is stored as self.<same_name> before using @rich.repr.auto.","Prefer an explicit __rich_repr__ when attribute names diverge from parameters.","Re-test reprs after renaming attributes or stacking decorators over the class."],"tags":["rich","repr","decorator","introspection","attributeerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}