Skip to content
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

Introduce keyword-only arguments in Bot methods #3035

Merged
merged 7 commits into from
May 18, 2022
Merged

Introduce keyword-only arguments in Bot methods #3035

merged 7 commits into from
May 18, 2022

Conversation

tal66
Copy link
Contributor

@tal66 tal66 commented May 14, 2022

Hi, this closes #3016.
Made {read, write, connect, pool}_timeout , api_kwargs keyword-only arguments.
The docs generated looked fine to me, and the tests passed without much changing - did I miss anything?

(btw, happened to notice in test_message.py, that some args were in the wrong order - for example, line 347. just added argument names there)

Edit: saw the pre-commit formatting error (4 lines), will fix in the next commit after your comments

Copy link
Member

@harshil21 harshil21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Overall looks great! Some comments:

  1. imo, the quote argument of the shortcut methods in Message, Chat, User should also be keyword only since its added by PTB. But lets wait and see what the other maintainers think.

  2. About tests:

    • In test_bot.py, one could add a test to check all bot methods and see if there are exactly 5 keyword arguments by checking their kind and name ({read, write, connect, pool}_timeout and api_kwargs). One could retrieve all public bot methods by using iscoroutinefunction and checking if the method doesn't start with an underscore.
    • The same would be need to done for the shortcuts, i.e in test_{chat, user, message}
  3. Can you also add to the points in the .. versionchanged:: 20.0 at the the top of bot.py, chat.py, message.py, and user.py files saying that the args ({read, write...) were changed to keyword only?

telegram/_bot.py Outdated Show resolved Hide resolved
telegram/_bot.py Show resolved Hide resolved
telegram/_chat.py Outdated Show resolved Hide resolved
telegram/_user.py Outdated Show resolved Hide resolved
telegram/_user.py Show resolved Hide resolved
tests/test_chat.py Show resolved Hide resolved
@harshil21 harshil21 added this to the v20.0a1 milestone May 14, 2022
@harshil21 harshil21 added 🛠 refactor change type: refactor 🛠 breaking change type: breaking labels May 14, 2022
@tal66
Copy link
Contributor Author

tal66 commented May 14, 2022

2nd commit addressed some of your comments.

I'll add the test you asked tomorrow (UTC time).

Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice PR! I left a new nitpicks below.

  1. imo, the quote argument of the shortcut methods in Message, Chat, User should also be keyword only since its added by PTB. But lets wait and see what the other maintainers think.

I guess the need to separate that from the other arguments is not as big, but I'm okay with the idea. We should then however make all PTB-added arguments keyword-only. We have have few more of those, e.g. InlineQurey.answer(…, auto_pagination=True) and Bot.send_location(…, location=…). For the methods of Bot, those can be seen here. For the shortcut methods, that would be all the arguments that are passed check_shortcut_signature(…, addition_kwargs=[…]) in the tests, e.g. here.
Inserting a print(shortcut, additional_kwargs) here & then running the test suite once should reveal all of them.

Corresponding .. versionchanged:: 20.0 directives should be added.

  1. About tests:

    • In test_bot.py, one could add a test to check all bot methods and see if there are exactly 5 keyword arguments by checking their kind and name ({read, write, connect, pool}_timeout and api_kwargs). One could retrieve all public bot methods by using iscoroutinefunction and checking if the method doesn't start with an underscore.
    • The same would be need to done for the shortcuts, i.e in test_{chat, user, message}

I think we can get away more easily if we make all ptb-added parameters kwargs-only:

  • For checking the signature of bot methods, we can just check in test_official that all parameters not documented by TG are are kwargs-only.
  • For checking shortcut signatures, it would suffice to ensure that the additional_kwargs are kwargs-only. all other parameters are matched against the Bot signature anyway.

@harshil21 what do you think?

@tal66 I'm aware that unit tests can be tedious to wrap your head around if you're not entirely familiar with the code base. If you have troubles with updating tests, please don't hesitate to reach out either here or in the dev-group on Telgeram!

telegram/_bot.py Outdated Show resolved Hide resolved
telegram/_bot.py Outdated Show resolved Hide resolved
telegram/_bot.py Show resolved Hide resolved
telegram/_chat.py Outdated Show resolved Hide resolved
telegram/_message.py Outdated Show resolved Hide resolved
telegram/_user.py Outdated Show resolved Hide resolved
@harshil21
Copy link
Member

make all PTB-added arguments keyword-only

agree.

we can just check in test_official

sure, modifying that test would be easier than starting from scratch

@tal66
Copy link
Contributor Author

tal66 commented May 15, 2022

Hi, for the tests I wanted to use inspect.getfullargspec, but for some reason it doesn't work as expected from inside test_official (thought of using it in check_method).

For example, print(inspect.getfullargspec(telegram.Bot.send_message)) gives FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'telegram._message.Message'>}) with args and kwonlyargs empty [] (even though inspect.signature does work). And it's the same for all other methods.
Any idea why?

@tal66
Copy link
Contributor Author

tal66 commented May 15, 2022

well, I tried some stuff and this works: [p.name for p in inspect.signature(method).parameters.values() if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD]. nice. but it's still weird that getfullargspec doesn't.

@Bibo-Joshi
Copy link
Member

Hi. getfullargspec can not "see through" decorators (at least not in all cases IIRC) and all the bot methods have a decorator for logging. inspect.signature is indeed the recommended method for inspecting the signature :)

telegram/_bot.py Show resolved Hide resolved
telegram/_inline/inlinequery.py Show resolved Hide resolved
telegram/_inline/inlinequery.py Outdated Show resolved Hide resolved
tests/test_official.py Outdated Show resolved Hide resolved
@Bibo-Joshi
Copy link
Member

I took the liberty to further adjust check_shortcut_signature, which also revealed one more necessary change in CallbackQuery.edit_message_caption :)
With this, I don't think there's anything left to add from my side. If @harshil21 gives a LGTM as well, we can merge :)

@tal66
Copy link
Contributor Author

tal66 commented May 17, 2022

ok, thanks. i didn't run your change, but from reading it seems like an exception will be raised on the first arg that isn't a kw, and stop the for loop. whereas in my implementation it showed me the full set of args that should be kw (if it matters).

tests/conftest.py Outdated Show resolved Hide resolved
Copy link
Member

@harshil21 harshil21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! If there are some docs to updated, that can also be done later on

@Bibo-Joshi Bibo-Joshi merged commit 076955d into python-telegram-bot:master May 18, 2022
@Bibo-Joshi
Copy link
Member

Well, pressing enter by accident messed up the commit message … but anyway, here we go 😄 Thanks a lot for the contribution @tal66 and thanks for the patience during the review process :)

@github-actions github-actions bot locked and limited conversation to collaborators May 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🛠 breaking change type: breaking 🛠 refactor change type: refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce keyword-only arguments for auxiliary arguments of Bot methods
3 participants