Skip to content

Commit 6000dc3

Browse files
authored
Add @type_check_only to all Protocols and known stubs-only classes (#1894)
1 parent 80f9ad6 commit 6000dc3

33 files changed

+81
-45
lines changed

django-stubs/conf/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Literal
1+
from typing import Any, Literal, type_check_only
22

33
from django.utils.functional import LazyObject
44
from typing_extensions import Self
@@ -11,6 +11,7 @@ DEFAULT_STORAGE_ALIAS: Literal["default"]
1111
STATICFILES_STORAGE_ALIAS: Literal["staticfiles"]
1212

1313
# required for plugin to be able to distinguish this specific instance of LazySettings from others
14+
@type_check_only
1415
class _DjangoConfLazyObject(LazyObject):
1516
def __getattr__(self, item: Any) -> Any: ...
1617

django-stubs/conf/global_settings.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from re import Pattern
33

44
# This is defined here as a do-nothing function because we can't import
55
# django.utils.translation -- that module depends on the settings.
6-
from typing import Any, Literal, Protocol
6+
from typing import Any, Literal, Protocol, type_check_only
77

88
from typing_extensions import TypeAlias
99

@@ -86,6 +86,7 @@ SERVER_EMAIL: str
8686
DATABASES: dict[str, dict[str, Any]]
8787

8888
# Classes used to implement DB routing behavior.
89+
@type_check_only
8990
class Router(Protocol):
9091
def allow_migrate(self, db: str, app_label: str, **hints: Any) -> bool | None: ...
9192

django-stubs/contrib/admin/helpers.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
2-
from typing import Any
2+
from typing import Any, type_check_only
33

44
from django import forms
55
from django.contrib.admin.options import ModelAdmin
@@ -21,6 +21,7 @@ class ActionForm(forms.Form):
2121

2222
checkbox: Any
2323

24+
@type_check_only
2425
class _PrepopulatedDict(TypedDict):
2526
field: BoundField
2627
dependencies: list[BoundField]
@@ -92,6 +93,7 @@ class AdminField:
9293
def label_tag(self) -> SafeString: ...
9394
def errors(self) -> SafeString: ...
9495

96+
@type_check_only
9597
class _FieldDictT(TypedDict):
9698
name: str
9799
label: str

django-stubs/contrib/admin/options.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
2-
from typing import Any, Generic, Literal, TypeVar
2+
from typing import Any, Generic, Literal, TypeVar, type_check_only
33

44
from django import forms
55
from django.contrib.admin.filters import FieldListFilter, ListFilter
@@ -52,10 +52,12 @@ csrf_protect_m: Any
5252

5353
_FieldGroups: TypeAlias = Sequence[str | Sequence[str]]
5454

55+
@type_check_only
5556
class _OptionalFieldOpts(TypedDict, total=False):
5657
classes: Sequence[str]
5758
description: _StrOrPromise
5859

60+
@type_check_only
5961
class _FieldOpts(_OptionalFieldOpts, total=True):
6062
fields: _FieldGroups
6163

django-stubs/contrib/admin/utils.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from collections import defaultdict
33
from collections.abc import Callable, Iterable, Sequence
4-
from typing import Any, Literal, TypeVar, overload
4+
from typing import Any, Literal, TypeVar, overload, type_check_only
55
from uuid import UUID
66

77
from _typeshed import Unused
@@ -59,6 +59,7 @@ class NestedObjects(Collector):
5959
def nested(self, format_callback: Callable[[Model], _T]) -> list[_T]: ...
6060
def can_fast_delete(self, *args: Unused, **kwargs: Unused) -> Literal[False]: ...
6161

62+
@type_check_only
6263
class _ModelFormatDict(TypedDict):
6364
verbose_name: str
6465
verbose_name_plural: str

django-stubs/contrib/auth/password_validation.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from collections.abc import Mapping, Sequence
22
from pathlib import Path, PosixPath
3-
from typing import Any, Protocol
3+
from typing import Any, Protocol, type_check_only
44

55
from django.db.models.base import Model
66
from typing_extensions import TypeAlias
77

88
_UserModel: TypeAlias = Model
99

10+
@type_check_only
1011
class PasswordValidator(Protocol):
1112
def validate(self, __password: str, __user: _UserModel | None = ...) -> None: ...
1213
def get_help_text(self) -> str: ...

django-stubs/contrib/gis/geos/factory.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Any, Protocol
1+
from typing import Any, Protocol, type_check_only
22

33
from django.contrib.gis.geos.geometry import GEOSGeometry
44

5+
@type_check_only
56
class _Reader(Protocol):
67
def read(self) -> str | bytes: ...
78

django-stubs/contrib/gis/utils/layermapping.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import Mapping
22
from pathlib import Path
3-
from typing import Any, Protocol
3+
from typing import Any, Protocol, type_check_only
44

55
from django.contrib.gis.gdal import DataSource, OGRGeomType
66
from django.contrib.gis.gdal.field import Field as OGRField
@@ -14,6 +14,7 @@ class InvalidDecimal(LayerMapError): ...
1414
class InvalidInteger(LayerMapError): ...
1515
class MissingForeignKey(LayerMapError): ...
1616

17+
@type_check_only
1718
class _Writer(Protocol):
1819
def write(self, __s: str) -> Any: ...
1920

django-stubs/core/checks/registry.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Sequence
2-
from typing import Any, Protocol, TypeVar, overload
2+
from typing import Any, Protocol, TypeVar, overload, type_check_only
33

44
from django.apps.config import AppConfig
55
from django.core.checks.messages import CheckMessage
@@ -20,6 +20,7 @@ class Tags:
2020
translation: str
2121
urls: str
2222

23+
@type_check_only
2324
class _CheckCallable(Protocol):
2425
def __call__(
2526
self,
@@ -31,6 +32,7 @@ class _CheckCallable(Protocol):
3132

3233
_C = TypeVar("_C", bound=_CheckCallable)
3334

35+
@type_check_only
3436
class _ProcessedCheckCallable(Protocol[_C]):
3537
tags: Sequence[str]
3638
__call__: _C

django-stubs/core/paginator.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Iterable, Iterator, Sequence, Sized
2-
from typing import ClassVar, Generic, Protocol, TypeVar, overload
2+
from typing import ClassVar, Generic, Protocol, TypeVar, overload, type_check_only
33

44
from django.utils.functional import _StrPromise, cached_property
55

@@ -10,6 +10,7 @@ class EmptyPage(InvalidPage): ...
1010

1111
_T = TypeVar("_T")
1212

13+
@type_check_only
1314
class _SupportsPagination(Protocol[_T], Sized, Iterable):
1415
@overload
1516
def __getitem__(self, __index: int) -> _T: ...

0 commit comments

Comments
 (0)