CallbackData with default prefix
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:
class SettingsActions:
class GoBack(CallbackData):
pass
class ShowFirstMenu(CallbackData):
pass
class ShowSecondMenu(CallbackData):
pass
class SetSomeFlag(CallbackData):
flag: bool
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
- [x] New feature (non-breaking change which adds functionality)
- [x] This change requires a documentation update
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.
- [x] Test that the prefix is set to class name, when not given explicitly
- [x] Test that the prefix can be set explicitly
Test Configuration:
- Operating System: Linux
- Python version: 3.10.12
Checklist:
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
:heavy_check_mark: Changelog found.
Thank you for adding a description of the changes
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.
Please provide more details and explain why these changes are needed.
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.