{"record":{"id":"c374ad11e106db8f","repo":"python-telegram-bot/python-telegram-bot","slug":"cannot-set-name-for-filters-self-class-name","errorCode":null,"errorMessage":"Cannot set name for filters.{self.__class__.__name__}","messagePattern":"Cannot set name for filters\\.(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"warning","filePath":"src/telegram/ext/filters.py","lineNumber":815,"sourceCode":"        chat_or_user = self._get_chat_or_user(message)\n        if chat_or_user:\n            if self.chat_ids:\n                return chat_or_user.id in self.chat_ids\n            if self.usernames:\n                return bool(chat_or_user.username and chat_or_user.username in self.usernames)\n            return self.allow_empty\n        return False\n\n    @property\n    def name(self) -> str:\n        return (\n            f\"filters.{self.__class__.__name__}(\"\n            f\"{', '.join(str(s) for s in (self.usernames or self.chat_ids))})\"\n        )\n\n    @name.setter\n    def name(self, _: str) -> NoReturn:\n        raise RuntimeError(f\"Cannot set name for filters.{self.__class__.__name__}\")\n\n\nclass Chat(_ChatUserBaseFilter):\n    \"\"\"Filters messages to allow only those which are from a specified chat ID or username.\n\n    Examples:\n        ``MessageHandler(filters.Chat(-1234), callback_method)``\n\n    Warning:\n        :attr:`chat_ids` will give a *copy* of the saved chat ids as :class:`frozenset`. This\n        is to ensure thread safety. To add/remove a chat, you should use :meth:`add_chat_ids`, and\n        :meth:`remove_chat_ids`. Only update the entire set by ``filter.chat_ids = new_set``,\n        if you are entirely sure that it is not causing race conditions, as this will complete\n        replace the current set of allowed chats.\n\n    Args:\n        chat_id(:obj:`int` | Collection[:obj:`int`], optional):\n            Which chat ID(s) to allow through.","sourceCodeStart":797,"sourceCodeEnd":833,"githubUrl":"https://github.com/python-telegram-bot/python-telegram-bot/blob/d3b69d2e9fb7af6c796f84516cfda751752c7cb7/src/telegram/ext/filters.py#L797-L833","documentation":"Filters like `filters.Chat` and `filters.User` generate their `name` from their current chat_ids/usernames. Since the name is derived state, assigning `.name` on these filter instances raises RuntimeError.","triggerScenarios":"Executing `filters.Chat(chat_id=1).name = \"x\"` — any assignment to `.name` on `_ChatUserBaseFilter` subclasses (Chat, User, ForwardedFrom, and similar).","commonSituations":"Applying the custom-filter naming convention (`self.name = ...`) to built-in filters, or generic code that labels every filter object it receives.","solutions":["Remove the `.name` assignment for built-in Chat/User filters; the name auto-reflects its configured IDs/usernames.","For a custom label, subclass the filter and override the `name` property instead of assigning it."],"exampleFix":"# before\nf = filters.Chat(chat_id=123)\nf.name = \"my-chat\"  # RuntimeError\n\n# after\nf = filters.Chat(chat_id=123)  # name auto: filters.Chat(123)","handlingStrategy":"validation","validationCode":"# do not assign .name to Chat/User filters; subclass instead\nclass MyChat(filters.Chat):\n    @property\n    def name(self):\n        return 'my-chat'","typeGuard":null,"tryCatchPattern":"try:\n    f.name = 'x'\nexcept RuntimeError as e:\n    if 'Cannot set name' in str(e):\n        pass","preventionTips":["Reserve name assignment for custom filter subclasses."],"tags":["python-telegram-bot","filters","immutable-attribute","derived-name"],"backgroundTag":"readonly-property-assignment","analyzedSha":"d3b69d2e9fb7af6c796f84516cfda751752c7cb7","analyzedAt":"2026-08-28T16:18:54.805Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}