Skip to content
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

Update contrib.sessions.backends #2351

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions django-stubs/contrib/sessions/backends/base.pyi
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
from _collections_abc import dict_items, dict_keys, dict_values
from collections.abc import Iterable
from datetime import datetime, timedelta
from typing import Any
from typing import Any, TypeVar, overload

from _typeshed import SupportsKeysAndGetItem

VALID_KEY_CHARS: Any

class CreateError(Exception): ...
class UpdateError(Exception): ...

_T = TypeVar("_T")
Viicos marked this conversation as resolved.
Show resolved Hide resolved

class SessionBase(dict[str, Any]):
TEST_COOKIE_NAME: str
TEST_COOKIE_VALUE: str
accessed: bool
modified: bool
serializer: Any
def __init__(self, session_key: str | None = None) -> None: ...
async def aset(self, key: str, value: Any) -> None: ...
@property
def key_salt(self) -> str: ...
@overload
async def aget(self, key: str) -> Any | None: ...
@overload
async def aget(self, key: str, default: Any) -> Any: ...
@overload
async def aget(self, key: str, default: _T) -> Any | _T: ... # type: ignore[misc]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to remove this case, Any | _T is still Any

Copy link
Contributor Author

@Viicos Viicos Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the signatures over from typeshed, but indeed because _VT is Any we can simplify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, pyright may retain the type information:

image

For a better user experience, maybe this can be useful?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still prefer not to complicate the signature, cause it does not provide anything extra, but is still visible in the autosuggestions / docs / etc.

@overload
async def apop(self, key: str) -> Any: ...
@overload
async def apop(self, key: str, default: Any) -> Any: ...
@overload
async def apop(self, key: str, default: _T) -> Any | _T: ... # type: ignore[misc]
Viicos marked this conversation as resolved.
Show resolved Hide resolved
async def asetdefault(self, key: str, value: Any) -> Any: ...
def set_test_cookie(self) -> None: ...
async def aset_test_cookie(self) -> None: ...
def test_cookie_worked(self) -> bool: ...
async def atest_cookie_worked(self) -> bool: ...
def delete_test_cookie(self) -> None: ...
async def adelete_test_cookie(self) -> None: ...
def encode(self, session_dict: dict[str, Any]) -> str: ...
def decode(self, session_data: bytes | str) -> dict[str, Any]: ...
@overload # type: ignore[override]
def update(self, dict_: SupportsKeysAndGetItem[str, Any]) -> None: ...
@overload
def update(self, dict_: Iterable[tuple[str, Any]]) -> None: ...
@overload
async def aupdate(self, dict_: SupportsKeysAndGetItem[str, Any]) -> None: ...
@overload
async def aupdate(self, dict_: Iterable[tuple[str, Any]]) -> None: ...
def has_key(self, key: Any) -> bool: ...
def keys(self) -> Any: ...
def values(self) -> Any: ...
def items(self) -> Any: ...
async def ahas_key(self, key: Any) -> bool: ...
async def akeys(self) -> dict_keys[str, Any]: ...
async def avalues(self) -> dict_values[str, Any]: ...
async def aitems(self) -> dict_items[str, Any]: ...
def clear(self) -> None: ...
def is_empty(self) -> bool: ...
def _get_session_key(self) -> str | None: ...
Expand All @@ -36,15 +68,28 @@ class SessionBase(dict[str, Any]):
def _session_key(self, value: str | None) -> None: ...
def get_session_cookie_age(self) -> int: ...
def get_expiry_age(self, **kwargs: Any) -> int: ...
async def aget_expiry_age(self, **kwargs: Any) -> int: ...
def get_expiry_date(self, **kwargs: Any) -> datetime: ...
async def aget_expiry_date(self, **kwargs: Any) -> datetime: ...
def set_expiry(self, value: datetime | timedelta | int | None) -> None: ...
async def aset_expiry(self, value: datetime | timedelta | int | None) -> None: ...
def get_expire_at_browser_close(self) -> bool: ...
async def aget_expire_at_browser_close(self) -> bool: ...
def flush(self) -> None: ...
async def aflush(self) -> None: ...
def cycle_key(self) -> None: ...
async def acycle_key(self) -> None: ...
def exists(self, session_key: str) -> bool: ...
async def aexists(self, session_key: str) -> bool: ...
def create(self) -> None: ...
async def acreate(self) -> None: ...
def save(self, must_create: bool = False) -> None: ...
async def asave(self, must_create: bool = False) -> None: ...
def delete(self, session_key: str | None = None) -> None: ...
async def adelete(self, session_key: str | None = None) -> None: ...
def load(self) -> dict[str, Any]: ...
async def aload(self) -> dict[str, Any]: ...
@classmethod
def clear_expired(cls) -> None: ...
@classmethod
async def aclear_expired(cls) -> None: ...
1 change: 1 addition & 0 deletions django-stubs/contrib/sessions/backends/cache.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ class SessionStore(SessionBase):
def __init__(self, session_key: str | None = None) -> None: ...
@property
def cache_key(self) -> str: ...
async def acache_key(self) -> str: ...
4 changes: 4 additions & 0 deletions django-stubs/contrib/sessions/backends/cached_db.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from logging import Logger
from typing import Any

