Skip to content

Commit

Permalink
Update django.middleware.csrf types (#1770)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownPlatypus committed Oct 17, 2023
1 parent 69fceac commit c621edc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
26 changes: 26 additions & 0 deletions django-stubs/middleware/csrf.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
from collections import defaultdict
from collections.abc import Callable
from logging import Logger
from re import Pattern
from typing import Any

from django.http.request import HttpRequest
from django.http.response import HttpResponseBase, HttpResponseForbidden
from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import cached_property

logger: Logger

invalid_token_chars_re: Pattern[str]

REASON_BAD_ORIGIN: str
REASON_NO_REFERER: str
REASON_BAD_REFERER: str
REASON_NO_CSRF_COOKIE: str
REASON_CSRF_TOKEN_MISSING: str
REASON_BAD_TOKEN: str
REASON_MALFORMED_REFERER: str
REASON_INSECURE_REFERER: str

REASON_INCORRECT_LENGTH: str
REASON_INVALID_CHARACTERS: str

CSRF_SECRET_LENGTH: int
CSRF_TOKEN_LENGTH: Any
CSRF_ALLOWED_CHARS: Any
Expand All @@ -21,7 +33,21 @@ CSRF_SESSION_KEY: str
def get_token(request: HttpRequest) -> str: ...
def rotate_token(request: HttpRequest) -> None: ...

class InvalidTokenFormat(Exception):
reason: str
def __init__(self, reason: str) -> None: ...

class RejectRequest(Exception):
reason: str
def __init__(self, reason: str) -> None: ...

class CsrfViewMiddleware(MiddlewareMixin):
@cached_property
def csrf_trusted_origins_hosts(self) -> list[str]: ...
@cached_property
def allowed_origins_exact(self) -> set[str]: ...
@cached_property
def allowed_origin_subdomains(self) -> defaultdict[str, list[str]]: ...
def process_request(self, request: HttpRequest) -> None: ...
def process_view(
self, request: HttpRequest, callback: Callable | None, callback_args: tuple, callback_kwargs: dict[str, Any]
Expand Down
7 changes: 7 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ django.core.validators.slug_re
django.core.validators.slug_unicode_re
django.core.management.commands.makemessages.plural_forms_re
django.http.request.host_validation_re
django.middleware.csrf.invalid_token_chars_re
django.template.base.filter_re
django.template.base.kwarg_re
django.test.client.CONTENT_TYPE_RE
Expand Down Expand Up @@ -103,3 +104,9 @@ django.db.migrations.operations.models.AlterTogetherOptionOperation.option_name
# Attributes defaulting to None messing with mypy
django.views.generic.detail.SingleObjectMixin.model
django.views.generic.edit.BaseDeleteView.form_class

# `error: <...> is not present at runtime`
# This happens often for variables removed in later django version.
# We still keep them in stubs to be a bit more backward compatible.
# RemovedInDjango40:
django.middleware.csrf.REASON_BAD_TOKEN
11 changes: 0 additions & 11 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2158,17 +2158,6 @@ django.middleware.cache.CacheMiddleware.__init__
django.middleware.cache.CacheMiddleware.cache
django.middleware.cache.FetchFromCacheMiddleware.cache
django.middleware.cache.UpdateCacheMiddleware.cache
django.middleware.csrf.CsrfViewMiddleware.allowed_origin_subdomains
django.middleware.csrf.CsrfViewMiddleware.allowed_origins_exact
django.middleware.csrf.CsrfViewMiddleware.csrf_trusted_origins_hosts
django.middleware.csrf.InvalidTokenFormat
django.middleware.csrf.REASON_BAD_ORIGIN
django.middleware.csrf.REASON_BAD_TOKEN
django.middleware.csrf.REASON_CSRF_TOKEN_MISSING
django.middleware.csrf.REASON_INCORRECT_LENGTH
django.middleware.csrf.REASON_INVALID_CHARACTERS
django.middleware.csrf.RejectRequest
django.middleware.csrf.invalid_token_chars_re
django.middleware.gzip.GZipMiddleware.max_random_bytes
django.shortcuts.SupportsGetAbsoluteUrl
django.template.Engine.template_context_processors
Expand Down

0 comments on commit c621edc

Please sign in to comment.