diff --git a/.gitignore b/.gitignore index 9e944f669..2bd243a74 100644 --- a/.gitignore +++ b/.gitignore @@ -93,6 +93,8 @@ telegram.jpg # virtual env venv* +pyvenv.cfg +Scripts/ # environment manager: .mise.toml \ No newline at end of file diff --git a/AUTHORS.rst b/AUTHORS.rst index 615353979..d5e7b1a0c 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -80,6 +80,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Kirill Vasin `_ - `Kjwon15 `_ - `Li-aung Yip `_ +- `locobott `_ - `Loo Zheng Yuan `_ - `LRezende `_ - `Luca Bellanti `_ diff --git a/changes/unreleased/4817.ZHx38jvj9zKRNZfQh9Xrpb.toml b/changes/unreleased/4817.ZHx38jvj9zKRNZfQh9Xrpb.toml new file mode 100644 index 000000000..31e63a6a7 --- /dev/null +++ b/changes/unreleased/4817.ZHx38jvj9zKRNZfQh9Xrpb.toml @@ -0,0 +1,5 @@ +bugfixes = "Allow for pattern matching empty inline queries" +[[pull_requests]] +uid = "4817" +author_uid = "locobott" +closes_threads = [] \ No newline at end of file diff --git a/src/telegram/ext/_handlers/inlinequeryhandler.py b/src/telegram/ext/_handlers/inlinequeryhandler.py index 0285d259c..3e7b7c4d4 100644 --- a/src/telegram/ext/_handlers/inlinequeryhandler.py +++ b/src/telegram/ext/_handlers/inlinequeryhandler.py @@ -118,11 +118,7 @@ class InlineQueryHandler(BaseHandler[Update, CCT, RT]): update.inline_query.chat_type not in self.chat_types ): return False - if ( - self.pattern - and update.inline_query.query - and (match := re.match(self.pattern, update.inline_query.query)) - ): + if self.pattern and (match := re.match(self.pattern, update.inline_query.query)): return match if not self.pattern: return True diff --git a/tests/ext/test_inlinequeryhandler.py b/tests/ext/test_inlinequeryhandler.py index 24aa69f01..cd20e1aec 100644 --- a/tests/ext/test_inlinequeryhandler.py +++ b/tests/ext/test_inlinequeryhandler.py @@ -152,6 +152,28 @@ class TestInlineQueryHandler: update.inline_query.query = "not_a_match" assert not handler.check_update(update) + @pytest.mark.parametrize( + ("query", "expected_result"), + [ + pytest.param("", True, id="empty string"), + pytest.param("not empty", False, id="non_empty_string"), + ], + ) + async def test_empty_inline_query_pattern(self, app, query, expected_result): + handler = InlineQueryHandler(self.callback, pattern=r"^$") + app.add_handler(handler) + + update = Update( + update_id=0, + inline_query=InlineQuery( + id="id", from_user=User(1, "test", False), query=query, offset="" + ), + ) + + async with app: + await app.process_update(update) + assert self.test_flag == expected_result + @pytest.mark.parametrize("chat_types", [[Chat.SENDER], [Chat.SENDER, Chat.SUPERGROUP], []]) @pytest.mark.parametrize( ("chat_type", "result"), [(Chat.SENDER, True), (Chat.CHANNEL, False), (None, False)]