Add Chat.effective_name Convenience Property (#3485)

This commit is contained in:
Viicos 2023-01-06 08:15:56 +01:00 committed by GitHub
parent f9cbddd10a
commit d48fa71ec5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View file

@ -388,10 +388,24 @@ class Chat(TelegramObject):
self._freeze() self._freeze()
@property
def effective_name(self) -> Optional[str]:
"""
:obj:`str`: Convenience property. Gives :attr:`title` if not :obj:`None`,
else :attr:`full_name` if not :obj:`None`.
.. versionadded:: 20.1
"""
if self.title is not None:
return self.title
if self.full_name is not None:
return self.full_name
return None
@property @property
def full_name(self) -> Optional[str]: def full_name(self) -> Optional[str]:
""" """
:obj:`str`: Convenience property. If :attr:`first_name` is not :obj:`None` gives, :obj:`str`: Convenience property. If :attr:`first_name` is not :obj:`None`, gives
:attr:`first_name` followed by (if available) :attr:`last_name`. :attr:`first_name` followed by (if available) :attr:`last_name`.
Note: Note:

View file

@ -212,6 +212,16 @@ class TestChat:
) )
assert chat.full_name is None assert chat.full_name is None
def test_effective_name(self):
chat = Chat(id=1, type=Chat.PRIVATE, first_name="first\u2022name")
assert chat.effective_name == "first\u2022name"
chat = Chat(id=1, type=Chat.GROUP, title="group")
assert chat.effective_name == "group"
chat = Chat(id=1, type=Chat.GROUP, first_name="first\u2022name", title="group")
assert chat.effective_name == "group"
chat = Chat(id=1, type=Chat.GROUP)
assert chat.effective_name is None
async def test_send_action(self, monkeypatch, chat): async def test_send_action(self, monkeypatch, chat):
async def make_assertion(*_, **kwargs): async def make_assertion(*_, **kwargs):
id_ = kwargs["chat_id"] == chat.id id_ = kwargs["chat_id"] == chat.id