Clarify/fix get[_input]_entity docstrings

This commit is contained in:
Lonami Exo 2018-06-21 11:08:14 +02:00
parent 777c91ee14
commit d8af64e3b9

View File

@ -101,22 +101,31 @@ class UserMethods(TelegramBaseClient):
async def get_entity(self, entity): async def get_entity(self, entity):
""" """
Turns the given entity into a valid Telegram user or chat. Turns the given entity into a valid Telegram :tl:`User`, :tl:`Chat`
or :tl:`Channel`. You can also pass a list or iterable of entities,
and they will be efficiently fetched from the network.
entity (`str` | `int` | :tl:`Peer` | :tl:`InputPeer`): entity (`str` | `int` | :tl:`Peer` | :tl:`InputPeer`):
The entity (or iterable of entities) to be transformed. If an username is given, **the username will be resolved** making
If it's a string which can be converted to an integer or starts an API call every time. Resolving usernames is an expensive
with '+' it will be resolved as if it were a phone number. operation and will start hitting flood waits around 50 usernames
in a short period of time.
If it doesn't start with '+' or starts with a '@' it will be If you want to get the entity for a *cached* username, you should
be resolved from the username. If no exact match is returned, first `get_input_entity(username) <get_input_entity>` which will
an error will be raised. use the cache), and then use `get_entity` with the result of the
previous call.
If the entity is an integer or a Peer, its information will be Similar limits apply to invite links, and you should use their
returned through a call to self.get_input_peer(entity). ID instead.
If the entity is neither, and it's not a TLObject, an Using phone numbers, exact names, integer IDs or :tl:`Peer`
error will be raised. rely on a `get_input_entity` first, which in turn needs the
entity to be in cache, unless a :tl:`InputPeer` was passed.
Unsupported types will raise ``TypeError``.
If the entity can't be found, ``ValueError`` will be raised.
Returns: Returns:
:tl:`User`, :tl:`Chat` or :tl:`Channel` corresponding to the :tl:`User`, :tl:`Chat` or :tl:`Channel` corresponding to the
@ -184,20 +193,54 @@ class UserMethods(TelegramBaseClient):
async def get_input_entity(self, peer): async def get_input_entity(self, peer):
""" """
Turns the given peer into its input entity version. Most requests Turns the given peer into its input entity version. Most requests
use this kind of InputUser, InputChat and so on, so this is the use this kind of :tl:`InputPeer`, so this is the most suitable call
most suitable call to make for those cases. to make for those cases. **Generally you should let the library do
its job** and don't worry about getting the input entity first, but
if you're going to use an entity often, consider making the call:
>>> import asyncio
>>> rc = asyncio.get_event_loop().run_until_complete
>>>
>>> from telethon import TelegramClient
>>> client = TelegramClient(...)
>>> # If you're going to use "username" often in your code
>>> # (make a lot of calls), consider getting its input entity
>>> # once, and then using the "user" everywhere instead.
>>> user = rc(client.get_input_entity('username'))
>>> # The same applies to IDs, chats or channels.
>>> chat = rc(client.get_input_entity(-123456789))
entity (`str` | `int` | :tl:`Peer` | :tl:`InputPeer`): entity (`str` | `int` | :tl:`Peer` | :tl:`InputPeer`):
The integer ID of an user or otherwise either of a If an username is given, **the library will use the cache**. This
:tl:`PeerUser`, :tl:`PeerChat` or :tl:`PeerChannel`, for means that it's possible to be using an username that *changed*.
which to get its ``Input*`` version.
If this ``Peer`` hasn't been seen before by the library, the top If the username is not found in the cache, it will be fetched.
dialogs will be loaded and their entities saved to the session The same rules apply to phone numbers (``'+34 123456789'``).
file (unless this feature was disabled explicitly).
If in the end the access hash required for the peer was not found, If an exact name is given, it must be in the cache too. This
a ValueError will be raised. is not reliable as different people can share the same name
and which entity is returned is arbitrary, and should be used
only for quick tests.
If a positive integer ID is given, the entity will be searched
in cached users, chats or channels, without making any call.
If a negative integer ID is given, the entity will be searched
exactly as either a chat (prefixed with ``-``) or as a channel
(prefixed with ``-100``).
If a :tl:`Peer` is given, it will be searched exactly in the
cache as either an user, chat or channel.
If the given object can be turned into an input entity directly,
said operation will be done.
Invite links make an API call **always** and are expensive.
You should use the chat ID instead.
Unsupported types will raise ``TypeError``.
If the entity can't be found, ``ValueError`` will be raised.
Returns: Returns:
:tl:`InputPeerUser`, :tl:`InputPeerChat` or :tl:`InputPeerChannel` :tl:`InputPeerUser`, :tl:`InputPeerChat` or :tl:`InputPeerChannel`