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
18 changes: 11 additions & 7 deletions django-stubs/contrib/postgres/fields/ranges.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Any

from django.db import models
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
from psycopg2.extras import ( # type: ignore[import-untyped] # ty: ignore[unresolved-import]
DateRange,
DateTimeTZRange,
NumericRange,
)

class RangeField(models.Field[Any, Any]):
empty_strings_allowed: bool = ...
Expand All @@ -12,22 +16,22 @@ class RangeField(models.Field[Any, Any]):
def value_to_string(self, obj: Any) -> Any: ...

class IntegerRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[no-any-unimported]

class BigIntegerRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[no-any-unimported]

class DecimalRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[no-any-unimported]

class FloatRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> NumericRange: ... # type: ignore[no-any-unimported]

class DateTimeRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> DateTimeTZRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> DateTimeTZRange: ... # type: ignore[no-any-unimported]

class DateRangeField(RangeField):
def __get__(self, instance: Any, owner: Any) -> DateRange: ... # type: ignore[override]
def __get__(self, instance: Any, owner: Any) -> DateRange: ... # type: ignore[no-any-unimported]

class RangeOperators:
EQUAL: str
Expand Down
164 changes: 68 additions & 96 deletions django-stubs/db/backends/utils.pyi
Original file line number Diff line number Diff line change
@@ -1,117 +1,89 @@
import types
from collections.abc import Iterable, Iterator, Mapping, Sequence
from datetime import date, datetime, time
import datetime
from collections.abc import Iterator, Mapping, Sequence
from contextlib import AbstractContextManager
from decimal import Decimal
from typing import IO, Any, Literal, TypeAlias
from logging import Logger
from types import TracebackType
from typing import Any, Literal, Protocol, TypeAlias, overload, type_check_only
from uuid import UUID

import psycopg2.extensions
from django.db.backends.postgresql.base import DatabaseWrapper
from psycopg2.extensions import Column
from typing_extensions import Self

logger: Any
logger: Logger

# Protocol matching psycopg2.sql.Composable, to avoid depending psycopg2
@type_check_only
class _Composable(Protocol):
def as_string(self, context: Any, /) -> str: ...
def __add__(self, other: Self, /) -> _Composable: ...
def __mul__(self, n: int, /) -> _Composable: ...

_ExecuteQuery: TypeAlias = str | _Composable

# Python types that can be adapted to SQL.
_Mixed: TypeAlias = None | bool | int | float | Decimal | str | bytes | datetime | UUID
_SQLType: TypeAlias = _Mixed | Sequence[_Mixed] | Mapping[str, _Mixed]
_SQLType: TypeAlias = (
None
| bool
| int
| float
| Decimal
| str
| bytes
| datetime.date
| datetime.datetime
| UUID
| tuple[Any, ...]
| list[Any]
)
_ExecuteParameters: TypeAlias = Sequence[_SQLType] | Mapping[str, _SQLType] | None

class CursorWrapper:
cursor: psycopg2.extensions.cursor = ...
db: DatabaseWrapper = ...
def __init__(self, cursor: psycopg2.extensions.cursor, db: DatabaseWrapper) -> None: ...
WRAP_ERROR_ATTRS: Any = ...
cursor: Any
db: Any
def __init__(self, cursor: Any, db: Any) -> None: ...
WRAP_ERROR_ATTRS: Any
APPS_NOT_READY_WARNING_MSG: str

