From fa9abfdbefecfac8469c8358dc5292cb208a9cde Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 11 Apr 2022 18:33:35 +0300 Subject: [PATCH] Changed msg._text attributes behaviour --- CHANGES/874.misc.rst | 2 ++ CHANGES/885.bugfix.rst | 1 + aiogram/types/message.py | 7 ++----- tests/test_api/test_types/test_message.py | 19 +++++++------------ 4 files changed, 12 insertions(+), 17 deletions(-) create mode 100644 CHANGES/874.misc.rst create mode 100644 CHANGES/885.bugfix.rst diff --git a/CHANGES/874.misc.rst b/CHANGES/874.misc.rst new file mode 100644 index 00000000..d167c1d8 --- /dev/null +++ b/CHANGES/874.misc.rst @@ -0,0 +1,2 @@ +Changed :code:`Message.html_text` and :code:`Message.md_text` attributes behaviour when message has no text. +The empty string will be used instead of raising error. diff --git a/CHANGES/885.bugfix.rst b/CHANGES/885.bugfix.rst new file mode 100644 index 00000000..6d1bb095 --- /dev/null +++ b/CHANGES/885.bugfix.rst @@ -0,0 +1 @@ +Fixed CallbackData factory parsing IntEnum's diff --git a/aiogram/types/message.py b/aiogram/types/message.py index e8139696..08d987d0 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -267,11 +267,8 @@ class Message(_BaseMessage): return ContentType.UNKNOWN def _unparse_entities(self, text_decoration: TextDecoration) -> str: - text = self.text or self.caption - if text is None: - raise TypeError("This message doesn't have any text.") - - entities = self.entities or self.caption_entities + text = self.text or self.caption or "" + entities = self.entities or self.caption_entities or [] return text_decoration.unparse(text=text, entities=entities) @property diff --git a/tests/test_api/test_types/test_message.py b/tests/test_api/test_types/test_message.py index 93341eb2..b2b66b77 100644 --- a/tests/test_api/test_types/test_message.py +++ b/tests/test_api/test_types/test_message.py @@ -641,13 +641,15 @@ class TestMessage: assert method.message_id == message.message_id @pytest.mark.parametrize( - "text,entities,correct", + "text,entities,mode,expected_value", [ - ["test", [MessageEntity(type="bold", offset=0, length=4)], True], - ["", [], False], + ["test", [MessageEntity(type="bold", offset=0, length=4)], "html", "test"], + ["test", [MessageEntity(type="bold", offset=0, length=4)], "md", "*test*"], + ["", [], "html", ""], + ["", [], "md", ""], ], ) - def test_html_text(self, text, entities, correct): + def test_html_text(self, text, entities, mode, expected_value): message = Message( message_id=42, chat=Chat(id=42, type="private"), @@ -655,11 +657,4 @@ class TestMessage: text=text, entities=entities, ) - if correct: - assert message.html_text - assert message.md_text - else: - with pytest.raises(TypeError): - assert message.html_text - with pytest.raises(TypeError): - assert message.md_text + assert getattr(message, f"{mode}_text") == expected_value