from django.contrib.sessions.backends.db import SessionStore as DBStore

KEY_PREFIX: str

logger: Logger

class SessionStore(DBStore):
cache_key_prefix: Any
def __init__(self, session_key: str | None = None) -> None: ...
@property
def cache_key(self) -> str: ...
async def acache_key(self) -> str: ...
1 change: 1 addition & 0 deletions django-stubs/contrib/sessions/backends/db.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ class SessionStore(SessionBase):
@cached_property
def model(self) -> type[AbstractBaseSession]: ...
def create_model_instance(self, data: dict[str, Any]) -> AbstractBaseSession: ...
async def acreate_model_instance(self, data: dict[str, Any]) -> AbstractBaseSession: ...
1 change: 0 additions & 1 deletion django-stubs/contrib/sessions/backends/file.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ class SessionStore(SessionBase):
storage_path: str
file_prefix: str
def __init__(self, session_key: str | None = None) -> None: ...
def clean(self) -> None: ...
3 changes: 2 additions & 1 deletion django-stubs/contrib/sessions/backends/signed_cookies.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.sessions.backends.base import SessionBase

class SessionStore(SessionBase): ...
class SessionStore(SessionBase):
async def aexists(self, session_key: str | None = None) -> bool: ...
4 changes: 4 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,7 @@ django.db.migrations.recorder.Migration@AnnotatedWith
django.contrib.auth.backends.UserModel
django.contrib.auth.forms.UserModel
django.contrib.auth.views.UserModel

