Skip to content

Warn about using F from magic_filter instead of aiogram one #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
evgfilim1 opened this issue Aug 24, 2022 · 0 comments
Closed

Warn about using F from magic_filter instead of aiogram one #990

evgfilim1 opened this issue Aug 24, 2022 · 0 comments
Labels
3.x Issue or PR for stable 3.x version new feature Missing feature under discussion A subject of discussion
Milestone

Comments

@evgfilim1
Copy link
Contributor

evgfilim1 commented Aug 24, 2022

Problem

Sometimes people may be confused why the filter won't pass when they have .as_("arg") at the end of F-expression. The reason is they use from magic_filter import F instead of from aiogram import F. The one that provided by aiogram has an additional method .as_(key: str) that is useful only with aiogram filters: it returns a dictionary if the filter passes, then the result is added to handler kwargs by aiogram.

Possible solution

Throw a warning if the user is using F provided by magic_filter

Adding an isinstance(self.callback, aiogram.utils.MagicFilter) check here would be enough

def __post_init__(self) -> None:
# TODO: Make possibility to extract and explain magic from filter object.
# Current solution is hard for debugging because the MagicFilter instance can't be extracted
if isinstance(self.callback, MagicFilter):
# MagicFilter instance is callable but generates only "CallOperation" instead of applying the filter
self.callback = self.callback.resolve
super().__post_init__()

Alternatives

No response

Code example

from aiogram import Router
from aiogram.types import Message, User
from magic_filter import F

# The filter below won't pass as `User` object has no attribute `as_`,
#  so the handler will never run
@router.message(F.from_user.as_("user"))
# Warning: You are using F provided by magic_filter directly, but
#  it lacks .as_() extension. Please change the import statement:
#  `from magic_filter import F` -> `from aiogram import F` to silence
#  this warning.
async def whoami(message: Message, user: User) -> None:
    return message.reply("You are {user.first_name}")

Additional information

No response

@evgfilim1 evgfilim1 added new feature Missing feature under discussion A subject of discussion 3.x Issue or PR for stable 3.x version labels Aug 24, 2022
@JrooTJunior JrooTJunior added this to the 3.0.0-beta.5 milestone Aug 24, 2022
JrooTJunior added a commit that referenced this issue Sep 27, 2022
JrooTJunior added a commit that referenced this issue Oct 1, 2022
* Rewrite filters

* Update README.rst

* Fixed tests

* Small optimization of the Text filter (TY to @bomzheg)

* Remove dataclass slots argument in due to the only Python 3.10 has an slots argument

* Fixed mypy

* Update tests

* Disable Python 3.11

* Fixed #1013: Empty mention should be None instead of empty string.

* Added #990 to the changelog

* Added #942 to the changelog

* Fixed coverage

* Update poetry and dependencies

* Fixed mypy

* Remove deprecated code

* Added more tests, update pyproject.toml

* Partial update docs

* Added initial Docs translation files

* Added more changes

* Added log message when connection is established in polling process

* Fixed action

* Disable lint for PyPy

* Added changelog for docs translation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issue or PR for stable 3.x version new feature Missing feature under discussion A subject of discussion
Projects
None yet
Development

No branches or pull requests

2 participants