mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
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.
This commit is contained in:
parent
b8e46446ba
commit
86d45cc276
|
@ -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__))
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue
Block a user