Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Addname andfull_name properties to theSharedUser class#4708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Show file tree
Hide file tree
Changes fromall 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
22 changes: 21 additions & 1 deletiontelegram/_shared.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains two objects used for request chats/users service messages."""
from collections.abc import Sequence
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Union

from telegram._files.photosize import PhotoSize
from telegram._telegramobject import TelegramObject
Expand DownExpand Up@@ -244,6 +244,26 @@ def __init__(

self._freeze()

@property
def name(self) -> Union[str, None]:
""":obj:`str`: Convenience property. If available, returns the user's :attr:`username`
prefixed with "@". If :attr:`username` is not available, returns :attr:`full_name`.
"""
if self.username:
return f"@{self.username}"
return self.full_name

@property
def full_name(self) -> Union[str, None]:
""":obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if
available) :attr:`last_name`, otherwise None.
"""
if self.first_name and self.last_name:
return f"{self.first_name} {self.last_name}"
if self.first_name or self.last_name:
return f"{self.first_name or self.last_name}"
return None

@classmethod
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SharedUser":
"""See :meth:`telegram.TelegramObject.de_json`."""
Expand Down
31 changes: 31 additions & 0 deletionstests/test_shared.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].

from unittest.mock import patch

import pytest

from telegram import ChatShared, PhotoSize, SharedUser, UsersShared
Expand DownExpand Up@@ -228,3 +230,32 @@ def test_equality(self, chat_shared):

assert a != d
assert hash(a) != hash(d)

def test_name(self, shared_user, ):
with shared_user._unfrozen():
assert shared_user.name == "@user"
with patch.object(shared_user, 'username', None):
assert shared_user.name == "first last"
with patch.multiple(
shared_user,
last_name=None,
first_name=None,
):
assert shared_user.full_name is None

def test_full_name(self, shared_user):
with shared_user._unfrozen():
# Test `and` (both exists)
assert shared_user.full_name == "first last"
# Test `or` (one of them exists)
with patch.object(shared_user, 'first_name', None):
assert shared_user.full_name == "last"
with patch.object(shared_user, 'last_name', None):
assert shared_user.full_name == "first"
# Test None (None of them exists)
with patch.multiple(
shared_user,
last_name=None,
first_name=None,
):
assert shared_user.full_name is None
Loading

[8]ページ先頭

©2009-2025 Movatter.jp