-
Notifications
You must be signed in to change notification settings - Fork 5.7k
User like properties #4713
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
User like properties #4713
Changes from all commits
2fb8ef6
91a8747
f44f018
0fc4c20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,103 @@ | ||||||||
#!/usr/bin/env python | ||||||||
# | ||||||||
# A library that provides a Python interface to the Telegram Bot API | ||||||||
# Copyright (C) 2015-2025 | ||||||||
# Leandro Toledo de Souza <[email protected]> | ||||||||
# | ||||||||
# This program is free software: you can redistribute it and/or modify | ||||||||
# it under the terms of the GNU Lesser Public License as published by | ||||||||
# the Free Software Foundation, either version 3 of the License, or | ||||||||
# (at your option) any later version. | ||||||||
# | ||||||||
# This program is distributed in the hope that it will be useful, | ||||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||
# GNU Lesser Public License for more details. | ||||||||
# | ||||||||
# You should have received a copy of the GNU Lesser Public License | ||||||||
# along with this program. If not, see [http://www.gnu.org/licenses/]. | ||||||||
"""Shared properties to extract username, first_name, last_name values if filled.""" | ||||||||
from __future__ import annotations | ||||||||
from typing import Protocol, overload | ||||||||
|
||||||||
|
||||||||
class UserLikeOptional(Protocol): | ||||||||
""" | ||||||||
Note: | ||||||||
`User`, `Contact` (and maybe some other) objects always have first_name, | ||||||||
unlike the `Chat` and `Shared`, were they are optional. | ||||||||
The `last_name` is always optional. | ||||||||
""" | ||||||||
last_name: str | None | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
that way you should be able to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
username: str | None | ||||||||
|
||||||||
|
||||||||
class UserLike(UserLikeOptional): | ||||||||
""" | ||||||||
Note: | ||||||||
`User`, `Contact` (and maybe some other) objects always have first_name, | ||||||||
unlike the `Chat` and `Shared`, were they are optional. | ||||||||
The `last_name` is always optional. | ||||||||
""" | ||||||||
first_name: str | ||||||||
|
||||||||
|
||||||||
class MiniUserLike(UserLikeOptional): | ||||||||
""" | ||||||||
Note: | ||||||||
`User`, `Contact` (and maybe some other) objects always have first_name, | ||||||||
unlike the `Chat` and `Shared`, were they are optional. | ||||||||
The `last_name` is always optional. | ||||||||
""" | ||||||||
first_name: str | None | ||||||||
|
||||||||
|
||||||||
@overload | ||||||||
def get_name(user: UserLike) -> str: | ||||||||
... | ||||||||
|
||||||||
|
||||||||
@overload | ||||||||
def get_name(user: MiniUserLike) -> str | None: | ||||||||
... | ||||||||
|
||||||||
|
||||||||
def get_name(user: UserLike | MiniUserLike) -> 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`. | ||||||||
For the UserLike object str will always be returned as `first_name`always exists. | ||||||||
""" | ||||||||
if user.username: | ||||||||
return f"@{user.username}" | ||||||||
return get_full_name(user=user, ) | ||||||||
|
||||||||
|
||||||||
@overload | ||||||||
def get_full_name(user: UserLike) -> str: | ||||||||
... | ||||||||
|
||||||||
|
||||||||
@overload | ||||||||
def get_full_name(user: MiniUserLike) -> str | None: | ||||||||
... | ||||||||
|
||||||||
|
||||||||
def get_full_name(user: UserLike | MiniUserLike) -> str | None: | ||||||||
""":obj:`str`: Convenience property. The user's :attr:`first_name`, followed by (if | ||||||||
available) :attr:`last_name`, otherwise None. | ||||||||
For the UserLike object str will always be returned as `first_name`always exists. | ||||||||
""" | ||||||||
if user.first_name and user.last_name: | ||||||||
return f"{user.first_name} {user.last_name}" | ||||||||
if user.first_name or user.last_name: | ||||||||
return f"{user.first_name or user.last_name}" | ||||||||
return None | ||||||||
|
||||||||
|
||||||||
def get_link(user: UserLike | MiniUserLike) -> str | None: | ||||||||
""":obj:`str`: Convenience property. If :attr:`username` is available, returns a t.me link | ||||||||
of the user. | ||||||||
""" | ||||||||
if user.username: | ||||||||
return f"https://t.me/{user.username}" | ||||||||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
# | ||
# A library that provides a Python interface to the Telegram Bot API | ||
# Copyright (C) 2015-2025 | ||
# Leandro Toledo de Souza <[email protected]> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser Public License for more details. | ||
# | ||
# 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 __future__ import annotations | ||
import pytest | ||
from typing import TYPE_CHECKING | ||
from telegram import SharedUser | ||
from tests.test_user import user # noqa: F401 noqa: F811 | ||
from telegram._utils.usernames import get_name, get_full_name, get_link | ||
|
||
if TYPE_CHECKING: | ||
from telegram._utils.usernames import UserLike, MiniUserLike | ||
|
||
|
||
@pytest.fixture(scope="class") | ||
def shared_user(): | ||
result = SharedUser( | ||
user_id=1, | ||
first_name="first\u2022name", | ||
last_name="last\u2022name", | ||
username="username", | ||
) | ||
result._unfreeze() | ||
return result | ||
|
||
|
||
def test_get_name(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
assert get_name(user=user) == get_name(user=shared_user) == "@username" | ||
shared_user.username = user.username = None | ||
assert get_name(user=user) == get_name(user=shared_user) == "first\u2022name last\u2022name" | ||
|
||
|
||
def test_full_name_both_exists(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
expected = "first\u2022name last\u2022name" | ||
assert get_full_name(user=user) == get_full_name(user=shared_user) == expected | ||
|
||
|
||
def test_full_name_last_name_missed(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
user.last_name = shared_user.last_name = None | ||
assert get_full_name(user=user) == get_full_name(user=shared_user) == "first\u2022name" | ||
|
||
|
||
def test_full_name_first_name_missed(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
user.first_name = shared_user.first_name = None | ||
assert get_full_name(user=user) == get_full_name(user=shared_user) == "last\u2022name" | ||
|
||
|
||
def test_full_name_both_missed(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
user.first_name = user.last_name = shared_user.first_name = shared_user.last_name = None | ||
assert get_full_name(user=user) is get_full_name(user=shared_user) is None | ||
|
||
|
||
def test_link(user: UserLike, shared_user: MiniUserLike, ): # noqa: F811 | ||
assert get_link(user=user, ) == get_link(user=shared_user, ) == f"https://t.me/{user.username}" | ||
user.username = shared_user.username = None | ||
assert get_link(user=user, ) is get_link(user=shared_user, ) is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please avoid using this. If this is for using
|
, please useUnion
andOptional
instead