From 1f4efbe7094c0a9a79650854bcc82953e564e701 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 25 May 2024 12:59:46 -0500 Subject: [PATCH] `AbstractBaseSession`: Use model fields for subclassed cases (#2180) * `AbstractBaseSession`: Use model fields for subclassed cases In situations where these fields are overridden in custom models, for instance extending 'session_key`'s `max_length`. Follow a similar style to `AuthBaseUser`. See also: - https://docs.djangoproject.com/en/5.0/topics/http/sessions/#extending-database-backed-session-engines - https://github.com/django/django/blob/5.0.6/django/contrib/sessions/base_session.py - https://github.com/typeddjango/django-stubs/blob/5.0.0/django-stubs/contrib/sessions/base_session.pyi#L13-L21 * `AbstractBaseSession`: Remove `objects` (declared in `Model`) Co-authored-by: Marti Raudsepp * `AbstractBaseSession`: Use `ClassVar` (credit: @flaeppe) --------- Co-authored-by: Marti Raudsepp --- django-stubs/contrib/sessions/base_session.pyi | 11 ++++++----- scripts/stubtest/allowlist_todo.txt | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/django-stubs/contrib/sessions/base_session.pyi b/django-stubs/contrib/sessions/base_session.pyi index f016f4b98..4da1bdb26 100644 --- a/django-stubs/contrib/sessions/base_session.pyi +++ b/django-stubs/contrib/sessions/base_session.pyi @@ -1,8 +1,9 @@ from datetime import datetime -from typing import Any, TypeVar +from typing import Any, ClassVar, TypeVar from django.contrib.sessions.backends.base import SessionBase from django.db import models +from typing_extensions import Self _T = TypeVar("_T", bound=AbstractBaseSession) @@ -11,10 +12,10 @@ class BaseSessionManager(models.Manager[_T]): def save(self, session_key: str, session_dict: dict[str, Any], expire_date: datetime) -> _T: ... class AbstractBaseSession(models.Model): - expire_date: datetime - session_data: str - session_key: str - objects: Any + session_key = models.CharField(primary_key=True) + session_data = models.TextField() + expire_date = models.DateTimeField() + objects: ClassVar[BaseSessionManager[Self]] @classmethod def get_session_store_class(cls) -> type[SessionBase] | None: ... diff --git a/scripts/stubtest/allowlist_todo.txt b/scripts/stubtest/allowlist_todo.txt index f5bd7c869..4fb92026b 100644 --- a/scripts/stubtest/allowlist_todo.txt +++ b/scripts/stubtest/allowlist_todo.txt @@ -509,6 +509,7 @@ django.contrib.sessions.backends.signed_cookies.SessionStore.exists django.contrib.sessions.base_session.AbstractBaseSession.expire_date django.contrib.sessions.base_session.AbstractBaseSession.get_next_by_expire_date django.contrib.sessions.base_session.AbstractBaseSession.get_previous_by_expire_date +django.contrib.sessions.base_session.AbstractBaseSession.objects django.contrib.sessions.base_session.AbstractBaseSession.session_data django.contrib.sessions.base_session.AbstractBaseSession.session_key django.contrib.sessions.base_session.BaseSessionManager.__slotnames__