use generic bases for session (#5638)

This commit is contained in:
David Lord 2024-11-13 08:27:11 -08:00 committed by GitHub
commit 2eab96a32a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import collections.abc as c
import hashlib import hashlib
import typing as t import typing as t
from collections.abc import MutableMapping from collections.abc import MutableMapping
@ -20,8 +21,7 @@ if t.TYPE_CHECKING: # pragma: no cover
from .wrappers import Response from .wrappers import Response
# TODO generic when Python > 3.8 class SessionMixin(MutableMapping[str, t.Any]):
class SessionMixin(MutableMapping): # type: ignore[type-arg]
"""Expands a basic dictionary with session attributes.""" """Expands a basic dictionary with session attributes."""
@property @property
@ -49,8 +49,7 @@ class SessionMixin(MutableMapping): # type: ignore[type-arg]
accessed = True accessed = True
# TODO generic when Python > 3.8 class SecureCookieSession(CallbackDict[str, t.Any], SessionMixin):
class SecureCookieSession(CallbackDict, SessionMixin): # type: ignore[type-arg]
"""Base class for sessions based on signed cookies. """Base class for sessions based on signed cookies.
This session backend will set the :attr:`modified` and This session backend will set the :attr:`modified` and
@ -72,7 +71,10 @@ class SecureCookieSession(CallbackDict, SessionMixin): # type: ignore[type-arg]
#: different users. #: different users.
accessed = False accessed = False
def __init__(self, initial: t.Any = None) -> None: def __init__(
self,
initial: c.Mapping[str, t.Any] | c.Iterable[tuple[str, t.Any]] | None = None,
) -> None:
def on_update(self: te.Self) -> None: def on_update(self: te.Self) -> None:
self.modified = True self.modified = True
self.accessed = True self.accessed = True