8000 AttributeError: '_data_filter' issue with BaseFilter and _MergedFilter in python-telegram-bot v20.0+ and documentation gap · Issue #4850 · python-telegram-bot/python-telegram-bot · GitHub
[go: up one dir, main page]

Skip to content
AttributeError: '_data_filter' issue with BaseFilter and _MergedFilter in python-telegram-bot v20.0+ and documentation gap #4850
@kadir5131

Description

@kadir5131

Steps to Reproduce

  1. Create a custom filter class inheriting from BaseFilter. In its init method, call super().init()
class MenuFilter(BaseFilter):
    def __init__(self, handler_menu_name: str = None, is_admin_only: bool = False):
        super().__init__()
        self.handler_menu_name = handler_menu_name
        self.is_admin_only = is_admin_only
        # ... other initialization code ...

    # This method is expected to be called, but won't be due to the error
    def __call__(self, update, context) -> bool:
        # ... filtering logic ...
        return True

2.Add this custom filter to a MessageHandler using the & (AND) operator, combined with other built-in filters like

filters.Regex or filters.UpdateType.MESSAGE.
import re
from telegram.ext import MessageHandler, filters

compiled_pattern = re.compile(f"^{re.escape(btn_text)}$")
mf = MenuFilter(handler_menu_name=menu_name, is_admin_only=True)

application.add_handler(
   MessageHandler(
       filters.Regex(compiled_pattern)
       & filters.UpdateType.MESSAGE
       & mf, # Error occurs here
       func,
       block=True
   )
)

3.Run the bot and send a message that would trigger this MessageHandler (e.g., a button text matching btn_text).

Expected behaviour

The init method of the MenuFilter class completes successfully.
The MessageHandler is added without issues.
Upon receiving a message, the call method (or filter method, if inheriting from MessageFilter) of MenuFilter is invoked, and the filtering logic is applied.

Actual behaviour

An AttributeError occurs during the MessageHandler registration:

ERROR    | 2025-07-04 03:35:42 | AnkaNestLogger | Handler kurulumunda hata: 'MenuFilter' object has no attribute '_data_filter'
Traceback (most recent call last):
  File "C:\\main\main.py", line 418, in setup_handlers
    filters.Regex(pattern)
    ~~~~~~~~~~~~~~~~~~~~~~
    & filters.UpdateType.MESSAGE
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    & mf,
    ^~~~
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 201, in __and__
    return _MergedFilter(self, and_filter=other)
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 425, in __init__
    and self.and_filter.data_filter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 249, in data_filter
    return self._data_filter
           ^^^^^^^^^^^^^^^^^
AttributeError: 'MenuFilter' object has no attribute '_data_filter'. Did you mean: 'data_filter'?

Operating System

Windows 11

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 22.2
Bot API 9.0
Python 3.13.5

Relevant log output

ERROR    | 2025-07-04 03:35:42 | AnkaNestLogger | Handler kurulumunda hata: 'MenuFilter' object has no attribute '_data_filter'
Traceback (most recent call last):
  File "C:\main\main.py", line 418, in setup_handlers
    filters.Regex(pattern)
    ~~~~~~~~~~~~~~~~~~~~~~
    & filters.UpdateType.MESSAGE
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    & mf,
    ^~~~
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 201, in __and__
    return _MergedFilter(self, and_filter=other)
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 425, in __init__
    and self.and_filter.data_filter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages\telegram\ext\filters.py", line 249, in data_filter
    return self._data_filter
           ^^^^^^^^^^^^^^^^^
AttributeError: 'MenuFilter' object has no attribute '_data_filter'. Did you mean: 'data_filter'?

Additional Context

This error stems from the _MergedFilter class's init method in filters.py attempting to access the data_filter attribute of the combined filters.

class _MergedFilter(UpdateFilter):
    

    __slots__ = ("and_filter", "base_filter", "or_filter")

    def __init__(
        self,
        base_filter: BaseFilter,
        and_filter: Optional[BaseFilter] = None,
        or_filter: Optional[BaseFilter] = None,
    ):
        super().__init__()
        self.base_filter = base_filter
        if self.base_filter.data_filter:
            self.data_filter = True
        self.and_filter = and_filter
        if (
            self.and_filter
            and not isinstance(self.and_filter, bool)
            and self.and_filter.data_filter #<-- this line leads to error.
        ):
            self.data_filter = True
        self.or_filter = or_filter
        if self.or_filter and not isinstance(self.and_filter, bool) and self.or_filter.data_filter:
            self.data_filter = True

There are very few examples of this in the documentation and I spent 1 day trying to solve this problem and they changed it but even the administrators didn't understand it and told me that I had to use type handler.
https://t.me/pythontelegrambotgroup/780976

from telegram.ext.filters import MessageFilter

class FilterAwesome(MessageFilter):
    def filter(self, message):
        return 'python-telegram-bot is awesome' in message.text

# Remember to initialize the class.
filter_awesome = FilterAwesome()
awesome_handler = MessageHandler(filter_awesome, callback)
application.add_handler(awesome_handler)

BaseFilter was acting as if there was no mf line even though it was there

class MenuFilter(BaseFilter):
    def __init__(self, handler_menu_name: str = None, is_admin_only: bool = False):
        super().__init__(data_filter=False) # Bu satırı ekleyin
        self.handler_menu_name = handler_menu_name
        self.is_admin_only = is_admin_only

    def __call__(self, update, context) -> bool:
        # 1) Mutlaka bir chat olmalı
        chat = update.effective_chat
        if not chat:
            return False

While such examples are helpful for cases where MessageFilter's init cor 5667 rectly initializes data_filter, they do not provide a general example for directly inheriting from BaseFilter and handling the data_filter requirement manually. This can lead to confusion for users who choose to inherit directly from BaseFilter or overlook this specific requirement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0