Skip to content

Commit 389d986

Browse files
GabDugadamchainz
andauthored
4.2 typings for db.migrations.operations (#1583)
* fix: misc type fixes against 4.2 * chore: type config * chore: improve typings for migrations.operations * chore: remove decorator generics * chore: rollback some changes * chore: fix allowlist for CI * Update django-stubs/db/migrations/state.pyi Co-authored-by: Adam Johnson <[email protected]> --------- Co-authored-by: Adam Johnson <[email protected]>
1 parent 7ce5692 commit 389d986

File tree

16 files changed

+94
-121
lines changed

16 files changed

+94
-121
lines changed

django-stubs/apps/config.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ from collections.abc import Iterator
33

44
from django.apps.registry import Apps
55
from django.db.models.base import Model
6-
from django.utils.functional import _StrOrPromise
6+
from django.utils.functional import _StrOrPromise, cached_property
77

8+
APPS_MODULE_NAME: str
89
MODELS_MODULE_NAME: str
910

1011
class AppConfig:
@@ -17,7 +18,8 @@ class AppConfig:
1718
models_module: str | None
1819
models: dict[str, type[Model]]
1920
def __init__(self, app_name: str, app_module: types.ModuleType | None) -> None: ...
20-
default_auto_field: str
21+
@cached_property
22+
def default_auto_field(self) -> str: ...
2123
@classmethod
2224
def create(cls, entry: str) -> AppConfig: ...
2325
def get_model(self, model_name: str, require_ready: bool = ...) -> type[Model]: ...

django-stubs/contrib/admin/views/main.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ from django.http.request import HttpRequest
1212

1313
ALL_VAR: str
1414
ORDER_VAR: str
15-
ORDER_TYPE_VAR: str
1615
PAGE_VAR: str
1716
SEARCH_VAR: str
1817
ERROR_FLAG: str

django-stubs/contrib/contenttypes/management/__init__.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ from django.db.migrations.state import StateApps
1010
from django.db.models.base import Model
1111

1212
class RenameContentType(migrations.RunPython):
13-
app_label: Any
14-
old_model: Any
15-
new_model: Any
13+
app_label: str
14+
old_model: str
15+
new_model: str
1616
def __init__(self, app_label: str, old_model: str, new_model: str) -> None: ...
1717
def rename_forward(self, apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: ...
1818
def rename_backward(self, apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: ...
1919

2020
def inject_rename_contenttypes_operations(
21-
plan: list[tuple[Migration, bool]] = ..., apps: StateApps = ..., using: str = ..., **kwargs: Any
21+
plan: list[tuple[Migration, bool]] | None = ..., apps: StateApps = ..., using: str = ..., **kwargs: Any
2222
) -> None: ...
2323
def get_contenttypes_and_models(
2424
app_config: AppConfig, using: str, ContentType: type[ContentType]

django-stubs/db/migrations/operations/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from .models import AlterIndexTogether as AlterIndexTogether
88
from .models import AlterModelManagers as AlterModelManagers
99
from .models import AlterModelOptions as AlterModelOptions
1010
from .models import AlterModelTable as AlterModelTable
11+
from .models import AlterModelTableComment as AlterModelTableComment
1112
from .models import AlterOrderWithRespectTo as AlterOrderWithRespectTo
1213
from .models import AlterUniqueTogether as AlterUniqueTogether
1314
from .models import CreateModel as CreateModel

django-stubs/db/migrations/operations/base.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from django.db.backends.base.base import BaseDatabaseWrapper
55
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
66
from django.db.migrations.state import ProjectState
77
from django.db.models import Model
8+
from typing_extensions import Self
89

910
class Operation:
1011
reversible: bool
@@ -13,6 +14,7 @@ class Operation:
1314
elidable: bool
1415
serialization_expand_args: list[str]
1516
_constructor_args: tuple[Sequence[Any], dict[str, Any]]
17+
def __new__(cls, *args: Any, **kwargs: Any) -> Self: ...
1618
def deconstruct(self) -> tuple[str, Sequence[Any], dict[str, Any]]: ...
1719
def state_forwards(self, app_label: str, state: ProjectState) -> None: ...
1820
def database_forwards(

django-stubs/db/migrations/operations/fields.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from django.db.models.fields import Field
2+
from django.utils.functional import cached_property
23

34
from .base import Operation
45

56
class FieldOperation(Operation):
67
model_name: str
78
name: str
89
def __init__(self, model_name: str, name: str, field: Field | None = ...) -> None: ...
9-
@property
10+
@cached_property
1011
def name_lower(self) -> str: ...
11-
@property
12+
@cached_property
1213
def model_name_lower(self) -> str: ...
1314
def is_same_model_operation(self, operation: FieldOperation) -> bool: ...
1415
def is_same_field_operation(self, operation: FieldOperation) -> bool: ...
@@ -29,7 +30,7 @@ class RenameField(FieldOperation):
2930
old_name: str
3031
new_name: str
3132
def __init__(self, model_name: str, old_name: str, new_name: str) -> None: ...
32-
@property
33+
@cached_property
3334
def old_name_lower(self) -> str: ...
34-
@property
35+
@cached_property
3536
def new_name_lower(self) -> str: ...

django-stubs/db/migrations/operations/models.pyi

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ from django.db.models.fields import Field
99
from django.db.models.indexes import Index
1010
from django.db.models.manager import Manager
1111
from django.db.models.options import _OptionTogetherT
12+
from django.utils.functional import cached_property
1213

1314
class ModelOperation(Operation):
1415
name: str
1516
def __init__(self, name: str) -> None: ...
16-
@property
17+
@cached_property
1718
def name_lower(self) -> str: ...
19+
def can_reduce_through(self, operation: Operation, app_label: str) -> bool: ...
1820

1921
class CreateModel(ModelOperation):
2022
fields: list[tuple[str, Field]]
@@ -36,9 +38,9 @@ class RenameModel(ModelOperation):
3638
old_name: str
3739
new_name: str
3840
def __init__(self, old_name: str, new_name: str) -> None: ...
39-
@property
41+
@cached_property
4042
def old_name_lower(self) -> str: ...
41-
@property
43+
@cached_property
4244
def new_name_lower(self) -> str: ...
4345

4446
class ModelOptionOperation(ModelOperation): ...
@@ -47,14 +49,29 @@ class AlterModelTable(ModelOptionOperation):
4749
table: str | None
4850
def __init__(self, name: str, table: str | None) -> None: ...
4951

52+
class AlterModelTableComment(ModelOptionOperation):
53+
table_comment: str
54+
def __init__(self, name: str, table_comment: str) -> None: ...
55+
def deconstruct(self) -> tuple[str, Sequence[Any], dict[str, Any]]: ...
56+
def state_forwards(self, app_label: str, state: Any) -> None: ...
57+
def database_forwards(
58+
self, app_label: str, schema_editor: BaseDatabaseSchemaEditor, from_state: Any, to_state: Any
59+
) -> None: ...
60+
def database_backwards(
61+
self, app_label: str, schema_editor: BaseDatabaseSchemaEditor, from_state: Any, to_state: Any
62+
) -> None: ...
63+
def describe(self) -> str: ...
64+
@property
65+
def migration_name_fragment(self) -> str: ...
66+
5067
class AlterTogetherOptionOperation(ModelOptionOperation):
5168
option_name: str
5269
def __init__(
5370
self,
5471
name: str,
5572
option_value: _OptionTogetherT | None,
5673
) -> None: ...
57-
@property
74+
@cached_property
5875
def option_value(self) -> set[tuple[str, ...]] | None: ...
5976
def deconstruct(self) -> tuple[str, Sequence[Any], dict[str, Any]]: ...
6077
def state_forwards(self, app_label: str, state: Any) -> None: ...
@@ -80,6 +97,7 @@ class AlterIndexTogether(AlterTogetherOptionOperation):
8097
def __init__(self, name: str, index_together: _OptionTogetherT | None) -> None: ...
8198

8299
class AlterOrderWithRespectTo(ModelOptionOperation):
100+
option_name: str
83101
order_with_respect_to: str
84102
def __init__(self, name: str, order_with_respect_to: str) -> None: ...
85103

@@ -94,7 +112,7 @@ class AlterModelManagers(ModelOptionOperation):
94112

95113
class IndexOperation(Operation):
96114
option_name: str
97-
@property
115+
@cached_property
98116
def model_name_lower(self) -> str: ...
99117

100118
class AddIndex(IndexOperation):
@@ -119,9 +137,9 @@ class RenameIndex(IndexOperation):
119137
old_name: str | None = ...,
120138
old_fields: Sequence[str] | None = ...,
121139
) -> None: ...
122-
@property
140+
@cached_property
123141
def old_name_lower(self) -> str: ...
124-
@property
142+
@cached_property
125143
def new_name_lower(self) -> str: ...
126144

127145
class AddConstraint(IndexOperation):

django-stubs/db/migrations/operations/special.pyi

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ from .base import Operation
1010
class SeparateDatabaseAndState(Operation):
1111
database_operations: Sequence[Operation]
1212
state_operations: Sequence[Operation]
13+
1314
def __init__(
14-
self, database_operations: Sequence[Operation] = ..., state_operations: Sequence[Operation] = ...
15+
self, database_operations: Sequence[Operation] | None = ..., state_operations: Sequence[Operation] | None = ...
1516
) -> None: ...
1617

1718
class RunSQL(Operation):
@@ -24,10 +25,12 @@ class RunSQL(Operation):
2425
self,
2526
sql: str | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]],
2627
reverse_sql: str | None | _ListOrTuple[str | tuple[str, dict[str, Any] | _ListOrTuple[str] | None]] = ...,
27-
state_operations: Sequence[Operation] = ...,
28+
state_operations: Sequence[Operation] | None = ...,
2829
hints: Mapping[str, Any] | None = ...,
2930
elidable: bool = ...,
3031
) -> None: ...
32+
@property
33+
def reversible(self) -> bool: ... # type: ignore[override]
3134

3235
class _CodeCallable(Protocol):
3336
def __call__(self, __state_apps: StateApps, __schema_editor: BaseDatabaseSchemaEditor) -> None: ...
@@ -46,3 +49,5 @@ class RunPython(Operation):
4649
) -> None: ...
4750
@staticmethod
4851
def noop(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None: ...
52+
@property
53+
def reversible(self) -> bool: ... # type: ignore[override]

django-stubs/db/migrations/state.pyi

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Iterator, Sequence
1+
from collections.abc import Iterable, Iterator, Sequence
22
from contextlib import contextmanager
33
from typing import Any
44

@@ -7,8 +7,10 @@ from django.apps.registry import Apps
77
from django.db.models.base import Model
88
from django.db.models.fields import Field
99
from django.db.models.manager import Manager
10+
from django.utils.functional import cached_property
1011

11-
class AppConfigStub(AppConfig): ...
12+
class AppConfigStub(AppConfig):
13+
def __init__(self, label: str) -> None: ...
1214

1315
class ModelState:
1416
name: str
@@ -30,9 +32,8 @@ class ModelState:
3032
def construct_managers(self) -> Iterator[tuple[str, Manager]]: ...
3133
@classmethod
3234
def from_model(cls, model: type[Model], exclude_rels: bool = ...) -> ModelState: ...
33-
# Removed in 3.2, but back in 4.0
34-
# def get_field(self, field_name: str) -> Field: ...
35-
@property
35+
def get_field(self, field_name: str) -> Field: ...
36+
@cached_property
3637
def name_lower(self) -> str: ...
3738
def render(self, apps: Apps) -> Any: ...
3839
def get_index_by_name(self, name: str) -> Any: ...
@@ -45,22 +46,46 @@ def get_related_models_recursive(model: type[Model]) -> set[tuple[str, str]]: ..
4546
class ProjectState:
4647
is_delayed: bool
4748
models: dict[Any, Any]
48-
real_apps: list[str]
49+
real_apps: set[str]
4950
def __init__(
50-
self, models: dict[tuple[str, str], ModelState] | None = ..., real_apps: list[str] | None = ...
51+
self, models: dict[tuple[str, str], ModelState] | None = ..., real_apps: set[str] | None = ...
5152
) -> None: ...
52-
def add_model(self, model_state: ModelState) -> None: ...
5353
@property
54+
def relations(self) -> Any: ...
55+
def add_model(self, model_state: ModelState) -> None: ...
56+
@cached_property
5457
def apps(self) -> StateApps: ...
5558
def clear_delayed_apps_cache(self) -> None: ...
5659
def clone(self) -> ProjectState: ...
57-
@property
58-
def concrete_apps(self) -> StateApps: ...
5960
@classmethod
6061
def from_apps(cls, apps: Apps) -> ProjectState: ...
6162
def reload_model(self, app_label: str, model_name: str, delay: bool = ...) -> None: ...
6263
def reload_models(self, models: list[Any], delay: bool = ...) -> None: ...
6364
def remove_model(self, app_label: str, model_name: str) -> None: ...
65+
def rename_model(self, app_label: str, old_name: str, new_name: str) -> None: ...
66+
def alter_model_options(
67+
self, app_label: str, model_name: str, options: dict[str, Any], option_keys: Iterable[str] | None = ...
68+
) -> None: ...
69+
def remove_model_options(self, app_label: str, model_name: str, option_name: str, value_to_remove: Any) -> None: ...
70+
def alter_model_managers(self, app_label: str, model_name: str, managers: list[tuple[str, Manager]]) -> None: ...
71+
def add_index(self, app_label: str, model_name: str, index: Any) -> None: ...
72+
def remove_index(self, app_label: str, model_name: str, index_name: str) -> None: ...
73+
def rename_index(self, app_label: str, model_name: str, old_index_name: str, new_index_name: str) -> None: ...
74+
def add_constraint(self, app_label: str, model_name: str, constraint: Any) -> None: ...
75+
def remove_constraint(self, app_label: str, model_name: str, constraint_name: str) -> None: ...
76+
def add_field(self, app_label: str, model_name: str, name: str, field: Field, preserve_default: Any) -> None: ...
77+
def remove_field(self, app_label: str, model_name: str, name: str) -> None: ...
78+
def alter_field(self, app_label: str, model_name: str, name: str, field: Field, preserve_default: Any) -> None: ...
79+
def rename_field(self, app_label: str, model_name: str, old_name: str, new_name: str) -> None: ...
80+
def update_model_field_relation(
81+
self, model: type[Model], model_key: tuple[str, str], field_name: str, field: Field, concretes: Any
82+
) -> None: ...
83+
def resolve_model_field_relations(
84+
self, model_key: tuple[str, str], field_name: str, field: Field, concretes: Any | None = ...
85+
) -> None: ...
86+
def resolve_model_relations(self, model_key: tuple[str, str], concretes: Any | None = ...) -> None: ...
87+
def resolve_fields_and_relations(self) -> None: ...
88+
def get_concrete_model_key(self, model: type[Model]) -> Any: ...
6489

6590
class StateApps(Apps):
6691
real_models: list[ModelState]

django-stubs/db/models/enums.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Choices(enum.Enum, metaclass=ChoicesMeta):
1919
def label(self) -> str: ...
2020
@enum_property
2121
def value(self) -> Any: ...
22+
@property
23+
def do_not_call_in_templates(self) -> bool: ...
2224

2325
# fake
2426
class _IntegerChoicesMeta(ChoicesMeta):

0 commit comments

Comments
 (0)