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
Enhancement: make SessionMiddleware
return custom types
#3470
Comments
I'd like to push back a little bit on this. |
Yes, that's more reasonable. Injecting sessions from e.g. a session-plugin that provides the necessary middlewares/DI providers seems more practical. |
Agreed. I really like the idea of being able to inject the session as part of DI. Then we should just hold back on this until v3. |
Summary
Currently, the
BaseSessionBackend
handles serialization of custom types (such asStruct
,BaseModel
etc.) using the type encoders set by the user. However, when it deserializes the data, the user will not get back the types that was initially serialized but only a dictionary. This is a bit inconsistent and removes type safety and other IDE/editor benefits when accessing data fromrequest.session
unless they manually convert the returned dictionary into a class or cast it into aTypedDict.
For example,
It would be nice if the session config could be given a type to which the session middleware (or rather, the session backend) would deserialize the session data into.
Basic Example
No response
Drawbacks and Impact
There may be some difficulty in making this backwards compatible if we want to make
request.session
return a generic type since we would have to add another generic parameter toRequest.
If this is not possible, users could do something likesession = cast(Session, request.session)
and the breaking changes could be made in v3. This also may require changes in the API ofBaseSessionBackend.deserialize_data
to allow it to take aScope
in order to get the type decoders as done inBaseSessionBackend.serialize_data.
Unresolved questions
No response
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: