Skip to content
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

TypeError raised when casting InputPeerSelf to any kind of int in utils.get_peer_id #764

Closed
schemacs opened this issue Apr 14, 2018 · 7 comments

Comments

@schemacs
Copy link

schemacs commented Apr 14, 2018

I am using the latest asyncio branch 06af73e

  File "/home/ubuntu/.local/lib/python3.6/site-packages/telethon/telegram_client.py", line 2385, in get_entity
    for x in inputs
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telethon/telegram_client.py", line 2385, in <listcomp>
    for x in inputs
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telethon/utils.py", line 462, in get_peer_id
    _raise_cast_fail(peer, 'int')
  File "/home/ubuntu/.local/lib/python3.6/site-packages/telethon/utils.py", line 82, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast InputPeerSelf to any kind of int.

This line of doc says get_input_entity will return InputPeerUser, InputPeerChat, or InputPeerChannel, but the actual code return InputPeerSelf, so this line will raise TypeError when calling utils.get_peer_id with InputPeerSelf.

Should it be changed to return await self.get_me(input_peer=True)?

@Lonami
Copy link
Member

Lonami commented Apr 14, 2018

I think I just need to merge master.

@schemacs
Copy link
Author

schemacs commented Apr 14, 2018

Thanks a lot, and should the check if peer in ('me', 'self'): in get_input_entity also changed to if peer in ('me', 'self') or isinstance(peer, InputPeerSelf):?

@jonbesga
Copy link
Contributor

jonbesga commented Apr 14, 2018

Came here to post exactly this issue!

In my case the problem is happening after calling the method download_profile_photo to download the profile image of the dialog Saved Messages (i.e. myself) :

  File "/home/jon/Projects/telegramstats/core/views.py", line 107, in dashboard
    x = get_profile_photo(client, dialog.entity)
  File "/home/jon/Projects/telegramstats/core/stats.py", line 74, in get_profile_photo
    return client.download_profile_photo(entity, file=os.path.join(STATICFILES_DIRS[0], f'{entity.id}.jpg'))
  File "/home/jon/.local/share/virtualenvs/telegramstats-TK1JrBmB/lib/python3.6/site-packages/telethon/telegram_client.py", line 1787, in download_profile_photo
    entity = self.get_entity(entity)
  File "/home/jon/.local/share/virtualenvs/telegramstats-TK1JrBmB/lib/python3.6/site-packages/telethon/telegram_client.py", line 2362, in get_entity
    for x in inputs
  File "/home/jon/.local/share/virtualenvs/telegramstats-TK1JrBmB/lib/python3.6/site-packages/telethon/telegram_client.py", line 2362, in <listcomp>
    for x in inputs
  File "/home/jon/.local/share/virtualenvs/telegramstats-TK1JrBmB/lib/python3.6/site-packages/telethon/utils.py", line 462, in get_peer_id
    _raise_cast_fail(peer, 'int')
  File "/home/jon/.local/share/virtualenvs/telegramstats-TK1JrBmB/lib/python3.6/site-packages/telethon/utils.py", line 82, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast InputPeerSelf to any kind of int.

@schemacs
Copy link
Author

schemacs commented Apr 14, 2018

@jonbesga You could try this:

diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py
index 0592621..06f5180 100644
--- a/telethon/telegram_client.py
+++ b/telethon/telegram_client.py
@@ -2487,8 +2487,8 @@ class TelegramClient(TelegramBareClient):
         Returns:
             :tl:`InputPeerUser`, :tl:`InputPeerChat` or :tl:`InputPeerChannel`.
         """
-        if peer in ('me', 'self'):
-            return InputPeerSelf()
+        if peer in ('me', 'self') or isinstance(peer, InputPeerSelf):
+            return self.get_me(input_peer=True)

         try:
             # First try to get the entity from cache, otherwise figure it out

@Lonami
Copy link
Member

Lonami commented Apr 15, 2018

Considering this fixed after b7ae612. If it's not please let me know and we can reopen it.

@Lonami Lonami closed this as completed Apr 15, 2018
@Lonami
Copy link
Member

Lonami commented Apr 15, 2018

Ohh. Now I read the original issue more carefully (sorry!). Indeed that documentation line is wrong. .get_input_entity is intended when you're going to use the input version of something, not to just get the ID. If you just want the ID of yourself you should use client.get_me(input_peer=True).user_id instead. It should not be changed though because the method is fine, it returns an InputPeer (which might not have the ID).

Lonami added a commit that referenced this issue Apr 15, 2018
@schemacs
Copy link
Author

schemacs commented Apr 16, 2018

That sounds reasonable. Thanks, and you have also excluded get_peer_id from calling with InputPeerSelf in 1316e07 .

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

No branches or pull requests

3 participants