def __getattr__(self, attr: str) -> Any: ...
def __iter__(self) -> Iterator[tuple[Any, ...]]: ...
def __next__(self) -> tuple[Any, ...]: ...
def __enter__(self) -> Self: ...
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
tb: types.TracebackType | None,
type: type[BaseException] | None,
value: BaseException | None,
traceback: TracebackType | None,
) -> None: ...
def callproc(
self,
procname: str,
params: Sequence[_SQLType] | None = ...,
kparams: Mapping[str, int] | None = ...,
) -> None: ...
def execute(
self,
sql: str,
params: Sequence[_SQLType] | Mapping[str, _SQLType] | None = ...,
) -> None: ...
def executemany(
self,
sql: str,
param_list: Sequence[Sequence[_SQLType] | Mapping[str, _SQLType] | None],
) -> None: ...
# copied over from psycopg2 since Django uses __getattr__ to proxy calls
@property
def description(self) -> tuple[Column, ...] | None: ...
def close(self) -> None: ...
@property
def closed(self) -> bool: ...
@property
def connection(self) -> psycopg2.extensions.connection: ...
@property
def name(self) -> str | None: ...
scrollable: bool | None
withhold: bool
def mogrify(
self,
operation: str,
parameters: Sequence[_SQLType] | Mapping[str, _SQLType] | None = ...,
) -> bytes: ...
def setinputsizes(self, sizes: int) -> None: ...
def fetchone(self) -> tuple[Any, ...] | None: ...
def fetchmany(self, size: int = ...) -> list[tuple[Any, ...]]: ...
def fetchall(self) -> list[tuple[Any, ...]]: ...
def scroll(self, value: int, mode: Literal["relative", "absolute"] = ...) -> None: ...
arraysize: int
itersize: int
@property
def rowcount(self) -> int: ...
@property
def rownumber(self) -> int: ...
@property
def lastrowid(self) -> int | None: ...
@property
def query(self) -> str | None: ...
@property
def statusmessage(self) -> str | None: ...
def cast(self, oid: int, s: str) -> Any: ...
tzinfo_factory: Any
def nextset(self) -> None: ...
def setoutputsize(self, size: int, column: int = ...) -> None: ...
def copy_from(
self,
file: IO[str],
table: str,
sep: str = ...,
null: str = ...,
size: int = ...,
columns: Iterable[str] | None = ...,
) -> None: ...
def copy_to(
self,
file: IO[str],
table: str,
sep: str = ...,
null: str = ...,
columns: str | None = ...,
) -> None: ...
def copy_expert(self, sql: str, file: IO[str], size: int = ...) -> None: ...
self, procname: str, params: Sequence[Any] | None = None, kparams: dict[str, int] | None = None
) -> Any: ...
def execute(self, sql: _ExecuteQuery, params: _ExecuteParameters | None = None) -> Any: ...
def executemany(self, sql: _ExecuteQuery, param_list: Sequence[_ExecuteParameters]) -> Any: ...

class CursorDebugWrapper(CursorWrapper):
cursor: Any
db: Any
def debug_sql(
self,
sql: str | None = None,
params: _ExecuteParameters | Sequence[_ExecuteParameters] | None = None,
use_last_executed_query: bool = False,
many: bool = False,
) -> AbstractContextManager[None]: ...

def typecast_date(s: str | None) -> date | None: ...
def typecast_time(s: str | None) -> time | None: ...
def typecast_timestamp(s: str | None) -> date | None: ...
def rev_typecast_decimal(d: Decimal) -> str: ...
def debug_transaction(connection: Any, sql: str) -> AbstractContextManager[None]: ...
def split_tzname_delta(tzname: str) -> tuple[str, str | None, str | None]: ...
@overload
def typecast_date(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap]
@overload
def typecast_date(s: str) -> datetime.date: ...
@overload
def typecast_time(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap]
@overload
def typecast_time(s: str) -> datetime.time: ...
@overload
def typecast_timestamp(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap]
@overload
def typecast_timestamp(s: str) -> datetime.datetime: ...
def split_identifier(identifier: str) -> tuple[str, str]: ...
def truncate_name(identifier: str, length: int | None = ..., hash_len: int = ...) -> str: ...
def truncate_name(identifier: str, length: int | None = None, hash_len: int = 4) -> str: ...
def names_digest(*args: str, length: int) -> str: ...
def format_number(value: Decimal | None, max_digits: int | None, decimal_places: int | None) -> str | None: ...
def strip_quotes(table_name: str) -> str: ...
2 changes: 2 additions & 0 deletions django-stubs/db/models/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ from .lookups import Transform as Transform
from .manager import Manager as Manager
from .query import Prefetch as Prefetch
from .query import QuerySet as QuerySet
from .query import aprefetch_related_objects as aprefetch_related_objects
from .query import prefetch_related_objects as prefetch_related_objects
from .query_utils import FilteredRelation as FilteredRelation
from .query_utils import Q as Q
Expand Down Expand Up @@ -192,6 +193,7 @@ __all__ = [
"When",
"Window",
"WindowFrame",
"aprefetch_related_objects",
"prefetch_related_objects",
"signals",
]
3 changes: 1 addition & 2 deletions django-stubs/db/models/expressions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ from typing import Any, TypeAlias
from django.db.models import Q, QuerySet
from django.db.models.fields import Field
from django.db.models.lookups import Lookup
from django.db.models.query import _BaseQuerySet
from django.db.models.sql.compiler import SQLCompiler
from typing_extensions import Self

