Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

david-shiko
Copy link
Contributor

@david-shiko david-shiko commented Mar 10, 2025

This is a basic implementation of the changes suggested in the pull request 4708 .

Please note that there are several issues with the code:

  1. I'm not sure if I have used overloads correctly, as warnings are still present.
  2. There is a failed slots test for the class.
  3. Is it necessary to test the properties of the User class, or utils.usernames is sufficient to test ?

…sts for it; Replace `user` and `shared_user` correspond properties implementations on a new functinos.
…rnames.py` implementation; Note: `full_name` and `effective_name` can not be easily replaced in the such way
…lved; Slots tests failed for Protocol class.
Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! I had just a very quick look so far

  1. I'm not sure if I have used overloads correctly, as warnings are still present.

I don't see anything wrong at first glance, but haven't seen the mypy warnings (pre-commit truncated the output). Looking up other code, have a look at

Tele_co = TypeVar("Tele_co", bound=TelegramObject, covariant=True)
TeleCrypto_co = TypeVar("TeleCrypto_co", bound="HasDecryptMethod", covariant=True)
if TYPE_CHECKING:
@type_check_only
class HasDecryptMethod(Protocol):
__slots__ = ()
@classmethod
def de_json_decrypted(
cls: type[TeleCrypto_co],
data: JSONDict,
bot: Optional["Bot"],
credentials: list["FileCredentials"],
) -> TeleCrypto_co: ...
@classmethod
def de_list_decrypted(
cls: type[TeleCrypto_co],
data: list[JSONDict],
bot: Optional["Bot"],
credentials: list["FileCredentials"],
) -> tuple[TeleCrypto_co, ...]: ...

for how Protocol and typevar are used there. maybe that helps.

  1. There is a failed slots test for the class.

in the above code section the if TYPE_CHECKING ensures that the class is not defined at test runtime. this would get the test passing.

  1. Is it necessary to test the properties of the User class, or utils.usernames is sufficient to test ?

I'd say testing the properties of the User(/Chat/SharedUser) class is enough and explicitly testing _utils.usernames is not required :)

Please also install the pre-commit hooks

# 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
Copy link
Member

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 use Union and Optional instead

unlike the `Chat` and `Shared`, were they are optional.
The `last_name` is always optional.
"""
last_name: str | None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
last_name: str | None
first_name: Optional[str]
last_name: str | None

that way you should be able to remove MiniUserLike

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. One of the hooks, probably black or isort, converts automatically during commit.
  2. Sometimes, the field always exists (for example, for the User type) and sometimes it might not (like in the SharedUser type). This affects the return type: it can be Optional[str] or guaranteed to be an str . That's why I introduced it.
  3. I know the name MiniUserLike is pretty silly, but I couldn't think of anything better. I hope you can help me with that :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • running the pre-commit hooks on python 3.9 (lowest supported version) should make sure that this doesn't happen. Though I admit I'm not completely sure why it does in the first place
  • you already differentiate between UserLikeOptional and UserLike. That should already do the trick IMO

@Bibo-Joshi
Copy link
Member

@david-shiko are you still interested in finishing this up?

@david-shiko
Copy link
Contributor Author

@david-shiko are you still interested in finishing this up?

Hi, yes, I'm interested, but I don't have any free time and I don't know when it will be, you can close it, and if anything, I'll make a new PR or open this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants