From 86d45cc27675df5d43928961bb1fb1e44e6db486 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 15 Jun 2017 17:03:59 +0200 Subject: [PATCH] Improve .get_input_peer and use it only when creating requests* This avoids cyclic dependencies, so types requiring an InputPeer as a parameter will NOT convert faulty types to the right ones. --- telethon/utils.py | 28 ++++++++++++++++++++-------- telethon_generator/tl_generator.py | 5 +++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/telethon/utils.py b/telethon/utils.py index 7d11ef17..9f22794b 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -5,9 +5,10 @@ to convert between an entity like an User, Chat, etc. into its Input version) from mimetypes import add_type, guess_extension from .tl.types import ( - Channel, Chat, ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, - MessageMediaDocument, MessageMediaPhoto, PeerChannel, PeerChat, PeerUser, - User, UserProfilePhoto) + Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull, + ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty, + InputPeerSelf, MessageMediaDocument, MessageMediaPhoto, PeerChannel, + PeerChat, PeerUser, User, UserFull, UserProfilePhoto) def get_display_name(entity): @@ -46,18 +47,29 @@ def get_extension(media): def get_input_peer(entity): """Gets the input peer for the given "entity" (user, chat or channel). A ValueError is raised if the given entity isn't a supported type.""" - if (isinstance(entity, InputPeerUser) or - isinstance(entity, InputPeerChat) or - isinstance(entity, InputPeerChannel)): + if any(isinstance(entity, c) for c in ( + InputPeerUser, InputPeerChat, InputPeerChannel, + InputPeerSelf, InputPeerEmpty)): return entity if isinstance(entity, User): return InputPeerUser(entity.id, entity.access_hash) - if isinstance(entity, Chat): + + if any(isinstance(entity, c) for c in ( + Chat, ChatEmpty, ChatForbidden)): return InputPeerChat(entity.id) - if isinstance(entity, Channel): + + if any(isinstance(entity, c) for c in ( + Channel, ChannelForbidden)): return InputPeerChannel(entity.id, entity.access_hash) + # Less common cases + if isinstance(entity, UserFull): + return InputPeerUser(entity.user.id, entity.user.access_hash) + + if isinstance(entity, ChatFull): + return InputPeerChat(entity.id) + raise ValueError('Cannot cast {} to any kind of InputPeer.' .format(type(entity).__name__)) diff --git a/telethon_generator/tl_generator.py b/telethon_generator/tl_generator.py index cffb0f75..042c29dc 100755 --- a/telethon_generator/tl_generator.py +++ b/telethon_generator/tl_generator.py @@ -186,7 +186,8 @@ class TLGenerator: builder.writeln('from {}.tl.mtproto_request import MTProtoRequest' .format('.' * depth)) - if any(a for a in tlobject.args if a.type == 'InputPeer'): + if tlobject.is_function and \ + any(a for a in tlobject.args if a.type == 'InputPeer'): # We can automatically convert a normal peer to an InputPeer, # it will make invoking a lot of requests a lot simpler. builder.writeln('from {}.utils import get_input_peer' @@ -312,7 +313,7 @@ class TLGenerator: ) else: raise ValueError('Cannot infer a value for ', arg) - elif arg.type == 'InputPeer': + elif arg.type == 'InputPeer' and tlobject.is_function: # Well-known case, auto-cast it to the right type builder.writeln( 'self.{0} = get_input_peer({0})'.format(arg.name))