Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

Type hints

Open Lonami opened this issue 5 years ago • 18 comments

Some of them are wrong, and a lot of them are missing. It would be really good to type hint all parameters and return values on the public API of the library. This includes but is not limitted to the methods in the TelegramClient, those in Message, Button, etc.

If you spot a type hint that's wrong, please report it here so we can make a single commit aggregating the changes.

Lonami avatar May 29 '19 07:05 Lonami

For example, connection_retries=None is wrong, because it should be optional.

Lonami avatar May 29 '19 07:05 Lonami

IMG_20200127_151847 I guess, get_messages shall be [TotalList, Message]

apepenkov avatar Jan 27 '20 12:01 apepenkov

https://github.com/LonamiWebs/Telethon/blob/master/telethon/client/account.py#L121 should be max_file_size: int (and technically also Optional, because None is allowed)

tulir avatar Feb 04 '20 21:02 tulir

functions.messages.GetCommonChatsRequest изображение add int

apepenkov avatar Feb 20 '20 14:02 apepenkov

same for functions.photos.GetUserPhotosRequest. I guess to TypeInputUser shall be added string(for username) int (for id), User (currently it's not there)

apepenkov avatar Feb 20 '20 14:02 apepenkov

@apepenkov Not sure if those should be included, since the raw API obviously doesn't take ints, it's just telethon converting them. Would be better to reduce the necessity of using raw requests.

tulir avatar Feb 20 '20 17:02 tulir

@apepenkov Not sure if those should be included, since the raw API obviously doesn't take ints, it's just telethon converting them. Would be better to reduce the necessity of using raw requests.

But in docs (and param itself) looks like need id/username. send_message or so converts actually too, but there are hints for int and str

apepenkov avatar Feb 21 '20 10:02 apepenkov

image send_message doesn't accept types.InputPhoto as file

apepenkov avatar May 10 '20 11:05 apepenkov

send_message doesn't accept types.InputMediaInvoice as file image

apepenkov avatar May 20 '20 19:05 apepenkov

There are a loooot of missing type-hints and it would be better if there was a branch just for fixing them (so that people could directly fix them instead of reporting here, hence speeding up the process).

HosseyNJF avatar Dec 07 '20 10:12 HosseyNJF

That's a good idea, I've created the typehints branch. We can combine all fixes there and then squash into a single main commit once we merge to master.

Lonami avatar Dec 07 '20 13:12 Lonami

Hi. Has anyone attempted tweaking the code generator, so that the hints of the raw API requests arguments are correct? Namely, Telethon can convert a lot of stuff into a valid input entity, but the type hints don't reflect this at all. I'm thinking that something as easy as Union[RealInputEntityType, EntityLike] would already be an improvement. Obviously, sometimes we can't tell if something will be successfully converted into the right type at runtime, but it's better than having your IDE telling you the argument type is wrong.

image

I'd be happy to study it more and maybe submit a PR. @Lonami, please express your opinion on this, if you have a chance.

Thoufak avatar Sep 25 '21 17:09 Thoufak

It could be a good idea to update the type hints to reflect the "auto-conversion" better. The type hints can get quite large, but it should be fine as long as it makes the IDE happy. I won't be opposed to a PR adding this feature, but beware the generator code is quite messy at the moment. Also, I'm in the process of updating the library so that it's ready for version 2.0. Once that's merged, I will review all open issues and ping back (if I remember) so that you don't unnecessarily work on something that may change.

Lonami avatar Sep 25 '21 17:09 Lonami

I'm excited to hear you're working on 2.0. Good luck with that! Will wait for your ping then.

Thoufak avatar Sep 26 '21 10:09 Thoufak

Did you include the py.typed file into the typed branch? (I'm asking because I can't see it)

https://www.python.org/dev/peps/pep-0561/#packaging-type-information

Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing.

Without this file mypy totally ignores type and consider the package as untyped

mdantonio avatar Oct 08 '21 05:10 mdantonio

Don't we need to add pyi files for type checking?

tsathis avatar Mar 13 '22 16:03 tsathis

is the official work on this still active? asking as i could not find a type hinting branch anymore, and 2.0 is still not out?

Morisander avatar Mar 13 '24 11:03 Morisander

I don't plan to add type hints to v1. Progress on v2 is slow, mostly due to lack of motivation, but I still hope to finish it one day.

Lonami avatar Mar 13 '24 15:03 Lonami