Skip to content

Commit

Permalink
Improve Collector and NestedObjects attributes and methods (#1826)
Browse files Browse the repository at this point in the history
* Improve Collector and NestedObjects class attributes, methods
  • Loading branch information
intgr authored Nov 7, 2023
1 parent b20807d commit 9c6e239
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
37 changes: 25 additions & 12 deletions django-stubs/contrib/admin/utils.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import datetime
from collections import defaultdict
from collections.abc import Callable, Iterable, Sequence
from typing import Any, Literal, overload
from typing import Any, Literal, TypeVar, overload
from uuid import UUID

from _typeshed import Unused
from django.contrib.admin.options import BaseModelAdmin
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.forms import AdminPasswordChangeForm
Expand All @@ -17,6 +19,8 @@ from django.http.request import HttpRequest
from django.utils.datastructures import _IndexableCollection
from typing_extensions import TypedDict

_T = TypeVar("_T")

class FieldIsAForeignKeyColumnName(Exception): ...

def lookup_spawns_duplicates(opts: Options, lookup_path: str) -> bool: ...
Expand All @@ -30,21 +34,30 @@ def get_deleted_objects(
) -> tuple[list[str], dict[str, int], set[str], list[str]]: ...

class NestedObjects(Collector):
data: dict[type[Model], set[Model] | list[Model]]
dependencies: dict[Any, Any]
fast_deletes: list[Any]
field_updates: dict[Any, Any]
using: str
edges: Any
protected: Any
model_objs: Any
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
edges: dict[Model, list[Model]]
protected: set[Model]
model_objs: defaultdict[str, set[Model]]
def add_edge(self, source: Model | None, target: Model) -> None: ...
def collect( # type: ignore[override]
self,
objs: _IndexableCollection[Model | None],
source: type[Model] | None = ...,
source_attr: str | None = ...,
*,
nullable: bool = ...,
collect_related: bool = ...,
reverse_dependency: bool = ...,
keep_parents: bool = ...,
fail_on_restricted: bool = ...,
) -> None: ...
def related_objects(
self, related_model: type[Model], related_fields: Iterable[Field], objs: _IndexableCollection[Model]
) -> QuerySet[Model]: ...
def nested(self, format_callback: Callable = ...) -> list[Any]: ...
def can_fast_delete(self, *args: Any, **kwargs: Any) -> bool: ...
@overload
def nested(self, format_callback: None = None) -> list[Model]: ...
@overload
def nested(self, format_callback: Callable[[Model], _T]) -> list[_T]: ...
def can_fast_delete(self, *args: Unused, **kwargs: Unused) -> Literal[False]: ...

class _ModelFormatDict(TypedDict):
verbose_name: str
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
from typing import Any
from typing import Literal

from _typeshed import Unused
from django.core.management import BaseCommand
from django.db.models.base import Model
from django.db.models.deletion import Collector

class Command(BaseCommand): ...

class NoFastDeleteCollector(Collector):
data: dict[type[Model], set[Model] | list[Model]]
dependencies: dict[Any, Any]
fast_deletes: list[Any]
field_updates: dict[Any, Any]
using: str
def can_fast_delete(self, *args: Any, **kwargs: Any) -> bool: ...
def can_fast_delete(self, *args: Unused, **kwargs: Unused) -> Literal[False]: ...
12 changes: 7 additions & 5 deletions django-stubs/db/models/deletion.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from collections.abc import Callable, Iterable, Iterator, Sequence
from typing import Any

Expand Down Expand Up @@ -57,12 +58,13 @@ class RestrictedError(IntegrityError):

class Collector:
using: str
origin: Model | QuerySet[Model] | None
data: dict[type[Model], set[Model] | list[Model]]
field_updates: Any
restricted_objects: Any
fast_deletes: Any
dependencies: Any
def __init__(self, using: str) -> None: ...
field_updates: defaultdict[tuple[Field, Any], list[Model]]
restricted_objects: defaultdict[Model, defaultdict[Field, set[Model]]]
fast_deletes: list[QuerySet[Model]]
dependencies: defaultdict[Model, set[Model]]
def __init__(self, using: str, origin: Model | QuerySet[Model] | None = None) -> None: ...
def add(
self,
objs: _IndexableCollection[Model],
Expand Down
3 changes: 0 additions & 3 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ django.contrib.admin.templatetags.admin_list.DOT
django.contrib.admin.tests.AdminSeleniumTestCase
django.contrib.admin.tests.AdminSeleniumTestCase.assertCountSeleniumElements
django.contrib.admin.tests.CSPMiddleware.process_response
django.contrib.admin.utils.NestedObjects.collect
django.contrib.admin.utils.NestedObjects.nested
django.contrib.admin.utils.QUOTE_MAP
django.contrib.admin.utils.UNQUOTE_MAP
django.contrib.admin.utils.prepare_lookup_value
Expand Down Expand Up @@ -1169,7 +1167,6 @@ django.db.models.constraints.CheckConstraint.validate
django.db.models.constraints.UniqueConstraint.__init__
django.db.models.constraints.UniqueConstraint.contains_expressions
django.db.models.constraints.UniqueConstraint.validate
django.db.models.deletion.Collector.__init__
django.db.models.expressions.BaseExpression.empty_result_set_value
django.db.models.expressions.BaseExpression.get_refs
django.db.models.expressions.BaseExpression.output_field
Expand Down

0 comments on commit 9c6e239

Please sign in to comment.