# Buggy code
async def check(self, obj: Union[Message, CallbackQuery, InlineQuery]):
    if isinstance(obj, Message):
        user_id = obj.from_user.id
        chat_id = obj.chat.id
    elif isinstance(obj, CallbackQuery):
        user_id = obj.from_user.id
        chat_id = None
        if obj.message is not None:
            # if the button was sent with message
            chat_id = obj.message.chat.id
    elif isinstance(obj, InlineQuery):
        user_id = obj.from_user.id
        chat_id = None
    else:
        return False

    if self.user_id and self.chat_id:
        return user_id in self.user_id and chat_id in self.chat_id
    if self.user_id:
        return user_id in self.user_id
    if self.chat_id:
        return chat_id in self.chat_id

    return False


# Traceback (AttributeError)
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<Dispatcher._process_polling_updates() done, defined at /home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/dispatcher.py:331> exception=AttributeError("'NoneType' object has no attribute 'id'")>
Traceback (most recent call last):
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/dispatcher.py", line 339, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/dispatcher.py", line 194, in process_updates
return await asyncio.gather(*tasks)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/dispatcher.py", line 221, in process_update
return await self.channel_post_handlers.notify(update.channel_post)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/handler.py", line 108, in notify
data.update(await check_filters(handler_obj.filters, args))
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/filters/filters.py", line 72, in check_filters
f = await execute_filter(filter_, args)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/filters/filters.py", line 56, in execute_filter
return await filter_.filter(*args, **filter_.kwargs)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/filters/filters.py", line 161, in __call__
return await self.check(*args)
File "/home/user/PycharmProjects/other-things/venv/lib/python3.8/site-packages/aiogram/dispatcher/filters/builtin.py", line 594, in check
user_id = obj.from_user.id
AttributeError: 'NoneType' object has no attribute 'id'

# Fixed code
async def check(self, obj: Union[Message, CallbackQuery, InlineQuery]):
    if isinstance(obj, Message):
        user_id = None
        if obj.from_user is not None:
            user_id = obj.from_user.id
        chat_id = obj.chat.id
    elif isinstance(obj, CallbackQuery):
        user_id = obj.from_user.id
        chat_id = None
        if obj.message is not None:
            # if the button was sent with message
            chat_id = obj.message.chat.id
    elif isinstance(obj, InlineQuery):
        user_id = obj.from_user.id
        chat_id = None
    else:
        return False

    if self.user_id and self.chat_id:
        return user_id in self.user_id and chat_id in self.chat_id
    if self.user_id:
        return user_id in self.user_id
    if self.chat_id:
        return chat_id in self.chat_id

    return False