From 02357faf03c4d4b362d3283a428fc51a002c7dc1 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Tue, 27 Sep 2022 23:35:36 +0300 Subject: [PATCH] Fixed #1013: Empty mention should be None instead of empty string. --- CHANGES/1013.bugfix.rst | 1 + aiogram/filters/command.py | 5 ++++- tests/test_filters/test_command.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1013.bugfix.rst diff --git a/CHANGES/1013.bugfix.rst b/CHANGES/1013.bugfix.rst new file mode 100644 index 00000000..3b8e1645 --- /dev/null +++ b/CHANGES/1013.bugfix.rst @@ -0,0 +1 @@ +Fixed empty mention in command parsing, now it will be None instead of an empty string diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index b46ca80d..4d8a0143 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -132,7 +132,10 @@ class Command(Filter): # "/command@mention" -> "/", ("command", "@", "mention") prefix, (command, _, mention) = full_command[0], full_command[1:].partition("@") return CommandObject( - prefix=prefix, command=command, mention=mention, args=args[0] if args else None + prefix=prefix, + command=command, + mention=mention or None, + args=args[0] if args else None, ) def validate_prefix(self, command: CommandObject) -> None: diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index e1c296ed..1cdd0b70 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -104,6 +104,23 @@ class TestCommandFilter: assert "args" in result assert result["args"] == "42" + async def test_empty_mention_is_none(self, bot: MockedBot): + # Fixed https://github.com/aiogram/aiogram/issues/1013: + # Empty mention should be None instead of empty string. + + message = Message( + message_id=0, + text="/test", + chat=Chat(id=42, type="private"), + date=datetime.datetime.now(), + ) + command = Command("test") + result = await command(message=message, bot=bot) + + assert "command" in result + command_obj: CommandObject = result["command"] + assert command_obj.mention is None + class TestCommandObject: @pytest.mark.parametrize(