-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Type hinting #1920
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
Type hinting #1920
Conversation
b79ada1
to
c856bbb
Compare
# Conflicts: # telegram/message.py
Okay, I prepared for going for ii.a) :D TBH, I didn't try to be as specific as possible. I rather tried to annotate most almost everything on a reasonably specific level and made sure that Mypy checks out. Many things can surely be improved, but IMO they can be revisited if
i.e. we can gradually improve the hints. Some more specific notes:
Comments on CI:
|
# Conflicts: # telegram/__main__.py # telegram/bot.py # telegram/callbackquery.py # telegram/chatmember.py # telegram/choseninlineresult.py # telegram/error.py # telegram/ext/callbackqueryhandler.py # telegram/ext/commandhandler.py # telegram/ext/conversationhandler.py # telegram/ext/dictpersistence.py # telegram/ext/filters.py # telegram/ext/inlinequeryhandler.py # telegram/ext/jobqueue.py # telegram/ext/messagequeue.py # telegram/ext/picklepersistence.py # telegram/ext/regexhandler.py # telegram/ext/stringcommandhandler.py # telegram/ext/stringregexhandler.py # telegram/ext/typehandler.py # telegram/files/animation.py # telegram/files/document.py # telegram/files/sticker.py # telegram/files/venue.py # telegram/files/video.py # telegram/files/videonote.py # telegram/files/voice.py # telegram/games/game.py # telegram/games/gamehighscore.py # telegram/inline/inlinekeyboardmarkup.py # telegram/inline/inlinequery.py # telegram/message.py # telegram/messageentity.py # telegram/passport/credentials.py # telegram/passport/encryptedpassportelement.py # telegram/passport/passportdata.py # telegram/passport/passportfile.py # telegram/payment/orderinfo.py # telegram/payment/precheckoutquery.py # telegram/payment/shippingoption.py # telegram/payment/shippingquery.py # telegram/payment/successfulpayment.py # telegram/poll.py # telegram/replykeyboardmarkup.py # telegram/update.py # telegram/user.py # telegram/userprofilephotos.py # telegram/utils/helpers.py # telegram/utils/webhookhandler.py
# Conflicts: # telegram/bot.py # telegram/botcommand.py # telegram/callbackquery.py # telegram/chat.py # telegram/chatpermissions.py # telegram/dice.py # telegram/ext/basepersistence.py # telegram/ext/conversationhandler.py # telegram/ext/dictpersistence.py # telegram/ext/dispatcher.py # telegram/ext/filters.py # telegram/ext/jobqueue.py # telegram/ext/messagequeue.py # telegram/ext/picklepersistence.py # telegram/ext/updater.py # telegram/files/animation.py # telegram/files/audio.py # telegram/files/document.py # telegram/files/photosize.py # telegram/files/sticker.py # telegram/files/video.py # telegram/files/videonote.py # telegram/files/voice.py # telegram/games/game.py # telegram/inline/inlinekeyboardbutton.py # telegram/inline/inlinekeyboardmarkup.py # telegram/inline/inputlocationmessagecontent.py # telegram/message.py # telegram/parsemode.py # telegram/passport/passportfile.py # telegram/payment/invoice.py # telegram/poll.py # telegram/replykeyboardmarkup.py # telegram/update.py # telegram/userprofilephotos.py # telegram/utils/helpers.py # telegram/utils/request.py # telegram/utils/webhookhandler.py # telegram/webhookinfo.py # tests/test_conversationhandler.py # tests/test_message.py # tests/test_updater.py
3247477
to
b7293a4
Compare
# Conflicts: # README.rst # telegram/bot.py # telegram/botcommand.py # telegram/chatmember.py # telegram/chatpermissions.py # telegram/dice.py # telegram/ext/basepersistence.py # telegram/ext/defaults.py # telegram/ext/dispatcher.py # telegram/ext/filters.py # telegram/ext/jobqueue.py # telegram/ext/updater.py # telegram/files/animation.py # telegram/files/audio.py # telegram/files/document.py # telegram/files/file.py # telegram/files/photosize.py # telegram/files/sticker.py # telegram/files/video.py # telegram/files/videonote.py # telegram/files/voice.py # telegram/games/game.py # telegram/inline/inlinekeyboardbutton.py # telegram/inline/inlinekeyboardmarkup.py # telegram/inline/inlinequery.py # telegram/message.py # telegram/passport/passportfile.py # telegram/payment/invoice.py # telegram/poll.py # telegram/replykeyboardmarkup.py # telegram/update.py # telegram/userprofilephotos.py # telegram/utils/helpers.py # telegram/utils/request.py # telegram/utils/webhookhandler.py # telegram/webhookinfo.py # tests/test_jobqueue.py # tests/test_message.py
@@ -144,7 +149,8 @@ def edit_message_text(self, text, *args, **kwargs): | |||
return self.bot.edit_message_text(text, chat_id=self.message.chat_id, | |||
message_id=self.message.message_id, *args, **kwargs) | |||
|
|||
def edit_message_caption(self, caption, *args, **kwargs): | |||
def edit_message_caption(self, caption: str, *args: Any, | |||
**kwargs: Any) -> Union[Message, bool]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange line break
@@ -172,7 +178,8 @@ def edit_message_caption(self, caption, *args, **kwargs): | |||
message_id=self.message.message_id, | |||
*args, **kwargs) | |||
|
|||
def edit_message_reply_markup(self, reply_markup, *args, **kwargs): | |||
def edit_message_reply_markup(self, reply_markup: 'InlineKeyboardMarkup', *args: Any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh I'd just add black to pre-commit.yml aswell if every single file is being changed anyway...
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
Advantage: Never ever any discussion around formatting again.
Disadvantages: None. Except you don't like PEP8 and prefer your own code style to keep your job with this Arbeitsbeschaffungsmaßnahme. ;D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think code formatting is worth its own discussion and PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Monumental PR! I eagerly look forward to have static typing supported in our library. Just a couple of small discussion points, most of them repeat themself. Then I am all good with it. Thank you so much for putting the time into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now! If there actually are errors, we can fix them in a quick release after 13. I would merge.
Codecov is still failing, but that's mostly due to this PR
So we can just up the coverage the time we change some code … Merging :) (Codacy fails, because the v13 branch is not tracked …) |
# Conflicts: # telegram/error.py # telegram/ext/callbackcontext.py # telegram/ext/callbackqueryhandler.py # telegram/ext/commandhandler.py # telegram/ext/conversationhandler.py # telegram/ext/dispatcher.py # telegram/ext/handler.py # telegram/ext/inlinequeryhandler.py # telegram/ext/messagehandler.py # telegram/ext/regexhandler.py # telegram/ext/stringcommandhandler.py # telegram/ext/stringregexhandler.py # telegram/ext/typehandler.py # telegram/utils/promise.py
# Conflicts: # telegram/error.py # telegram/ext/callbackcontext.py # telegram/ext/callbackqueryhandler.py # telegram/ext/commandhandler.py # telegram/ext/conversationhandler.py # telegram/ext/dispatcher.py # telegram/ext/handler.py # telegram/ext/inlinequeryhandler.py # telegram/ext/messagehandler.py # telegram/ext/regexhandler.py # telegram/ext/stringcommandhandler.py # telegram/ext/stringregexhandler.py # telegram/ext/typehandler.py # telegram/utils/promise.py
Preparations for type hinting:
Mypy as static type checker is added to pre-commit and make-file
Python 3.5 support is dropped, to allow typing for variables
Two make sure that new PRs include typing, I tried two approaches:
diff-cover
to compute the coverage of the changed lines only and let the test fail, if its < 100%. Unfortunately, mypy will mark any line with an "Any" as uncovered, even if that's allowed during type checking. But especially intelegram.ext
we have a lot of stuff that can be "Any" (CH states, entries ofuser_data
and so on). So, even after adding typing for the current code base, we would eitherdiff-cover
approach: its easier to see, if we're satisfied or not. Downside: When a file is changed all functions in that file will be checked and all classes/functions that are imported or inherited from, too. We can eitherpre-commit
test and allow it to fail until the whole base is typedPersonally, I like ii. better that i., which is why this is the current state of this pr. More precisely, I think I'd vote for ii.c)
Closes #1373