Skip to content
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
1 change: 1 addition & 0 deletions django-stubs/contrib/admin/sites.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AdminSite:
) -> None: ...
def unregister(self, model_or_iterable: type[Model] | Iterable[type[Model]]) -> None: ...
def is_registered(self, model: type[Model]) -> bool: ...
def get_model_admin(self, model: type[Model]) -> ModelAdmin[Any]: ...
def add_action(self, action: Callable[..., Any], name: str | None = ...) -> None: ...
def disable_action(self, name: str) -> None: ...
def get_action(self, name: str) -> Callable[..., Any]: ...
Expand Down
14 changes: 13 additions & 1 deletion django-stubs/core/files/storage.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import datetime
from typing import IO, Any

from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import File
from django.utils.functional import LazyObject
from django.utils.functional import LazyObject, cached_property

class Storage:
def open(self, name: str, mode: str = ...) -> File: ...
Expand Down Expand Up @@ -45,3 +46,14 @@ class DefaultStorage(LazyObject): ...
default_storage: Storage

def get_storage_class(import_path: str | None = ...) -> type[Storage]: ...

class InvalidStorageError(ImproperlyConfigured): ...

class StorageHandler:
def __init__(self, backends: dict[str, dict[str, Any]] | None = ...) -> None: ...
def __getitem__(self, alias: str) -> Storage: ...
def create_storage(self, params: dict[str, Any]) -> Storage: ...
@cached_property
def backends(self) -> dict[str, dict[str, Any]]: ...

storages: StorageHandler
1 change: 1 addition & 0 deletions django-stubs/http/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from .response import FileResponse as FileResponse
from .response import Http404 as Http404
from .response import HttpResponse as HttpResponse
from .response import HttpResponseBadRequest as HttpResponseBadRequest
from .response import HttpResponseBase as HttpResponseBase
from .response import HttpResponseForbidden as HttpResponseForbidden
from .response import HttpResponseGone as HttpResponseGone
from .response import HttpResponseNotAllowed as HttpResponseNotAllowed
Expand Down
1 change: 1 addition & 0 deletions django-stubs/http/response.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HttpResponseBase:
def __delitem__(self, header: str | bytes) -> None: ...
def __getitem__(self, header: str | bytes) -> str: ...
def has_header(self, header: str) -> bool: ...
def __contains__(self, header: str) -> bool: ...
def items(self) -> Iterable[tuple[str, str]]: ...
@overload
def get(self, header: str | bytes, alternate: str | None) -> str: ...
Expand Down
2 changes: 2 additions & 0 deletions django-stubs/test/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .client import AsyncClient as AsyncClient
from .client import AsyncRequestFactory as AsyncRequestFactory
from .client import Client as Client
from .client import RequestFactory as RequestFactory
from .testcases import LiveServerTestCase as LiveServerTestCase
Expand Down
8 changes: 7 additions & 1 deletion django-stubs/test/testcases.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import threading
import unittest
from collections.abc import Callable, Iterable
from collections.abc import Callable, Generator, Iterable
from contextlib import contextmanager
from types import TracebackType
from typing import Any, ClassVar, TypeVar, overload

Expand Down Expand Up @@ -211,6 +212,11 @@ class TransactionTestCase(SimpleTestCase):
) -> _AssertNumQueriesContext: ...

class TestCase(TransactionTestCase):
@classmethod
@contextmanager
def captureOnCommitCallbacks(
cls, *, using: str = ..., execute: bool = ...
) -> Generator[list[Callable[[], Any]]]: ...
@classmethod
def setUpTestData(cls) -> None: ...

Expand Down
1 change: 1 addition & 0 deletions django-stubs/utils/http.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ def limited_parse_qsl(
fields_limit: int | None = ...,
) -> list[tuple[str, str]]: ...
def escape_leading_slashes(url: str) -> str: ...
def parse_header_parameters(line: str, max_length: int = ...) -> tuple[str, dict[str, str]]: ...
98 changes: 98 additions & 0 deletions tests/pyright/test_missing_symbols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from .base import run_pyright


def test_async_test_client_is_importable() -> None:
"""An async test suite imports AsyncClient from django.test, as the docs show."""
results = run_pyright(
"""\
from django.test import AsyncClient, AsyncRequestFactory

reveal_type(AsyncClient)
reveal_type(AsyncRequestFactory)
"""
)
assert [r for r in results if r.type == "error"] == []


def test_http_response_base_is_importable() -> None:
"""Middleware and view code annotates against HttpResponseBase."""
results = run_pyright(
"""\
from django.http import HttpResponseBase

def handler() -> HttpResponseBase: ...
"""
)
assert [r for r in results if r.type == "error"] == []


def test_capture_on_commit_callbacks() -> None:
"""A test asserts on work deferred with transaction.on_commit."""
results = run_pyright(
"""\
from django.test import TestCase

class Foo(TestCase):
def test_it(self) -> None:
with self.captureOnCommitCallbacks(execute=True) as callbacks:
reveal_type(callbacks)
"""
)
assert [r for r in results if r.type == "error"] == []


def test_storage_handler_is_importable() -> None:
"""Code reads a non-default storage backend by its STORAGES alias."""
results = run_pyright(
"""\
from django.core.files.storage import InvalidStorageError, Storage, storages

def get(alias: str) -> Storage:
try:
return storages[alias]
except InvalidStorageError:
raise
"""
)
assert [r for r in results if r.type == "error"] == []


def test_parse_header_parameters_is_importable() -> None:
"""A request handler splits a Content-Type header into its value and params."""
results = run_pyright(
"""\
from django.utils.http import parse_header_parameters

value, params = parse_header_parameters("text/html; charset=utf-8", max_length=4096)
reveal_type(value)
reveal_type(params)
"""
)
assert [r for r in results if r.type == "error"] == []


def test_admin_site_get_model_admin() -> None:
"""A test asserts which bulk actions a model's admin exposes."""
results = run_pyright(
"""\
from django.contrib.admin.sites import site
from django.contrib.auth.models import User

model_admin = site.get_model_admin(User)
reveal_type(model_admin)
"""
)
assert [r for r in results if r.type == "error"] == []


def test_http_response_supports_in() -> None:
"""A test asserts a header is present with `in`, the way Django documents it."""
results = run_pyright(
"""\
from django.http import HttpResponse

response = HttpResponse()
reveal_type("Retry-After" in response)
"""
)
assert [r for r in results if r.type == "error"] == []
Loading