{"record":{"id":"6f2368439211a113","repo":"pola-rs/polars","slug":"cannot-override-cls-name-name-with-custo","errorCode":null,"errorMessage":"cannot override `{cls.__name__}.{name}` with custom namespace {ns_class.__name__!r}","messagePattern":"cannot override `(.+?)\\.(.+?)` with custom namespace (.+?)","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/api.py","lineNumber":60,"sourceCode":"\n        ns_instance = self._ns(instance)  # type: ignore[call-arg]\n        setattr(instance, self._accessor, ns_instance)\n        return ns_instance\n\n\ndef _create_namespace(\n    name: str, cls: type[Expr | DataFrame | LazyFrame | Series]\n) -> Callable[[type[NS]], type[NS]]:\n    \"\"\"Register custom namespace against the underlying Polars class.\"\"\"\n\n    def namespace(ns_class: type[NS]) -> type[NS]:\n        if name in _reserved_namespaces:\n            msg = f\"cannot override reserved namespace {name!r}\"\n            raise AttributeError(msg)\n        elif (attr := getattr(cls, name, None)) is not None:\n            if isfunction(attr) or isinstance(attr, property) or name.startswith(\"_\"):\n                msg = f\"cannot override `{cls.__name__}.{name}` with custom namespace {ns_class.__name__!r}\"\n                raise AttributeError(msg)\n            warn(\n                f\"overriding existing custom namespace {name!r} (on {cls.__name__})\",\n                UserWarning,\n                stacklevel=find_stacklevel(),\n            )\n\n        setattr(cls, name, NameSpace(name, ns_class))\n        cls._accessors.add(name)\n        return ns_class\n\n    return namespace\n\n\ndef register_expr_namespace(name: str) -> Callable[[type[NS]], type[NS]]:\n    \"\"\"\n    Decorator for registering custom functionality with a Polars Expr.\n\n    Parameters","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/api.py#L42-L78","documentation":"AttributeError from _create_namespace (py-polars/src/polars/api.py:57-60). Even if the name is not a reserved polars namespace, registering it is refused when the target class already has an attribute of that name that is a function, a property, or starts with an underscore — i.e. you cannot shadow real methods, properties, or private internals with a custom namespace. Overriding another (non-reserved, non-method) custom namespace only warns.","triggerScenarios":"@pl.register_dataframe_namespace('group_by') (method exists), @pl.register_lazyframe_namespace('collect'), @pl.register_series_namespace('_private') (underscore name), or any name matching an existing function/property on DataFrame/Expr/LazyFrame/Series.","commonSituations":"Extension libraries choosing names ('sort', 'join', 'width') that collide with real methods; private-namespaced internal attributes beginning with '_'.","solutions":["Rename the namespace to something not present on the class: check hasattr(pl.DataFrame, name) is None first","Namespace-qualify the feature, e.g. 'mylib_sort' or 'ops'","Expose functionality as plain functions taking a frame instead of a namespace if the natural name is taken"],"exampleFix":"# before\n@pl.register_dataframe_namespace('group_by')\nclass MyGroupBy: ...\n\n# after\n@pl.register_dataframe_namespace('mygroup')\nclass MyGroupBy: ...","handlingStrategy":"validation","validationCode":"from inspect import isfunction, isproperty\nimport polars as pl\n\ndef safe_namespace_name(name: str, cls=pl.DataFrame) -> bool:\n    attr = getattr(cls, name, None)\n    if attr is None:\n        return True\n    return not (isfunction(attr) or isinstance(attr, property) or name.startswith('_'))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the namespace name is not an existing method/property on the class","Avoid underscore-prefixed names entirely for registered namespaces","Namespace-name-check in a startup test for extension packages"],"tags":["polars","register-namespace","name-collision","attributeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}