# Using the definitions from `dict` (from typeshed)
django.contrib.sessions.backends.base.SessionBase.get
django.contrib.sessions.backends.base.SessionBase.setdefault
3 changes: 0 additions & 3 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,6 @@ django.contrib.redirects.models.Redirect.new_path
django.contrib.redirects.models.Redirect.old_path
django.contrib.redirects.models.Redirect.site
django.contrib.redirects.models.Redirect.site_id
django.contrib.sessions.backends.base.SessionBase.get
django.contrib.sessions.backends.base.SessionBase.setdefault
django.contrib.sessions.backends.base.SessionBase.update
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
Expand Down
59 changes: 0 additions & 59 deletions scripts/stubtest/allowlist_todo_django51.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,65 +98,6 @@ django.contrib.postgres.operations.CreateExtension.category
django.contrib.postgres.operations.RemoveCollation.category
django.contrib.postgres.operations.RemoveIndexConcurrently.category
django.contrib.postgres.operations.ValidateConstraint.category
django.contrib.sessions.backends.base.SessionBase.aclear_expired
django.contrib.sessions.backends.base.SessionBase.acreate
django.contrib.sessions.backends.base.SessionBase.acycle_key
django.contrib.sessions.backends.base.SessionBase.adelete
django.contrib.sessions.backends.base.SessionBase.adelete_test_cookie
django.contrib.sessions.backends.base.SessionBase.aexists
django.contrib.sessions.backends.base.SessionBase.aflush
django.contrib.sessions.backends.base.SessionBase.aget
django.contrib.sessions.backends.base.SessionBase.aget_expire_at_browser_close
django.contrib.sessions.backends.base.SessionBase.aget_expiry_age
django.contrib.sessions.backends.base.SessionBase.aget_expiry_date
django.contrib.sessions.backends.base.SessionBase.ahas_key
django.contrib.sessions.backends.base.SessionBase.aitems
django.contrib.sessions.backends.base.SessionBase.akeys
django.contrib.sessions.backends.base.SessionBase.aload
django.contrib.sessions.backends.base.SessionBase.apop
django.contrib.sessions.backends.base.SessionBase.asave
django.contrib.sessions.backends.base.SessionBase.aset
django.contrib.sessions.backends.base.SessionBase.aset_expiry
django.contrib.sessions.backends.base.SessionBase.aset_test_cookie
django.contrib.sessions.backends.base.SessionBase.asetdefault
django.contrib.sessions.backends.base.SessionBase.atest_cookie_worked
django.contrib.sessions.backends.base.SessionBase.aupdate
django.contrib.sessions.backends.base.SessionBase.avalues
django.contrib.sessions.backends.cache.SessionStore.acache_key
django.contrib.sessions.backends.cache.SessionStore.aclear_expired
django.contrib.sessions.backends.cache.SessionStore.acreate
django.contrib.sessions.backends.cache.SessionStore.adelete
django.contrib.sessions.backends.cache.SessionStore.aexists
django.contrib.sessions.backends.cache.SessionStore.aload
django.contrib.sessions.backends.cache.SessionStore.asave
django.contrib.sessions.backends.cached_db.SessionStore.acache_key
django.contrib.sessions.backends.cached_db.SessionStore.adelete
django.contrib.sessions.backends.cached_db.SessionStore.aexists
django.contrib.sessions.backends.cached_db.SessionStore.aflush
django.contrib.sessions.backends.cached_db.SessionStore.aload
django.contrib.sessions.backends.cached_db.SessionStore.asave
django.contrib.sessions.backends.cached_db.logger
django.contrib.sessions.backends.db.SessionStore.aclear_expired
django.contrib.sessions.backends.db.SessionStore.acreate
django.contrib.sessions.backends.db.SessionStore.acreate_model_instance
django.contrib.sessions.backends.db.SessionStore.adelete
django.contrib.sessions.backends.db.SessionStore.aexists
django.contrib.sessions.backends.db.SessionStore.aload
django.contrib.sessions.backends.db.SessionStore.asave
django.contrib.sessions.backends.file.SessionStore.aclear_expired
django.contrib.sessions.backends.file.SessionStore.acreate
django.contrib.sessions.backends.file.SessionStore.adelete
django.contrib.sessions.backends.file.SessionStore.aexists
django.contrib.sessions.backends.file.SessionStore.aload
django.contrib.sessions.backends.file.SessionStore.asave
django.contrib.sessions.backends.file.SessionStore.clean
django.contrib.sessions.backends.signed_cookies.SessionStore.aclear_expired
django.contrib.sessions.backends.signed_cookies.SessionStore.acreate
django.contrib.sessions.backends.signed_cookies.SessionStore.acycle_key
django.contrib.sessions.backends.signed_cookies.SessionStore.adelete
django.contrib.sessions.backends.signed_cookies.SessionStore.aexists
django.contrib.sessions.backends.signed_cookies.SessionStore.aload
django.contrib.sessions.backends.signed_cookies.SessionStore.asave
django.core.checks.templates.E001
django.core.checks.templates.E002
django.core.checks.templates.W003
Expand Down
Loading