Expand Down Expand Up @@ -134,7 +133,7 @@ class Subquery(Expression):
template: str = ...
queryset: QuerySet[Any] = ...
extra: dict[Any, Any] = ...
def __init__(self, queryset: _BaseQuerySet[Any], output_field: _OutputField | None = ..., **extra: Any) -> None: ...
def __init__(self, queryset: QuerySet[Any], output_field: _OutputField | None = ..., **extra: Any) -> None: ...

class Exists(Subquery):
negated: bool = ...
Expand Down
36 changes: 17 additions & 19 deletions django-stubs/db/models/manager.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Callable, Iterable, MutableMapping
from collections.abc import Callable, Iterable, Mapping
from typing import Any, Generic, TypeVar

from django.db.models.base import Model
Expand Down Expand Up @@ -62,12 +62,12 @@ class ManyToManyRelatedManager(Manager[_T], Generic[_T, _V]):
def add(
self,
*objs: QuerySet[_T] | _T | _V,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
) -> None: ...
async def aadd(
self,
*objs: QuerySet[_T] | _T | _V,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
) -> None: ...
def remove(self, *objs: QuerySet[_T] | _T | _V) -> None: ...
async def aremove(self, *objs: QuerySet[_T] | _T | _V) -> None: ...
Expand All @@ -76,55 +76,53 @@ class ManyToManyRelatedManager(Manager[_T], Generic[_T, _V]):
objs: QuerySet[_T] | Iterable[_T],
*,
clear: bool = ...,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
) -> None: ...
async def aset(
self,
objs: QuerySet[_T] | Iterable[_T],
*,
clear: bool = ...,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
) -> None: ...
def clear(self) -> None: ...
async def aclear(self) -> None: ...
def create(
self,
defaults: MutableMapping[str, Any] | None = ...,
through_defaults: MutableMapping[str, Any] | None = ...,
defaults: Mapping[str, Any] | None = ...,
through_defaults: Mapping[str, Any] | None = ...,
**kwargs: Any,
) -> _T: ...
async def acreate(
self,
defaults: MutableMapping[str, Any] | None = ...,
through_defaults: MutableMapping[str, Any] | None = ...,
defaults: Mapping[str, Any] | None = ...,
through_defaults: Mapping[str, Any] | None = ...,
**kwargs: Any,
) -> _T: ...
def get_or_create(
self,
defaults: MutableMapping[str, Any] | None = ...,
defaults: Mapping[str, Any] | None = ...,
*,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
**kwargs: Any,
) -> tuple[_T, bool]: ...
async def aget_or_create(
self,
defaults: MutableMapping[str, Any] | None = ...,
defaults: Mapping[str, Any] | None = ...,
*,
through_defaults: MutableMapping[str, Any] = ...,
through_defaults: Mapping[str, Any] = ...,
**kwargs: Any,
) -> tuple[_T, bool]: ...
def update_or_create(
self,
defaults: MutableMapping[str, Any] | None = ...,
*,
through_defaults: MutableMapping[str, Any] = ...,
defaults: Mapping[str, Any] | None = None,
create_defaults: Mapping[str, Any] | None = None,
**kwargs: Any,
) -> tuple[_T, bool]: ...
async def aupdate_or_create(
self,
defaults: MutableMapping[str, Any] | None = ...,
*,
through_defaults: MutableMapping[str, Any] = ...,
defaults: Mapping[str, Any] | None = None,
create_defaults: Mapping[str, Any] | None = None,
**kwargs: Any,
) -> tuple[_T, bool]: ...

Expand Down
Loading
Loading