Replace redundant isinstance calls with a tuple parameter

This commit is contained in:
Lonami Exo 2017-10-13 11:38:12 +02:00
parent db63b5e39a
commit 4fd9d361f0
5 changed files with 17 additions and 23 deletions

View File

@ -14,7 +14,7 @@ class TcpClient:
if isinstance(timeout, timedelta): if isinstance(timeout, timedelta):
self.timeout = timeout.seconds self.timeout = timeout.seconds
elif isinstance(timeout, int) or isinstance(timeout, float): elif isinstance(timeout, (int, float)):
self.timeout = float(timeout) self.timeout = float(timeout)
else: else:
raise ValueError('Invalid timeout type', type(timeout)) raise ValueError('Invalid timeout type', type(timeout))

View File

@ -808,8 +808,9 @@ class TelegramBareClient:
try: try:
import socks import socks
if isinstance(error, socks.GeneralProxyError) or \ if isinstance(error, (
isinstance(error, socks.ProxyConnectionError): socks.GeneralProxyError, socks.ProxyConnectionError
)):
# This is a known error, and it's not related to # This is a known error, and it's not related to
# Telegram but rather to the proxy. Disconnect and # Telegram but rather to the proxy. Disconnect and
# hand it over to the main thread. # hand it over to the main thread.

View File

@ -74,9 +74,7 @@ class EntityDatabase:
getattr(p, 'access_hash', 0) # chats won't have hash getattr(p, 'access_hash', 0) # chats won't have hash
if self.enabled_full: if self.enabled_full:
if isinstance(e, User) \ if isinstance(e, (User, Chat, Channel)):
or isinstance(e, Chat) \
or isinstance(e, Channel):
new.append(e) new.append(e)
except ValueError: except ValueError:
pass pass

View File

@ -149,8 +149,7 @@ class UpdateState:
self._updates.append(update.update) self._updates.append(update.update)
self._updates_available.set() self._updates_available.set()
elif isinstance(update, tl.Updates) or \ elif isinstance(update, (tl.Updates, tl.UpdatesCombined)):
isinstance(update, tl.UpdatesCombined):
self._updates.extend(update.updates) self._updates.extend(update.updates)
self._updates_available.set() self._updates_available.set()

View File

@ -37,7 +37,7 @@ def get_display_name(entity):
else: else:
return '(No name)' return '(No name)'
if isinstance(entity, Chat) or isinstance(entity, Channel): if isinstance(entity, (Chat, Channel)):
return entity.title return entity.title
return '(unknown)' return '(unknown)'
@ -50,8 +50,7 @@ def get_extension(media):
"""Gets the corresponding extension for any Telegram media""" """Gets the corresponding extension for any Telegram media"""
# Photos are always compressed as .jpg by Telegram # Photos are always compressed as .jpg by Telegram
if (isinstance(media, UserProfilePhoto) or isinstance(media, ChatPhoto) or if isinstance(media, (UserProfilePhoto, ChatPhoto, MessageMediaPhoto)):
isinstance(media, MessageMediaPhoto)):
return '.jpg' return '.jpg'
# Documents will come with a mime type # Documents will come with a mime type
@ -87,12 +86,10 @@ def get_input_peer(entity, allow_self=True):
else: else:
return InputPeerUser(entity.id, entity.access_hash) return InputPeerUser(entity.id, entity.access_hash)
if any(isinstance(entity, c) for c in ( if isinstance(entity, (Chat, ChatEmpty, ChatForbidden)):
Chat, ChatEmpty, ChatForbidden)):
return InputPeerChat(entity.id) return InputPeerChat(entity.id)
if any(isinstance(entity, c) for c in ( if isinstance(entity, (Channel, ChannelForbidden)):
Channel, ChannelForbidden)):
return InputPeerChannel(entity.id, entity.access_hash) return InputPeerChannel(entity.id, entity.access_hash)
# Less common cases # Less common cases
@ -122,7 +119,7 @@ def get_input_channel(entity):
if type(entity).SUBCLASS_OF_ID == 0x40f202fd: # crc32(b'InputChannel') if type(entity).SUBCLASS_OF_ID == 0x40f202fd: # crc32(b'InputChannel')
return entity return entity
if isinstance(entity, Channel) or isinstance(entity, ChannelForbidden): if isinstance(entity, (Channel, ChannelForbidden)):
return InputChannel(entity.id, entity.access_hash) return InputChannel(entity.id, entity.access_hash)
if isinstance(entity, InputPeerChannel): if isinstance(entity, InputPeerChannel):
@ -266,7 +263,7 @@ def get_input_media(media, user_caption=None, is_photo=False):
if isinstance(media, MessageMediaGame): if isinstance(media, MessageMediaGame):
return InputMediaGame(id=media.game.id) return InputMediaGame(id=media.game.id)
if isinstance(media, ChatPhoto) or isinstance(media, UserProfilePhoto): if isinstance(media, (ChatPhoto, UserProfilePhoto)):
if isinstance(media.photo_big, FileLocationUnavailable): if isinstance(media.photo_big, FileLocationUnavailable):
return get_input_media(media.photo_small, is_photo=True) return get_input_media(media.photo_small, is_photo=True)
else: else:
@ -291,10 +288,9 @@ def get_input_media(media, user_caption=None, is_photo=False):
venue_id=media.venue_id venue_id=media.venue_id
) )
if any(isinstance(media, t) for t in ( if isinstance(media, (
MessageMediaEmpty, MessageMediaUnsupported, MessageMediaEmpty, MessageMediaUnsupported,
FileLocationUnavailable, ChatPhotoEmpty, ChatPhotoEmpty, UserProfilePhotoEmpty, FileLocationUnavailable)):
UserProfilePhotoEmpty)):
return InputMediaEmpty() return InputMediaEmpty()
if isinstance(media, Message): if isinstance(media, Message):
@ -319,11 +315,11 @@ def get_peer_id(peer, add_mark=False):
peer = get_input_peer(peer, allow_self=False) peer = get_input_peer(peer, allow_self=False)
# Set the right ID/kind, or raise if the TLObject is not recognised # Set the right ID/kind, or raise if the TLObject is not recognised
if isinstance(peer, PeerUser) or isinstance(peer, InputPeerUser): if isinstance(peer, (PeerUser, InputPeerUser)):
return peer.user_id return peer.user_id
elif isinstance(peer, PeerChat) or isinstance(peer, InputPeerChat): elif isinstance(peer, (PeerChat, InputPeerChat)):
return -peer.chat_id if add_mark else peer.chat_id return -peer.chat_id if add_mark else peer.chat_id
elif isinstance(peer, PeerChannel) or isinstance(peer, InputPeerChannel): elif isinstance(peer, (PeerChannel, InputPeerChannel)):
i = peer.channel_id i = peer.channel_id
if add_mark: if add_mark:
# Concat -100 through math tricks, .to_supergroup() on Madeline # Concat -100 through math tricks, .to_supergroup() on Madeline