-
-
Notifications
You must be signed in to change notification settings - Fork 893
CallbackData with default prefix #1668
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
base: dev-3.x
Are you sure you want to change the base?
Conversation
✔️ Changelog found.Thank you for adding a description of the changes |
cls.__separator__ = kwargs.pop("sep", ":") | ||
cls.__prefix__ = kwargs.pop("prefix") | ||
cls.__prefix__ = prefix |
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.
Why not simply do this?
cls.__prefix__ = kwargs.pop("prefix", cls.__name__)
Please provide more details and explain why these changes are needed. Please note that callback data has a strict size limit (64 bytes) on the Telegram Bot API side, so you should not increase the data size. In most cases, 1-5 characters are enough for the prefix part. But on the other hand, class names should be as clear and self-descriptive as possible, so in most cases this means that there should be more than 5 characters to explain what this class does (Exactly what you showed in your example). Therefore, these statements are mutually exclusive. |
My use case is a fairly large, a couple of levels deep settings menu, built on inline keyboards. I use CallbackData classes as actions to move between levels and adjust settings. In this case three are many CallbackData classes either without, or with a single data field. So, there is no issues with the 64 bytes limit. But I'm forced to define the prefix anyways. |
Description
This PR modifies
CallbackData
and makes the prefix optional. By default the class name is used as prefix.This makes it less tedious to define a large number of custom
CallbackData
classes. For instance:NOTE: This PR does not touch the documentation, because I was not sure how welcome this change would be. It made my own code easier to maintain. If you think it's useful, I'll update the documentation.
Type of change
How Has This Been Tested?
This PR removes one test that checks that an exception is raised when the prefix is not provided. And adds one test that checks the new behavior.
Test Configuration:
Checklist: