Enhancement: make SessionMiddleware
return custom types
#3470
Labels
Enhancement
This is a new feature or request
SessionMiddleware
return custom types
#3470
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: