From 9fbe1a9145d0708f47619643798e292dfb235e5e Mon Sep 17 00:00:00 2001 From: Scorpio Date: Tue, 2 Apr 2024 14:01:04 +0300 Subject: [PATCH] [5.0] Add `db_default=` parameter to models `Field` classes (#1876) --- django-stubs/contrib/gis/db/models/fields.pyi | 6 ++++-- django-stubs/contrib/postgres/fields/array.pyi | 5 +++-- django-stubs/db/backends/base/schema.pyi | 2 ++ django-stubs/db/models/fields/__init__.pyi | 14 +++++++++++++- django-stubs/db/models/fields/files.pyi | 4 +++- django-stubs/db/models/fields/related.pyi | 15 +++++++++++++-- scripts/stubtest/allowlist_todo_django50.txt | 4 ---- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/django-stubs/contrib/gis/db/models/fields.pyi b/django-stubs/contrib/gis/db/models/fields.pyi index 30e061930..3272022f1 100644 --- a/django-stubs/contrib/gis/db/models/fields.pyi +++ b/django-stubs/contrib/gis/db/models/fields.pyi @@ -13,8 +13,8 @@ from django.contrib.gis.geos import ( Polygon, ) from django.core.validators import _ValidatorCallable -from django.db.models.expressions import Combinable -from django.db.models.fields import Field, _ErrorMessagesMapping, _FieldChoices +from django.db.models.expressions import Combinable, Expression +from django.db.models.fields import NOT_PROVIDED, Field, _ErrorMessagesMapping, _FieldChoices from django.utils.functional import _StrOrPromise # __set__ value type @@ -51,6 +51,7 @@ class BaseSpatialField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -95,6 +96,7 @@ class GeometryField(BaseSpatialField[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., diff --git a/django-stubs/contrib/postgres/fields/array.pyi b/django-stubs/contrib/postgres/fields/array.pyi index 2b34c7885..e7a0ea6a3 100644 --- a/django-stubs/contrib/postgres/fields/array.pyi +++ b/django-stubs/contrib/postgres/fields/array.pyi @@ -3,8 +3,8 @@ from typing import Any, TypeVar from django.core.validators import _ValidatorCallable from django.db.models import Field, Transform -from django.db.models.expressions import Combinable -from django.db.models.fields import _ErrorMessagesDict, _ErrorMessagesMapping, _FieldChoices +from django.db.models.expressions import Combinable, Expression +from django.db.models.fields import NOT_PROVIDED, _ErrorMessagesDict, _ErrorMessagesMapping, _FieldChoices from django.db.models.fields.mixins import CheckFieldDefaultMixin from django.utils.functional import _StrOrPromise @@ -37,6 +37,7 @@ class ArrayField(CheckFieldDefaultMixin, Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., diff --git a/django-stubs/db/backends/base/schema.pyi b/django-stubs/db/backends/base/schema.pyi index bf73444a2..8abf68484 100644 --- a/django-stubs/db/backends/base/schema.pyi +++ b/django-stubs/db/backends/base/schema.pyi @@ -10,6 +10,7 @@ from django.db.models.base import Model from django.db.models.constraints import BaseConstraint from django.db.models.fields import Field from django.db.models.indexes import Index +from django.db.models.sql.compiler import _AsSqlType from typing_extensions import Self logger: Logger @@ -76,6 +77,7 @@ class BaseDatabaseSchemaEditor(AbstractContextManager[Any]): def skip_default(self, field: Any) -> bool: ... def skip_default_on_alter(self, field: Any) -> bool: ... def prepare_default(self, value: Any) -> Any: ... + def db_default_sql(self, field: Field) -> _AsSqlType: ... def effective_default(self, field: Field) -> int | str: ... def quote_value(self, value: Any) -> str: ... def create_model(self, model: type[Model]) -> None: ... diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index b4323a5e3..815c1ad24 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -11,7 +11,7 @@ from django.core.checks import CheckMessage from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist from django.db.backends.base.base import BaseDatabaseWrapper from django.db.models import Model -from django.db.models.expressions import Col, Combinable +from django.db.models.expressions import Col, Combinable, Expression from django.db.models.fields.reverse_related import ForeignObjectRel from django.db.models.query_utils import Q, RegisterLookupMixin from django.forms import Widget @@ -144,6 +144,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]): choices: _ChoicesList | None db_column: str | None db_comment: str | None + db_default: type[NOT_PROVIDED] | Expression column: str concrete: bool default: Any @@ -182,6 +183,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]): validators: Iterable[validators._ValidatorCallable] = ..., error_messages: _ErrorMessagesMapping | None = ..., db_comment: str | None = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., ) -> None: ... def __set__(self, instance: Any, value: _ST) -> None: ... # class access @@ -282,6 +284,7 @@ class DecimalField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -310,6 +313,7 @@ class CharField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -339,6 +343,7 @@ class SlugField(CharField[_ST, _GT]): blank: bool = ..., null: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -374,6 +379,7 @@ class URLField(CharField[_ST, _GT]): db_index: bool = ..., rel: ForeignObjectRel | None = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., serialize: bool = ..., unique_for_date: str | None = ..., @@ -405,6 +411,7 @@ class TextField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -455,6 +462,7 @@ class GenericIPAddressField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -489,6 +497,7 @@ class DateField(DateTimeCheckMixin, Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -519,6 +528,7 @@ class TimeField(DateTimeCheckMixin, Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -553,6 +563,7 @@ class UUIDField(Field[_ST, _GT]): db_index: bool = ..., rel: ForeignObjectRel | None = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., serialize: bool = ..., unique_for_date: str | None = ..., @@ -591,6 +602,7 @@ class FilePathField(Field[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., diff --git a/django-stubs/db/models/fields/files.pyi b/django-stubs/db/models/fields/files.pyi index 3843ae405..204f569c0 100644 --- a/django-stubs/db/models/fields/files.pyi +++ b/django-stubs/db/models/fields/files.pyi @@ -6,7 +6,8 @@ from django.core.files.base import File from django.core.files.images import ImageFile from django.core.files.storage import Storage from django.db.models.base import Model -from django.db.models.fields import Field, _ErrorMessagesMapping, _FieldChoices +from django.db.models.expressions import Expression +from django.db.models.fields import NOT_PROVIDED, Field, _ErrorMessagesMapping, _FieldChoices from django.db.models.query_utils import DeferredAttribute from django.utils._os import _PathCompatible from django.utils.functional import _StrOrPromise @@ -58,6 +59,7 @@ class FileField(Field): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | str = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., diff --git a/django-stubs/db/models/fields/related.pyi b/django-stubs/db/models/fields/related.pyi index 2c60b595e..ead3e49af 100644 --- a/django-stubs/db/models/fields/related.pyi +++ b/django-stubs/db/models/fields/related.pyi @@ -4,8 +4,15 @@ from uuid import UUID from django.core import validators # due to weird mypy.stubtest error from django.db.models.base import Model -from django.db.models.expressions import Combinable -from django.db.models.fields import Field, _AllLimitChoicesTo, _ErrorMessagesMapping, _FieldChoices, _LimitChoicesTo +from django.db.models.expressions import Combinable, Expression +from django.db.models.fields import ( + NOT_PROVIDED, + Field, + _AllLimitChoicesTo, + _ErrorMessagesMapping, + _FieldChoices, + _LimitChoicesTo, +) from django.db.models.fields.mixins import FieldCacheMixin from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor as ForwardManyToOneDescriptor from django.db.models.fields.related_descriptors import ForwardOneToOneDescriptor as ForwardOneToOneDescriptor @@ -56,6 +63,7 @@ class RelatedField(FieldCacheMixin, Field[_ST, _GT]): db_index: bool = ..., rel: ForeignObjectRel | None = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., serialize: bool = ..., unique_for_date: str | None = ..., @@ -110,6 +118,7 @@ class ForeignObject(RelatedField[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -166,6 +175,7 @@ class ForeignKey(ForeignObject[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., @@ -207,6 +217,7 @@ class OneToOneField(ForeignKey[_ST, _GT]): null: bool = ..., db_index: bool = ..., default: Any = ..., + db_default: type[NOT_PROVIDED] | Expression | _ST = ..., editable: bool = ..., auto_created: bool = ..., serialize: bool = ..., diff --git a/scripts/stubtest/allowlist_todo_django50.txt b/scripts/stubtest/allowlist_todo_django50.txt index 8d18a8736..c4bce2abf 100644 --- a/scripts/stubtest/allowlist_todo_django50.txt +++ b/scripts/stubtest/allowlist_todo_django50.txt @@ -12,7 +12,6 @@ django.contrib.gis.db.backends.spatialite.schema.SpatialiteSchemaEditor.alter_db django.contrib.gis.db.models.Case.allowed_default django.contrib.gis.db.models.ExpressionWrapper.allowed_default django.contrib.gis.db.models.F.allowed_default -django.contrib.gis.db.models.Field.__init__ django.contrib.gis.db.models.Field._get_flatchoices django.contrib.gis.db.models.Field.generated django.contrib.gis.db.models.FilteredRelation.relabeled_clone @@ -74,7 +73,6 @@ django.db.backends.base.features.BaseDatabaseFeatures.supports_nulls_distinct_un django.db.backends.base.features.BaseDatabaseFeatures.supports_stored_generated_columns django.db.backends.base.features.BaseDatabaseFeatures.supports_virtual_generated_columns django.db.backends.base.operations.BaseDatabaseOperations.prepare_join_on_clause -django.db.backends.base.schema.BaseDatabaseSchemaEditor.db_default_sql django.db.backends.mysql.features.DatabaseFeatures.allows_group_by_selected_pks django.db.backends.mysql.features.DatabaseFeatures.delete_can_self_reference_subquery django.db.backends.mysql.features.DatabaseFeatures.has_native_uuid_field @@ -107,7 +105,6 @@ django.db.migrations.serializer.BaseUnorderedSequenceSerializer django.db.models.Case.allowed_default django.db.models.ExpressionWrapper.allowed_default django.db.models.F.allowed_default -django.db.models.Field.__init__ django.db.models.Field._get_flatchoices django.db.models.Field.generated django.db.models.FilteredRelation.relabeled_clone @@ -133,7 +130,6 @@ django.db.models.expressions.OrderByList.allowed_default django.db.models.expressions.RawSQL.allowed_default django.db.models.expressions.Value.allowed_default django.db.models.expressions.When.allowed_default -django.db.models.fields.Field.__init__ django.db.models.fields.Field._get_flatchoices django.db.models.fields.Field.generated django.db.models.fields.related.ForeignKey.cast_db_type