-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Type-hinting for IDEs #1373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As far as I understand it, python stub files can have python 3 syntax type-hinting since they don't get executed but parsed by the IDE that does the type-hinting.
|
Yes, as far as I can see stub files can be made for the library (quite a lot of work) and a PR towards this is very welcome. Placing comments in the code for type hinting would be a half-baked (no signature types) and intermediate solution. I definitely would prefer stub files. @tiirex9 would you like to do this? |
I will have a look into this, yes. :) |
Those stub files can be put in Typeshed which is where the major IDEs pull their stubs from. |
Is that general consent that this should be added to typeshed instead of the library itself? |
It can be both indeed. Typeshed is meant for packages you don't have access to (builtins, third-party libs) and last time I asked the answer was that we didn't want any typings in the library, so it made sense. But having them directly in the checkout is obviously valuable. |
So it's a lot of work if I would create all stub files manually and it would get very difficult to keep in track with the code. But since all the information is in the docstrings, we could generate it. |
Yes, but I do believe that is the only way we're really gonna get good quality stubs. And when we drop py2 support (soon-ish!) then it will be very easy to use those stubs as the actual signatures, so it's not wasted work :)
This should be done while editing the code. E.g. when adding a new method, it should also be added in the stubs while you are at it. Note that stubs also don't have to all be written at once. Writing stubs just for e.g. the .ext submodule or even parts of it, would still be a bonus :) |
Good point. 👍 |
Im thought about committing a change and could add hints. What is the currrent stance towards this? |
@Poolitzer none.
|
All good, except that we want the stub files in our repo, not typeshed |
If I understand correctly, the hinting could be added to the code directly, now that we deprecated py2? |
@Bibo-Joshi, as I understand from the previous discussions, all agreed to add type hinting using stub files (copy of all public class without body). It's doesn't affect using python2 |
Yes, but that was when we still supported Py2. With that gone, we can go for the cleaner solution of signature typing and I see no reason for that not being in the code |
@Bibo-Joshi, +1 for such a decision. If we don't support python2, I even can work on PR. Which minimal version of Python3 is supported by the library? |
Happy to hear that :D |
After some interal discussion, this is the plan:
@hyzyla @igorsereda you might want to wait for 1. to happen before getting stared or even PR for that :D |
I shall start with master prep |
I gave us a base: https://github.com/python-telegram-bot/python-telegram-bot/tree/type_hinting_master If we merge this into master, we will face the following downside: The implemented mypy workflow will always fail, until we fixed the mentioned errors. This should happen in the dedicated branch though, not in master. So I would actually propose that we develope in this branch. |
Is there no way to make mypy only check changed code? Like codecov does? |
If I remember correctly, I tried this before, and the problem is the workflow requires a github token for that (to access this change), and this token only works if the PR (where it originates) comes from inside this project. I might be mistaken though, I will need to investigate later. The current status of the branch is that it just runs through the whole project, deal with it ;P |
Investigating that would be great. Developing on non-master branches will lead to confusion … |
If I understood correctly from this article, when running locally |
@Bibo-Joshi This might be a problem if we only add/change a single function in a file with many others... |
Would have to test that, though … |
Well yes, you understood correctly ;P You can ofc do it, but we can also just implement it proper. |
@Poolitzer I tried something with the
I don't know, if I got all the arguments right and certainly this is no ideal solution. But until someone decides to build a codecov.io equivalent for typing coverage, I think it's a feasible way to go. PS: For future reference, the idea is from here. |
@Bibo-Joshi I did already add mypy to pre commits didnt I? I think you are onto something there, but we need a way to fail/succeed the test, after your coverage report runs through. And also, where is the file saved? We are talking github actions here right, that would mean it gets deleted, so we need to print the lines out. |
Uh, yes. I fiddled with the
The coverage test fails, if the coverage on the diff is < 100%. If there are errors in the typing itself, that
Currently, only the command line output of |
The html report is now saved as artifact and I made sure that |
Wouldn't this also allow us to remove the types from the docstrings and let sphinx infer them automatically? |
Theoretically, yes. Unfortunately, there are only third party extensions for that yet. This one reasonably active and I've tested it on a personal project. However, there still a lot of functionality missing, e.g. Class attributes and module constants arent handled and also default values are not appended to the arguments doc string automatically. Therefore personally I'd stick to the manual docstrings (for now) … On another note, annotating constants is supported only in py>=3.6, so maybe it'd be worth considering dropping py3.5 for this purpose. |
@Bibo-Joshi Uhm, didnt we had to drop .5 anyway because cryptography didnt support it? |
@Poolitzer Well, it's still in the test matrix … |
@Bibo-Joshi Oh okay, then it cant be it, must have been an older version, my bad |
I was looking for types as well, which would make the API much easier to use to newcomers. Could we drop 3.5 so we can introduce type-hinting? |
Let me throw https://github.com/dropbox/pyannotate in here. I'd wager that you're overthinking this and just making a single PR to master with all type information at once will be not too much work |
closed by #1920 |
Hi there,
I like to use the Message.reply_* functions but it's kind of annoying to always look up the parameters manually. There is a lack of type-hinting here. And with python stub files (or type hinting in the python files directly this would not be a problem at all.
The Problem is, if I have a update instance and I call
update.message
it doesn't know that this is a Message instance. If I tell my IDE usingmessage = update.message # type: Message
it works so far. But when I then typemessage.reply_text(
there is no type-hinting (or variable hinting for that matter) for the named parameters. This is annoying.I started to create python stub files like this:

But it would be awesome if type-hinting a la PEP-484 would be integrated in the project itself.
I know that python 2 compatibility (at least until EOL) is still a thing, but type-hinting with comments like described above (and at some place in PEP-484) is always possible for both python versions.

The text was updated successfully, but these errors were encountered: