Skip to content

[FEATURE] Customization of context logic #2068

@Bibo-Joshi

Description

@Bibo-Joshi

Is your feature request related to a problem? Please describe.

  • The CallbackContext introduced in v12 is a very central thing in the library and at some point people will wanna customize it, especially when using collect_additional_context in custom handlers. Also when we get typing (Type hinting #1920, due to v13), type checkers will complain setting custom attributes.
  • The persistence setup, which is tightly integrated with context, has two shortcomings:
    • Currently, on startup all data is loaded from the persistence-backend, which might slow down the startup on huge databases and even might be unneccessary. Allowing to implement a fetch-on-demand logic would solve that
    • When sharing a database with an external service, currently there is no automated way to update the data while running the process. Again, more customization of the persistence setup could solve that

Describe the solution you'd like

  1. Allow for BasePersistence.get_data() to return custom classes, defaulting to dict(). That way users can solve both of the above mentioned problems on their own (same for get_conversations). For most classes this should still be compatible with the changes introduced in Refactor Bot persistence #1994, but advanced users can use set/replace_bot() manually anyway …
  2. Allow to pass a custom subclass of CallbackContext to Updater/Dispatcher that will be used instead of CallbackContext. We should document CC.from_*() for that.
  3. Those custom classes (3 for bot/user/chat_data, 1 for context) should be passable to Updater/Dispatcher in a clean way. I.e. probably add a single parameter to pass it as tuple/dict/custom class or maybe add it to the Defaults class, although I'm not sure that's a good idea
  4. As we're introducing type hinting in v13 (Type hinting #1920), ideally the typing is clever enough to tell the handlers that they get (update: Update, context: CustomCallbackContext) instead of (update: Update, context: CallbackContext)

Describe alternatives you've considered

Additional context

Reference for devs: @JosXa @tsnoam and I had a brief discussion about some of these ideas in dev chat on 2020-08-25.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions