Changed msg.<html/md>_text attributes behaviour

This commit is contained in:
Alex Root Junior 2022-04-11 18:33:35 +03:00
parent bc20999429
commit fa9abfdbef
No known key found for this signature in database
GPG key ID: 074C1D455EBEA4AC
4 changed files with 12 additions and 17 deletions

2
CHANGES/874.misc.rst Normal file
View file

@ -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.

1
CHANGES/885.bugfix.rst Normal file
View file

@ -0,0 +1 @@
Fixed CallbackData factory parsing IntEnum's

View file

@ -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

View file

@ -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", "<b>test</b>"],
["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