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
39 changes: 25 additions & 14 deletions django-stubs/db/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from typing import Any

from django.db.backends.utils import CursorWrapper

from . import migrations as migrations
from .utils import DEFAULT_DB_ALIAS as DEFAULT_DB_ALIAS
from .backends.base.base import BaseDatabaseWrapper
from .utils import DEFAULT_DB_ALIAS as DEFAULT_DB_ALIAS # Not exported in __all__
from .utils import DJANGO_VERSION_PICKLE_KEY as DJANGO_VERSION_PICKLE_KEY
from .utils import ConnectionDoesNotExist as ConnectionDoesNotExist
from .utils import ConnectionHandler as ConnectionHandler
from .utils import ConnectionHandler, ConnectionRouter
from .utils import DatabaseError as DatabaseError
from .utils import DataError as DataError
from .utils import Error as Error
Expand All @@ -18,14 +15,28 @@ from .utils import OperationalError as OperationalError
from .utils import ProgrammingError as ProgrammingError

connections: ConnectionHandler
router: Any
connection: DefaultConnectionProxy

class DefaultConnectionProxy:
def cursor(self) -> CursorWrapper: ...
def __getattr__(self, item: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __delattr__(self, name: str) -> None: ...
router: ConnectionRouter
# Actually ConnectionProxy, but quacks exactly like BaseDatabaseWrapper, it's not worth distinguishing the two.
connection: BaseDatabaseWrapper

def close_old_connections(**kwargs: Any) -> None: ...
def reset_queries(**kwargs: Any) -> None: ...

__all__ = [
"DEFAULT_DB_ALIAS",
"DJANGO_VERSION_PICKLE_KEY",
"DataError",
"DatabaseError",
"Error",
"IntegrityError",
"InterfaceError",
"InternalError",
"NotSupportedError",
"OperationalError",
"ProgrammingError",
"close_old_connections",
"connection",
"connections",
"reset_queries",
"router",
]
3 changes: 1 addition & 2 deletions django-stubs/db/backends/base/operations.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ from decimal import Decimal
from typing import Any, TypeAlias

from django.core.management.color import Style
from django.db import DefaultConnectionProxy
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.utils import CursorWrapper
from django.db.models.base import Model
from django.db.models.expressions import Case, Expression
from django.db.models.fields import Field
from django.db.models.sql.compiler import SQLCompiler

_Connection: TypeAlias = DefaultConnectionProxy | BaseDatabaseWrapper
_Connection: TypeAlias = BaseDatabaseWrapper

class BaseDatabaseOperations:
compiler_module: str = ...
Expand Down
45 changes: 21 additions & 24 deletions django-stubs/db/migrations/executor.pyi
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
from collections.abc import Callable
from typing import Any
from collections.abc import Sequence
from typing import Protocol, type_check_only

from django.db import DefaultConnectionProxy
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.migrations.migration import Migration

from .loader import MigrationLoader
from .recorder import MigrationRecorder
from .state import ProjectState

@type_check_only
class _ProgressCallbackT(Protocol):
def __call__(self, action: str, migration: Migration | None = ..., fake: bool | None = ..., /) -> None: ...

class MigrationExecutor:
connection: Any = ...
loader: MigrationLoader = ...
recorder: MigrationRecorder = ...
progress_callback: Callable[..., Any] = ...
connection: BaseDatabaseWrapper
loader: MigrationLoader
recorder: MigrationRecorder
progress_callback: _ProgressCallbackT | None
def __init__(
self,
connection: DefaultConnectionProxy | BaseDatabaseWrapper | None,
progress_callback: Callable[..., Any] | None = ...,
connection: BaseDatabaseWrapper | None,
progress_callback: _ProgressCallbackT | None = None,
) -> None: ...
def migration_plan(
self,
targets: list[tuple[str, str | None]] | set[tuple[str, str]],
clean_start: bool = ...,
self, targets: Sequence[tuple[str, str | None]] | set[tuple[str, str]], clean_start: bool = False
) -> list[tuple[Migration, bool]]: ...
def migrate(
self,
targets: list[tuple[str, str | None]] | None,
plan: list[tuple[Migration, bool]] | None = ...,
state: ProjectState | None = ...,
fake: bool = ...,
fake_initial: bool = ...,
targets: Sequence[tuple[str, str | None]] | None,
plan: Sequence[tuple[Migration, bool]] | None = None,
state: ProjectState | None = None,
fake: bool = False,
fake_initial: bool = False,
) -> ProjectState: ...
def collect_sql(self, plan: list[tuple[Migration, bool]]) -> list[str]: ...
def apply_migration(
self,
state: ProjectState,
migration: Migration,
fake: bool = ...,
fake_initial: bool = ...,
self, state: ProjectState, migration: Migration, fake: bool = False, fake_initial: bool = False
) -> ProjectState: ...
def unapply_migration(self, state: ProjectState, migration: Migration, fake: bool = ...) -> ProjectState: ...
def record_migration(self, app_label: str, name: str, forward: bool = True) -> None: ...
def unapply_migration(self, state: ProjectState, migration: Migration, fake: bool = False) -> ProjectState: ...
def check_replacements(self) -> None: ...
def detect_soft_applied(
self, project_state: ProjectState | None, migration: Migration
Expand Down
12 changes: 12 additions & 0 deletions django-stubs/db/models/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from django.core.exceptions import ObjectDoesNotExist as ObjectDoesNotExist
from . import lookups as lookups
from . import signals as signals
from .aggregates import Aggregate as Aggregate
from .aggregates import AnyValue as AnyValue
from .aggregates import Avg as Avg
from .aggregates import Count as Count
from .aggregates import Max as Max
Expand All @@ -11,6 +12,7 @@ from .aggregates import StdDev as StdDev
from .aggregates import StringAgg as StringAgg
from .aggregates import Sum as Sum
from .aggregates import Variance as Variance
from .base import DEFERRED as DEFERRED
from .base import Model as Model
from .constraints import BaseConstraint as BaseConstraint
from .constraints import CheckConstraint as CheckConstraint
Expand Down Expand Up @@ -44,6 +46,7 @@ from .expressions import ValueRange as ValueRange
from .expressions import When as When
from .expressions import Window as Window
from .expressions import WindowFrame as WindowFrame
from .expressions import WindowFrameExclusion as WindowFrameExclusion
from .fields import BLANK_CHOICE_DASH as BLANK_CHOICE_DASH
from .fields import NOT_PROVIDED as NOT_PROVIDED
from .fields import AutoField as AutoField
Expand All @@ -52,17 +55,20 @@ from .fields import BigIntegerField as BigIntegerField
from .fields import BinaryField as BinaryField
from .fields import BooleanField as BooleanField
from .fields import CharField as CharField
from .fields import CommaSeparatedIntegerField as CommaSeparatedIntegerField
from .fields import DateField as DateField
from .fields import DateTimeField as DateTimeField
from .fields import DecimalField as DecimalField
from .fields import DurationField as DurationField
from .fields import EmailField as EmailField
from .fields import Empty as Empty
from .fields import Field as Field
from .fields import FilePathField as FilePathField
from .fields import FloatField as FloatField
from .fields import GenericIPAddressField as GenericIPAddressField
from .fields import IntegerField as IntegerField
from .fields import IPAddressField as IPAddressField
from .fields import NullBooleanField as NullBooleanField
from .fields import PositiveBigIntegerField as PositiveBigIntegerField
from .fields import PositiveIntegerField as PositiveIntegerField
from .fields import PositiveSmallIntegerField as PositiveSmallIntegerField
Expand Down Expand Up @@ -101,6 +107,7 @@ from .query_utils import Q as Q
__all__ = [
"BLANK_CHOICE_DASH",
"CASCADE",
"DEFERRED",
"DO_NOTHING",
"NOT_PROVIDED",
"PROTECT",
Expand All @@ -109,6 +116,7 @@ __all__ = [
"SET_DEFAULT",
"SET_NULL",
"Aggregate",
"AnyValue",
"AutoField",
"Avg",
"BaseConstraint",
Expand All @@ -120,6 +128,7 @@ __all__ = [
"CharField",
"CheckConstraint",
"Choices",
"CommaSeparatedIntegerField",
"CompositePrimaryKey",
"Count",
"DateField",
Expand All @@ -128,6 +137,7 @@ __all__ = [
"Deferrable",
"DurationField",
"EmailField",
"Empty",
"Exists",
"Expression",
"ExpressionList",
Expand Down Expand Up @@ -158,6 +168,7 @@ __all__ = [
"Max",
"Min",
"Model",
"NullBooleanField",
"ObjectDoesNotExist",
"OneToOneField",
"OneToOneRel",
Expand Down Expand Up @@ -193,6 +204,7 @@ __all__ = [
"When",
"Window",
"WindowFrame",
"WindowFrameExclusion",
"aprefetch_related_objects",
"prefetch_related_objects",
"signals",
Expand Down
99 changes: 88 additions & 11 deletions django-stubs/db/models/aggregates.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,98 @@
from typing import Any
from collections.abc import Sequence
from typing import Any, ClassVar

from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.expressions import Combinable, Func
from django.db.models.fields import IntegerField
from django.db.models.functions.mixins import FixDurationInputMixin, NumericOutputFieldMixin
from django.db.models.query import _OrderByFieldName
from django.db.models.query_utils import Q
from django.db.models.sql.compiler import SQLCompiler, _AsSqlType
from typing_extensions import override

class Aggregate(Func):
filter_template: str = ...
filter: Any = ...
allow_distinct: bool = ...
def __init__(self, *expressions: Any, distinct: bool = ..., filter: Any | None = ..., **extra: Any) -> None: ...
name: str
filter: Any
allow_distinct: bool
allow_order_by: bool
empty_result_set_value: int | None
def __init__(
self,
*expressions: Any,
distinct: bool = False,
filter: Q | None = None,
default: Any | None = None,
order_by: _OrderByFieldName | Sequence[_OrderByFieldName] | None = None,
**extra: Any,
) -> None: ...
@property
def default_alias(self) -> str: ...
@override
def as_sql( # type: ignore[override]
self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, **extra_context: Any
) -> _AsSqlType: ...

class AnyValue(Aggregate):
@override
def as_sql( # type: ignore[override]
self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, **extra_context: Any
) -> _AsSqlType: ...

class Avg(FixDurationInputMixin, NumericOutputFieldMixin, Aggregate): ...

class Count(Aggregate):
output_field: ClassVar[IntegerField[Any]]
def __init__(
self,
expression: Combinable | str,
filter: Q | None = None,
*,
distinct: bool = False,
**extra: Any,
) -> None: ...

class Avg(Aggregate): ...
class Count(Aggregate): ...
class Max(Aggregate): ...
class Min(Aggregate): ...
class StdDev(Aggregate): ...

class StdDev(NumericOutputFieldMixin, Aggregate):
def __init__(
self,
expression: Combinable | str,
sample: bool = False,
*,
filter: Q | None = None,
default: Any | None = None,
**extra: Any,
) -> None: ...

class StringAgg(Aggregate):
def __init__(self, expression: Combinable | str, delimiter: Combinable | str, **extra: Any) -> None: ...
def __init__(
self,
expression: Combinable | str,
delimiter: str | Combinable,
*,
distinct: bool = False,
filter: Q | None = None,
default: Any | None = None,
order_by: _OrderByFieldName | Sequence[_OrderByFieldName] | None = None,
**extra: Any,
) -> None: ...
def as_oracle(self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, **extra_context: Any) -> _AsSqlType: ...
def as_mysql(self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, **extra_context: Any) -> _AsSqlType: ...
@override
def as_sqlite(self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, **extra_context: Any) -> _AsSqlType: ... # type: ignore[override]

class Sum(FixDurationInputMixin, Aggregate): ...

class Variance(NumericOutputFieldMixin, Aggregate):
def __init__(
self,
expression: Combinable | str,
sample: bool = False,
*,
filter: Q | None = None,
default: Any | None = None,
**extra: Any,
) -> None: ...

class Sum(Aggregate): ...
class Variance(Aggregate): ...
__all__ = ["Aggregate", "AnyValue", "Avg", "Count", "Max", "Min", "StdDev", "StringAgg", "Sum", "Variance"]
4 changes: 4 additions & 0 deletions django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ from django.db.models.options import Options
from django.db.models.query import QuerySet
from typing_extensions import Self

class Deferred: ...

DEFERRED: Deferred

class ModelStateFieldsCacheDescriptor: ...

class ModelState:
Expand Down
7 changes: 7 additions & 0 deletions django-stubs/db/models/expressions.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Callable, Iterable, Iterator, Sequence
from datetime import datetime, timedelta
from decimal import Decimal
from enum import Enum
from typing import Any, TypeAlias

from django.db.models import Q, QuerySet
Expand Down Expand Up @@ -225,6 +226,12 @@ class Window(Expression):
output_field: _OutputField | None = ...,
) -> None: ...

class WindowFrameExclusion(Enum):
CURRENT_ROW = "CURRENT ROW"
GROUP = "GROUP"
TIES = "TIES"
NO_OTHERS = "NO OTHERS"

class WindowFrame(Expression):
template: str = ...
frame_type: str = ...
Expand Down
5 changes: 5 additions & 0 deletions django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ _ST = TypeVar("_ST")
# __get__ return type
_GT = TypeVar("_GT")

class Empty: ...
class NOT_PROVIDED: ...

class Field(RegisterLookupMixin, Generic[_ST, _GT]):
Expand Down Expand Up @@ -761,6 +762,8 @@ class CharField(Field[_C | Combinable, _C], Generic[_C]):
error_messages: _ErrorMessagesToOverride | None = ...,
) -> CharField[str | None]: ...

class CommaSeparatedIntegerField(CharField[_C]): ...

class SlugField(CharField[_C]):
@overload
def __new__(
Expand Down Expand Up @@ -1065,6 +1068,8 @@ class BooleanField(Field[_B | Combinable, _B], Generic[_B]):
error_messages: _ErrorMessagesToOverride | None = ...,
) -> BooleanField[bool | None]: ...

NullBooleanField: TypeAlias = BooleanField[bool | None]

class IPAddressField(Field[_C | Combinable, _C], Generic[_C]):
@overload
def __new__(
Expand Down
Loading
Loading