Skip to content

Overhaul of Filters #2759

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

Merged
merged 69 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
5948095
Temporarily enable tests for the v14 branch
Bibo-Joshi Jul 14, 2021
53aca25
Move and Rename TelegramDecryptionError to telegram.error.PassportDec…
harshil21 Aug 11, 2021
af2516b
Add Code Comment Guidelines to Contribution Guide (#2612)
Poolitzer Aug 11, 2021
b5723f0
Improve Type Hinting for CallbackContext (#2587)
revolter Aug 12, 2021
6ca92ec
Add Custom pytest Marker to Ease Development (#2628)
Poolitzer Aug 12, 2021
efea686
Make BasePersistence Methods Abstract (#2624)
starry-shivam Aug 12, 2021
104eb17
Refactor Initialization of Persistence Classes (#2604)
Bibo-Joshi Aug 13, 2021
ab262ae
Remove `__dict__` from `__slots__` and drop Python 3.6 (#2619, #2636)
harshil21 Aug 19, 2021
8b7cbcc
Remove day_is_strict argument of JobQueue.run_monthly (#2634)
iota-008 Aug 26, 2021
5630f98
Drop Non-CallbackContext API (#2617)
Poolitzer Aug 29, 2021
09043a4
Fix Signatures and Improve test_official (#2643)
harshil21 Aug 29, 2021
d7a286c
Remove Deprecated Functionality (#2644)
Bibo-Joshi Aug 30, 2021
d652a86
Add User Friendly Type Check For Init Of {Inline, Reply}KeyboardMarku…
harshil21 Sep 15, 2021
bd1e28c
Refine Dispatcher.dispatch_error (#2660)
Bibo-Joshi Sep 17, 2021
a6673c8
Warnings Overhaul (#2662)
harshil21 Sep 20, 2021
b1017ef
Clear Up Import Policy (#2671)
Bibo-Joshi Sep 22, 2021
bf62537
Make InlineQuery.answer Raise ValueError (#2675)
Bibo-Joshi Sep 24, 2021
2b4ab57
Doc Fixes (#2597)
Bibo-Joshi Oct 1, 2021
b630e1b
Move Defaults to telegram.ext (#2648)
Bibo-Joshi Oct 3, 2021
b0385c1
Update Notification Workflows (#2695)
Poolitzer Oct 3, 2021
681393f
Pass Failing Jobs to Error Handlers (#2692)
Bibo-Joshi Oct 3, 2021
2c44dc0
Improve Signature Inspection for Bot Methods (#2686)
harshil21 Oct 3, 2021
6965ae9
Handle Filepaths via the Pathlib Module (#2688)
eldbud Oct 5, 2021
13433e3
Add Filters.update.edited (#2705)
PhilippFr Oct 7, 2021
01be85f
Use Error Messages for Pylint Instead of Codes (#2700)
Piraty Oct 8, 2021
7ba5b3a
Introduce Builder Pattern for Updater and Dispatcher (#2646)
Bibo-Joshi Oct 9, 2021
eec90a0
Mark Internal Modules As Private (#2687)
Oct 10, 2021
163d598
Accept File Paths for Updater/DispatcherBuilder.private_key (#2724)
Bibo-Joshi Oct 11, 2021
1adc96c
Make Tests Agnostic of the CWD (#2727)
eldbud Oct 13, 2021
38a6a6d
Refactor MRO of InputMedia* and Some File-Like Classes (#2717)
eldbud Oct 15, 2021
b24d7d8
Introduce Enums for telegram.constants (#2708)
Bibo-Joshi Oct 19, 2021
5352a05
Introduce TelegramObject.set/get_bot (#2712)
zpavloudis Oct 21, 2021
8f4105d
Remove Job.job_queue (#2740)
Bibo-Joshi Oct 21, 2021
49d8c62
Drop `__dict__` from `Dispatcher.__slots__` (#2745)
Bibo-Joshi Oct 24, 2021
36d09df
Update Exceptions for Immutable Attributes (#2749)
harshil21 Oct 26, 2021
bff5bc5
simply remove the Filters class
harshil21 Oct 28, 2021
4b49fe3
Apply KISS and update docs accordingly
harshil21 Oct 29, 2021
194e910
Start refactoring filters with __call__
harshil21 Oct 29, 2021
acdc66a
Use mimetypes module in filters where possible
harshil21 Oct 29, 2021
c42d4cd
apply KISS to self.name
harshil21 Oct 29, 2021
8488b2e
add shortcuts with allow_empty=True for ChatUserBase subclasses
harshil21 Oct 29, 2021
86ae349
start using check_update + fixes in Dice
harshil21 Oct 30, 2021
c81a087
Update Code Quality Dependencies (#2748)
harshil21 Oct 30, 2021
577a46d
few doc fixes + another fix for Dice
harshil21 Oct 30, 2021
426327a
Apply check_update for filters in handlers and update their docs
harshil21 Oct 30, 2021
3f2aaf4
filters docs overhaul + apply KISS more consistently
harshil21 Oct 30, 2021
2fa2b63
Get filter tests running!
harshil21 Oct 30, 2021
5f67544
Apply new filter syntax project wide + get all tests running!
harshil21 Oct 30, 2021
f307393
Update some more docs
harshil21 Oct 30, 2021
cd9d8f0
merge v14 + fix conflict
harshil21 Oct 30, 2021
070b14c
deepsource + other tests
harshil21 Oct 31, 2021
da57e02
Address most of the review
harshil21 Nov 3, 2021
98ae286
Convert partials to classes :(
harshil21 Nov 3, 2021
dbeb573
Add versionchanged and some doc fixes
harshil21 Nov 3, 2021
e6df7d0
some more dice name fixes
harshil21 Nov 3, 2021
b3fc965
Make USER, CHAT, VIA_BOT independent of _CUBF
harshil21 Nov 4, 2021
171e7ff
Convert `BaseFilter`'s `__new__` to `__init__`
harshil21 Nov 4, 2021
b74ac36
make docs consistent
harshil21 Nov 4, 2021
0964cce
resolve most of code review (mostly docs)
harshil21 Nov 6, 2021
04bb4cd
dice optimizations
harshil21 Nov 6, 2021
17f0ccd
changes to Message/UpdateFilter.check_update
harshil21 Nov 12, 2021
54ed706
remove filters.UpdateType.ALL and call super().check_update instead
harshil21 Nov 13, 2021
9522a60
Add check_update to docs and address review
harshil21 Nov 13, 2021
499388e
pre-commit
harshil21 Nov 13, 2021
fbfa8cc
use pylint ignore instead of type ignore
harshil21 Nov 19, 2021
d54ab3b
merge v14 + fix conflicts
harshil21 Nov 19, 2021
de5809f
fix more merge conflicts
harshil21 Nov 19, 2021
2964b08
sort filters alphabetically
harshil21 Nov 19, 2021
ec31aab
deepsource false positive
harshil21 Nov 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
import inspect
from docutils.nodes import Element
from sphinx.application import Sphinx
from sphinx.domains.python import PyXRefRole
Expand Down Expand Up @@ -358,7 +359,23 @@ def process_link(self, env: BuildEnvironment, refnode: Element,


def autodoc_skip_member(app, what, name, obj, skip, options):
pass
"""We use this to not document certain members like filter() or check_update() for filters.
See https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#skipping-members"""

included = {'MessageFilter', 'UpdateFilter'} # filter() and check_update() only for these.
included_in_obj = any(inc in repr(obj) for inc in included)

if included_in_obj: # it's difficult to see if check_update is from an inherited-member or not
for frame in inspect.stack(): # From https://github.com/sphinx-doc/sphinx/issues/9533
if frame.function == "filter_members":
docobj = frame.frame.f_locals["self"].object
if not any(inc in str(docobj) for inc in included) and name == 'check_update':
return True
break

if name == 'filter' and obj.__module__ == 'telegram.ext.filters':
if not included_in_obj:
return True # return True to exclude from docs.


def setup(app: Sphinx):
Expand Down
5 changes: 5 additions & 0 deletions docs/source/telegram.ext.filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
telegram.ext.filters Module
===========================

.. :bysource: since e.g filters.CHAT is much above filters.Chat() in the docs when it shouldn't.
The classes in `filters.py` are sorted alphabetically such that :bysource: still is readable

.. automodule:: telegram.ext.filters
:inherited-members:
:members:
:show-inheritance:
:member-order: bysource
10 changes: 5 additions & 5 deletions examples/conversationbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
ConversationHandler,
Updater,
CallbackContext,
Expand Down Expand Up @@ -146,13 +146,13 @@ def main() -> None:
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
GENDER: [MessageHandler(Filters.regex('^(Boy|Girl|Other)$'), gender)],
PHOTO: [MessageHandler(Filters.photo, photo), CommandHandler('skip', skip_photo)],
GENDER: [MessageHandler(filters.Regex('^(Boy|Girl|Other)$'), gender)],
PHOTO: [MessageHandler(filters.PHOTO, photo), CommandHandler('skip', skip_photo)],
LOCATION: [
MessageHandler(Filters.location, location),
MessageHandler(filters.LOCATION, location),
CommandHandler('skip', skip_location),
],
BIO: [MessageHandler(Filters.text & ~Filters.command, bio)],
BIO: [MessageHandler(filters.TEXT & ~filters.COMMAND, bio)],
},
fallbacks=[CommandHandler('cancel', cancel)],
)
Expand Down
12 changes: 6 additions & 6 deletions examples/conversationbot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
ConversationHandler,
Updater,
CallbackContext,
Expand Down Expand Up @@ -126,23 +126,23 @@ def main() -> None:
states={
CHOOSING: [
MessageHandler(
Filters.regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice
filters.Regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice
),
MessageHandler(Filters.regex('^Something else...$'), custom_choice),
MessageHandler(filters.Regex('^Something else...$'), custom_choice),
],
TYPING_CHOICE: [
MessageHandler(
Filters.text & ~(Filters.command | Filters.regex('^Done$')), regular_choice
filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), regular_choice
)
],
TYPING_REPLY: [
MessageHandler(
Filters.text & ~(Filters.command | Filters.regex('^Done$')),
filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')),
received_information,
)
],
},
fallbacks=[MessageHandler(Filters.regex('^Done$'), done)],
fallbacks=[MessageHandler(filters.Regex('^Done$'), done)],
)

dispatcher.add_handler(conv_handler)
Expand Down
10 changes: 5 additions & 5 deletions examples/deeplinking.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from telegram.ext import (
CommandHandler,
CallbackQueryHandler,
Filters,
filters,
Updater,
CallbackContext,
)
Expand Down Expand Up @@ -115,20 +115,20 @@ def main() -> None:

# Register a deep-linking handler
dispatcher.add_handler(
CommandHandler("start", deep_linked_level_1, Filters.regex(CHECK_THIS_OUT))
CommandHandler("start", deep_linked_level_1, filters.Regex(CHECK_THIS_OUT))
)

# This one works with a textual link instead of an URL
dispatcher.add_handler(CommandHandler("start", deep_linked_level_2, Filters.regex(SO_COOL)))
dispatcher.add_handler(CommandHandler("start", deep_linked_level_2, filters.Regex(SO_COOL)))

# We can also pass on the deep-linking payload
dispatcher.add_handler(
CommandHandler("start", deep_linked_level_3, Filters.regex(USING_ENTITIES))
CommandHandler("start", deep_linked_level_3, filters.Regex(USING_ENTITIES))
)

# Possible with inline keyboard buttons as well
dispatcher.add_handler(
CommandHandler("start", deep_linked_level_4, Filters.regex(USING_KEYBOARD))
CommandHandler("start", deep_linked_level_4, filters.Regex(USING_KEYBOARD))
)

# register callback handler for inline keyboard button
Expand Down
4 changes: 2 additions & 2 deletions examples/echobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
Updater,
CallbackContext,
)
Expand Down Expand Up @@ -68,7 +68,7 @@ def main() -> None:
dispatcher.add_handler(CommandHandler("help", help_command))

# on non command i.e message - echo the message on Telegram
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
dispatcher.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))

# Start the Bot
updater.start_polling()
Expand Down
4 changes: 2 additions & 2 deletions examples/nestedconversationbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
ConversationHandler,
CallbackQueryHandler,
Updater,
Expand Down Expand Up @@ -319,7 +319,7 @@ def main() -> None:
SELECTING_FEATURE: [
CallbackQueryHandler(ask_for_input, pattern='^(?!' + str(END) + ').*$')
],
TYPING: [MessageHandler(Filters.text & ~Filters.command, save_input)],
TYPING: [MessageHandler(filters.TEXT & ~filters.COMMAND, save_input)],
},
fallbacks=[
CallbackQueryHandler(end_describing, pattern='^' + str(END) + '$'),
Expand Down
4 changes: 2 additions & 2 deletions examples/passportbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pathlib import Path

from telegram import Update
from telegram.ext import MessageHandler, Filters, Updater, CallbackContext
from telegram.ext import MessageHandler, filters, Updater, CallbackContext

# Enable logging

Expand Down Expand Up @@ -110,7 +110,7 @@ def main() -> None:
dispatcher = updater.dispatcher

# On messages that include passport data call msg
dispatcher.add_handler(MessageHandler(Filters.passport_data, msg))
dispatcher.add_handler(MessageHandler(filters.PASSPORT_DATA, msg))

# Start the Bot
updater.start_polling()
Expand Down
4 changes: 2 additions & 2 deletions examples/paymentbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
PreCheckoutQueryHandler,
ShippingQueryHandler,
Updater,
Expand Down Expand Up @@ -149,7 +149,7 @@ def main() -> None:
dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_callback))

# Success! Notify your user!
dispatcher.add_handler(MessageHandler(Filters.successful_payment, successful_payment_callback))
dispatcher.add_handler(MessageHandler(filters.SUCCESSFUL_PAYMENT, successful_payment_callback))

# Start the Bot
updater.start_polling()
Expand Down
12 changes: 6 additions & 6 deletions examples/persistentconversationbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
Filters,
filters,
ConversationHandler,
PicklePersistence,
Updater,
Expand Down Expand Up @@ -144,23 +144,23 @@ def main() -> None:
states={
CHOOSING: [
MessageHandler(
Filters.regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice
filters.Regex('^(Age|Favourite colour|Number of siblings)$'), regular_choice
),
MessageHandler(Filters.regex('^Something else...$'), custom_choice),
MessageHandler(filters.Regex('^Something else...$'), custom_choice),
],
TYPING_CHOICE: [
MessageHandler(
Filters.text & ~(Filters.command | Filters.regex('^Done$')), regular_choice
filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')), regular_choice
)
],
TYPING_REPLY: [
MessageHandler(
Filters.text & ~(Filters.command | Filters.regex('^Done$')),
filters.TEXT & ~(filters.COMMAND | filters.Regex('^Done$')),
received_information,
)
],
},
fallbacks=[MessageHandler(Filters.regex('^Done$'), done)],
fallbacks=[MessageHandler(filters.Regex('^Done$'), done)],
name="my_conversation",
persistent=True,
)
Expand Down
4 changes: 2 additions & 2 deletions examples/pollbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
PollAnswerHandler,
PollHandler,
MessageHandler,
Filters,
filters,
Updater,
CallbackContext,
)
Expand Down Expand Up @@ -163,7 +163,7 @@ def main() -> None:
dispatcher.add_handler(CommandHandler('quiz', quiz))
dispatcher.add_handler(PollHandler(receive_quiz_answer))
dispatcher.add_handler(CommandHandler('preview', preview))
dispatcher.add_handler(MessageHandler(Filters.poll, receive_poll))
dispatcher.add_handler(MessageHandler(filters.POLL, receive_poll))
dispatcher.add_handler(CommandHandler('help', help_handler))

# Start the Bot
Expand Down
7 changes: 2 additions & 5 deletions telegram/ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ._callbackqueryhandler import CallbackQueryHandler
from ._choseninlineresulthandler import ChosenInlineResultHandler
from ._inlinequeryhandler import InlineQueryHandler
from .filters import BaseFilter, MessageFilter, UpdateFilter, Filters
from . import filters
from ._messagehandler import MessageHandler
from ._commandhandler import CommandHandler, PrefixHandler
from ._stringcommandhandler import StringCommandHandler
Expand All @@ -49,7 +49,6 @@
from ._builders import DispatcherBuilder, UpdaterBuilder

__all__ = (
'BaseFilter',
'BasePersistence',
'CallbackContext',
'CallbackDataCache',
Expand All @@ -66,13 +65,12 @@
'DispatcherBuilder',
'DispatcherHandlerStop',
'ExtBot',
'Filters',
'filters',
'Handler',
'InlineQueryHandler',
'InvalidCallbackData',
'Job',
'JobQueue',
'MessageFilter',
'MessageHandler',
'PersistenceInput',
'PicklePersistence',
Expand All @@ -84,7 +82,6 @@
'StringCommandHandler',
'StringRegexHandler',
'TypeHandler',
'UpdateFilter',
'Updater',
'UpdaterBuilder',
)
2 changes: 1 addition & 1 deletion telegram/ext/_basepersistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from telegram.ext._utils.types import UD, CD, BD, ConversationDict, CDCData


class PersistenceInput(NamedTuple):
class PersistenceInput(NamedTuple): # skipcq: PYL-E0239
"""Convenience wrapper to group boolean input for :class:`BasePersistence`.

Args:
Expand Down
7 changes: 3 additions & 4 deletions telegram/ext/_callbackcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ class CallbackContext(Generic[BT, UD, CD, BD]):

Attributes:
matches (List[:obj:`re match object`]): Optional. If the associated update originated from
a regex-supported handler or had a :class:`Filters.regex`, this will contain a list of
match objects for every pattern where ``re.search(pattern, string)`` returned a match.
Note that filters short circuit, so combined regex filters will not always
be evaluated.
a :class:`filters.Regex`, this will contain a list of match objects for every pattern
where ``re.search(pattern, string)`` returned a match. Note that filters short circuit,
so combined regex filters will not always be evaluated.
args (List[:obj:`str`]): Optional. Arguments passed to a command if the associated update
is handled by :class:`telegram.ext.CommandHandler`, :class:`telegram.ext.PrefixHandler`
or :class:`telegram.ext.StringCommandHandler`. It contains a list of the words in the
Expand Down
Loading