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

Django core cache #1774

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion django-stubs/contrib/sessions/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.exceptions import SuspiciousOperation
from django.core.exceptions import BadRequest, SuspiciousOperation

class InvalidSessionKey(SuspiciousOperation): ...
class SuspiciousSession(SuspiciousOperation): ...
class SessionInterrupted(BadRequest): ...
1 change: 1 addition & 0 deletions django-stubs/core/cache/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.utils.connection import BaseConnectionHandler
from .backends.base import BaseCache as BaseCache
from .backends.base import CacheKeyWarning as CacheKeyWarning
from .backends.base import InvalidCacheBackendError as InvalidCacheBackendError
from .backends.base import InvalidCacheKey as InvalidCacheKey

DEFAULT_CACHE_ALIAS: str

Expand Down
5 changes: 3 additions & 2 deletions django-stubs/core/cache/backends/db.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, ClassVar

from django.core.cache.backends.base import BaseCache
from django.utils.functional import _StrOrPromise
Expand All @@ -20,4 +20,5 @@ class BaseDatabaseCache(BaseCache):
cache_model_class: Any
def __init__(self, table: str, params: dict[str, Any]) -> None: ...

class DatabaseCache(BaseDatabaseCache): ...
class DatabaseCache(BaseDatabaseCache):
pickle_protocol: ClassVar[int]
5 changes: 3 additions & 2 deletions django-stubs/core/cache/backends/filebased.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any
from typing import Any, ClassVar

from django.core.cache.backends.base import BaseCache

class FileBasedCache(BaseCache):
cache_suffix: str
cache_suffix: ClassVar[str]
pickle_protocol: ClassVar[int]
def __init__(self, dir: str, params: dict[str, Any]) -> None: ...
4 changes: 3 additions & 1 deletion django-stubs/core/cache/backends/locmem.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any
from typing import Any, ClassVar

from django.core.cache.backends.base import BaseCache

class LocMemCache(BaseCache):
pickle_protocol: ClassVar[int]

def __init__(self, name: str, params: dict[str, Any]) -> None: ...
4 changes: 4 additions & 0 deletions django-stubs/core/cache/backends/memcached.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class BaseMemcachedCache(BaseCache):
library: ModuleType,
value_not_found_exception: type[BaseException],
) -> None: ...
@property
def client_servers(self) -> Sequence[str]: ...
Copy link
Member

Choose a reason for hiding this comment

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

Return types should be exact, no need for abstactions (unless explicitly documented).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thinks it's still correct here.
self._servers is set in the init this way:

        if isinstance(server, str):
            self._servers = re.split("[;,]", server)
        else:
            self._servers = server

And server is annotated as server: str | Sequence[str]. So in the else branch, we could have
a Sequence. PyMemcacheCache(("aa", "bb"), {}) is valid for ex.

However, in the PyLibMCCache override, it always returns list[str].

I don't know if this distinction is worth it tho. I've updated the PR to match runtime types, let me know what you think


class MemcachedCache(BaseMemcachedCache):
def __init__(self, server: str | Sequence[str], params: dict[str, Any]) -> None: ...

class PyLibMCCache(BaseMemcachedCache):
def __init__(self, server: str | Sequence[str], params: dict[str, Any]) -> None: ...
@property
def client_servers(self) -> list[str]: ...

class PyMemcacheCache(BaseMemcachedCache):
def __init__(self, server: str | Sequence[str], params: dict[str, Any]) -> None: ...
2 changes: 1 addition & 1 deletion django-stubs/core/checks/templates.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from django.core.checks.messages import Error, Warning

E001: Error
E002: Error
W001: Warning
W003: Warning

def check_setting_app_dirs_loaders(app_configs: Sequence[AppConfig] | None, **kwargs: Any) -> Sequence[Error]: ...
def check_string_if_invalid_is_string(app_configs: Sequence[AppConfig] | None, **kwargs: Any) -> Sequence[Error]: ...
Expand Down
2 changes: 2 additions & 0 deletions django-stubs/core/exceptions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SuspiciousFileOperation(SuspiciousOperation): ...
class DisallowedHost(SuspiciousOperation): ...
class DisallowedRedirect(SuspiciousOperation): ...
class TooManyFieldsSent(SuspiciousOperation): ...
class TooManyFilesSent(SuspiciousOperation): ...
class RequestDataTooBig(SuspiciousOperation): ...
class RequestAborted(Exception): ...
class BadRequest(Exception): ...
Expand Down Expand Up @@ -48,4 +49,5 @@ class ValidationError(Exception):
def __iter__(self) -> Iterator[tuple[str, list[str]] | str]: ...

class EmptyResultSet(Exception): ...
class FullResultSet(Exception): ...
class SynchronousOnlyOperation(Exception): ...
3 changes: 3 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ django.views.generic.edit.BaseDeleteView.form_class
# We still keep them in stubs to be a bit more backward compatible.
# RemovedInDjango40:
django.middleware.csrf.REASON_BAD_TOKEN

# RemovedInDjango41
django.core.cache.backends.memcached.MemcachedCache
12 changes: 0 additions & 12 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ django.contrib.sessions.base_session.AbstractBaseSession.get_previous_by_expire_
django.contrib.sessions.base_session.AbstractBaseSession.session_data
django.contrib.sessions.base_session.AbstractBaseSession.session_key
django.contrib.sessions.base_session.BaseSessionManager.__slotnames__
django.contrib.sessions.exceptions.SessionInterrupted
django.contrib.sessions.management.commands.clearsessions.Command.handle
django.contrib.sessions.models.Session.expire_date
django.contrib.sessions.models.Session.get_next_by_expire_date
Expand All @@ -818,19 +817,8 @@ django.contrib.staticfiles.finders.FileSystemFinder.__init__
django.contrib.staticfiles.management.commands.collectstatic
django.contrib.staticfiles.management.commands.findstatic.Command.handle_label
django.contrib.staticfiles.storage
django.core.cache.InvalidCacheKey
django.core.cache.backends.db.DatabaseCache.pickle_protocol
django.core.cache.backends.filebased.FileBasedCache.pickle_protocol
django.core.cache.backends.locmem.LocMemCache.pickle_protocol
django.core.cache.backends.memcached.BaseMemcachedCache.client_servers
django.core.cache.backends.memcached.MemcachedCache
django.core.cache.backends.memcached.PyLibMCCache.client_servers
django.core.cache.cache
django.core.checks.registry.CheckRegistry.register
django.core.checks.templates.W001
django.core.checks.templates.W003
django.core.exceptions.FullResultSet
django.core.exceptions.TooManyFilesSent
django.core.files.File.__next__
django.core.files.File.size
django.core.files.base.ContentFile.size
Expand Down