Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Abshar <https://github.com/abxhr>`_
- `Alateas <https://github.com/alateas>`_
- `Ales Dokshanin <https://github.com/alesdokshanin>`_
- `Alizia <https://github.com/thefunkycat>`_
- `Ambro17 <https://github.com/Ambro17>`_
- `Andrej Zhilenkov <https://github.com/Andrej730>`_
- `Anton Tagunov <https://github.com/anton-tagunov>`_
Expand Down
3 changes: 1 addition & 2 deletions docs/auxil/admonition_inserter.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def _create_available_in(self) -> dict[type, str]:
break

for line in lines_with_attrs:
line_match = attr_docstr_pattern.match(line)
if not line_match:
if not (line_match := attr_docstr_pattern.match(line)):
continue

target_attr = line_match.group("attr_name")
Expand Down
6 changes: 4 additions & 2 deletions docs/auxil/sphinx_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ def autodoc_process_bases(app, name, obj, option, bases: list):
bases[idx] = f":class:`{base}`"

# Now convert `telegram._message.Message` to `telegram.Message` etc
match = re.search(pattern=r"(telegram(\.ext|))\.[_\w\.]+", string=base)
if not match or "_utils" in base:
if (
not (match := re.search(pattern=r"(telegram(\.ext|))\.[_\w\.]+", string=base))
or "_utils" in base
):
continue

parts = match.group(0).split(".")
Expand Down
3 changes: 1 addition & 2 deletions telegram/ext/_callbackqueryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ def check_update(self, update: object) -> Optional[Union[bool, object]]:
return self.pattern(callback_data)
if not isinstance(callback_data, str):
return False
match = re.match(self.pattern, callback_data)
if match:
if match := re.match(self.pattern, callback_data):
return match
else:
return True
Expand Down
3 changes: 1 addition & 2 deletions telegram/ext/_choseninlineresulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def check_update(self, update: object) -> Optional[Union[bool, object]]:
"""
if isinstance(update, Update) and update.chosen_inline_result:
if self.pattern:
match = re.match(self.pattern, update.chosen_inline_result.result_id)
if match:
if match := re.match(self.pattern, update.chosen_inline_result.result_id):
return match
else:
return True
Expand Down
13 changes: 7 additions & 6 deletions telegram/ext/_inlinequeryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ def check_update(self, update: object) -> Optional[Union[bool, Match[str]]]:
update.inline_query.chat_type not in self.chat_types
):
return False
if self.pattern:
if update.inline_query.query:
match = re.match(self.pattern, update.inline_query.query)
if match:
return match
else:
if (
self.pattern
and update.inline_query.query
and (match := re.match(self.pattern, update.inline_query.query))
):
return match
if not self.pattern:
return True
return None

Expand Down
6 changes: 2 additions & 4 deletions telegram/ext/_stringregexhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ def check_update(self, update: object) -> Optional[Match[str]]:
:obj:`None` | :obj:`re.match`

"""
if isinstance(update, str):
match = re.match(self.pattern, update)
if match:
return match
if isinstance(update, str) and (match := re.match(self.pattern, update)):
return match
return None

def collect_additional_context(
Expand Down
20 changes: 8 additions & 12 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,8 @@ def __init__(self, pattern: Union[str, Pattern[str]]):
super().__init__(name=f"filters.CaptionRegex({self.pattern})", data_filter=True)

def filter(self, message: Message) -> Optional[Dict[str, List[Match[str]]]]:
if message.caption:
match = self.pattern.search(message.caption)
if match:
return {"matches": [match]}
if message.caption and (match := self.pattern.search(message.caption)):
return {"matches": [match]}
return {}


Expand Down Expand Up @@ -974,15 +972,15 @@ def __init__(self, values: Optional[SCT[int]] = None, emoji: Optional[DiceEmojiE
self.name = "filters.Dice.ALL"

def filter(self, message: Message) -> bool:
if not message.dice: # no dice
if not (dice := message.dice): # no dice
return False

if self.emoji:
emoji_match = message.dice.emoji == self.emoji
emoji_match = dice.emoji == self.emoji
if self.values:
return message.dice.value in self.values and emoji_match # emoji and value
return dice.value in self.values and emoji_match # emoji and value
return emoji_match # emoji, no value
return message.dice.value in self.values if self.values else True # no emoji, only value
return dice.value in self.values if self.values else True # no emoji, only value


class Dice(_Dice):
Expand Down Expand Up @@ -1597,10 +1595,8 @@ def __init__(self, pattern: Union[str, Pattern[str]]):
super().__init__(name=f"filters.Regex({self.pattern})", data_filter=True)

def filter(self, message: Message) -> Optional[Dict[str, List[Match[str]]]]:
if message.text:
match = self.pattern.search(message.text)
if match:
return {"matches": [match]}
if message.text and (match := self.pattern.search(message.text)):
return {"matches": [match]}
return {}


Expand Down
3 changes: 1 addition & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ def build_test_message(kwargs):
assert helpers.effective_message_type(empty_update) is None

def test_effective_message_type_wrong_type(self):
entity = {}
with pytest.raises(
TypeError, match=re.escape(f"neither Message nor Update (got: {type(entity)})")
TypeError, match=re.escape(f"neither Message nor Update (got: {type(entity := {})})")
):
helpers.effective_message_type(entity)

Expand Down