-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Customize context #2262
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
Customize context #2262
Conversation
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.
Hey there. Relax, I am just a little warning for the maintainers to release directly after merging your PR, otherwise we have broken examples and people might get confused :)
Alright, worked a bit on this. Type hinting is now on a reasonably good level. some comments:
Tests for |
I skipped Probably needs some reworking when #2324 is merged, i.e. moving some types from |
Hey @Bibo-Joshi, I'm just wondering what is the decision to have the dispatcher fetch the all users and chats data upon initialization and then later only use single items (per user_id and chat_id) when constructing the context and when updating persistence?
|
Sorry, I don't understand the question. Are you asking, why the default is to load everything on init? Or where in the code you can see that decision? |
The actual decision, I've updated my comment above for the reason why that might be an issue. |
I'm not sure if I fully understand the first bullet point, but if I do, then both arguments are exactly what this PR tries to improve. See #2068 for a more detailed description. |
Just clarify with the example:
|
Wouldn't it be easier to have the persistence actually provide get_user_data(user_id) and just use that when constructing the context. It should simplify the codebase and not need the Dict/Mapping piece. Though might not be backwards compatible with previous version. |
No, this is not, what will be happening, or is currently happening. the dispatcher calls get_user_data on init, which returns a mapping, say M (currently a defaultdict). now when an update comes in, dispatcher will access M[user_id]. It's up to M to decide whether the corresponding data for this (and only this!) user has already been loaded or not and load it if necessary. After processing the update, dispatcher will call update_user_data(data, user_id). It's up to the persistence to do the updating of the data for this (and only this!) user. Analogously for So if something's overriden anywhere, it's the fault of either the mapping or the persistence, but never of the dispatcher ;)
This one got me thinking :D It might actually be the better approach in two senses:
To make this work with and without persistence, we'd need to refactor how dispatcher is storing user/chat_data internally. More precisely, we would need methods
And I guess this can be made in a non-breaking way:
My thoughts about how we should proceed:
Other things that came to my mind while thinking about this:
|
Thanks, I stand corrected on the override stuff. And I guess the return of (user/chat)_data should be a Mapping (which is the case in this PR 💯 ) as in actually returning the protocol on how to fetch the data and not necessarily the data itself, but that should maybe be addressed in the docs. The But why not really move the whole data storing and persistence into one component out of dp altogether (like Datastore), that could make a decision on what to do with the online data and storing it whenever it's underlying implementation needs. Dispatcher only would need to have hook calls to it. I guess the same user/chat_data object issue across callbacks could be addressed by:
So persistence would be on all the time, just the default implementation would be similar to the current DictPersistence. |
# Conflicts: # telegram/ext/callbackcontext.py # telegram/ext/commandhandler.py # telegram/ext/picklepersistence.py
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.
example review
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.
found this in another quick glance
Realized that having |
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
# Conflicts: # telegram/ext/dispatcher.py # telegram/ext/picklepersistence.py # telegram/ext/updater.py # tests/test_persistence.py
TODO:
.. versionadded::
directivesPOC for #2068, so we have some code base to discuss about
The first three commits only introduce a
custom_context
parameter to specify a subclass ofCallbackContext
. That works pretty fineThe last commit expands to customizing
chat/user/bot_data
and is more in a POC state. Some comments:ContextCustomizer
- the idea being similarly toDefaults
. including that stuff directly intoDefaults
doesn't seem like a good idea to me. The naming is only temporary, though, I'm not really happy withContextCustomizer
DictPersistence
andPicklePersistence
generic, i.e. allow using it with custom classes. But probably that doesn't have to be in the same PR, as that will mean writing a lot of boring tests …Dispatcher
now being a generic class depending on the classes passed via thecontext_customizer
object, I somehow need to extract those in a way mypy understands. Will probably make a SO question for that at some point … Also I need to figure out how to annotate something likeuser_data: UDMapping[int, UDClass]
, where bothUDMapping
andUDClass
need to beTypeVars
(I think)Edit:
First SO question coming from this PR