Skip to content

Commit

Permalink
AbstractBaseSession: Use model fields for subclassed cases (#2180)
Browse files Browse the repository at this point in the history
* `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 <[email protected]>

* `AbstractBaseSession`: Use `ClassVar` (credit: @flaeppe)

---------

Co-authored-by: Marti Raudsepp <[email protected]>
  • Loading branch information
tony and intgr authored May 25, 2024
1 parent e196985 commit 1f4efbe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 6 additions & 5 deletions django-stubs/contrib/sessions/base_session.pyi
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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: ...
Expand Down
1 change: 1 addition & 0 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down

0 comments on commit 1f4efbe

Please sign in to comment.