Skip to content

Commit

Permalink
[5.0] Add django.db.models.GeneratedField (#1944)
Browse files Browse the repository at this point in the history
* Add django.db.models.GeneratedField

* Fix all the known test failures for GeneratedField

* Ignore cached_col cached_property issues

* Removed default values from GeneratedField

* Apply suggestions from code review

---------

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
palfrey and sobolevn authored Feb 17, 2024
1 parent 5a8e7b3 commit 2a2231a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions django-stubs/db/models/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ from .fields import URLField as URLField
from .fields import UUIDField as UUIDField
from .fields.files import FileField as FileField
from .fields.files import ImageField as ImageField
from .fields.generated import GeneratedField as GeneratedField
from .fields.json import JSONField as JSONField
from .fields.proxy import OrderWrt as OrderWrt
from .fields.related import ForeignKey as ForeignKey
Expand Down
47 changes: 47 additions & 0 deletions django-stubs/db/models/fields/generated.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Any, Iterable, Literal

from django.core.validators import _ValidatorCallable
from django.db import models
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.expressions import Expression
from django.db.models.fields import _ErrorMessagesMapping, _FieldChoices
from django.db.models.sql import Query
from django.utils.datastructures import DictWrapper
from django.utils.functional import _StrOrPromise

class GeneratedField(models.Field):
generated: Literal[True]
db_returning: Literal[True]
_query: Query | None
output_field: models.Field | None

def __init__(
self,
*,
expression: Expression,
output_field: models.Field,
db_persist: bool | None = ...,
verbose_name: _StrOrPromise | None = ...,
name: str | None = ...,
primary_key: bool = ...,
unique: bool = ...,
blank: bool = ...,
null: bool = ...,
default: Any = ...,
editable: bool = ...,
auto_created: bool = ...,
serialize: bool = ...,
unique_for_date: str | None = ...,
unique_for_month: str | None = ...,
unique_for_year: str | None = ...,
choices: _FieldChoices | None = ...,
help_text: _StrOrPromise = ...,
db_column: str | None = ...,
db_comment: str | None = ...,
db_tablespace: str | None = ...,
validators: Iterable[_ValidatorCallable] = ...,
error_messages: _ErrorMessagesMapping | None = ...,
**kwargs: Any,
) -> None: ...
def generated_sql(self, connection: BaseDatabaseWrapper) -> tuple[str, Any]: ...
def db_type_parameters(self, connection: BaseDatabaseWrapper) -> DictWrapper: ...
3 changes: 3 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ django.contrib.gis.db.models.ForeignObjectRel.name
django.contrib.gis.db.models.ForeignObjectRel.one_to_many
django.contrib.gis.db.models.ForeignObjectRel.one_to_one
django.contrib.gis.db.models.ForeignObjectRel.related_model
django.contrib.gis.db.models.GeneratedField.cached_col
django.contrib.gis.db.models.IntegerField.validators
django.contrib.gis.db.models.Lookup.contains_aggregate
django.contrib.gis.db.models.Lookup.contains_over_clause
Expand Down Expand Up @@ -300,6 +301,7 @@ django.db.models.ForeignObjectRel.name
django.db.models.ForeignObjectRel.one_to_many
django.db.models.ForeignObjectRel.one_to_one
django.db.models.ForeignObjectRel.related_model
django.db.models.GeneratedField.cached_col
django.db.models.IntegerField.validators
django.db.models.Lookup.contains_aggregate
django.db.models.Lookup.contains_over_clause
Expand All @@ -313,6 +315,7 @@ django.db.models.fields.DecimalField.validators
django.db.models.fields.Field.cached_col
django.db.models.fields.Field.validators
django.db.models.fields.IntegerField.validators
django.db.models.fields.generated.GeneratedField.cached_col
django.db.models.fields.related.ForeignObject.foreign_related_fields
django.db.models.fields.related.ForeignObject.local_related_fields
django.db.models.fields.related.ForeignObject.related_fields
Expand Down
3 changes: 0 additions & 3 deletions scripts/stubtest/allowlist_todo_django50.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ django.contrib.gis.db.models.ForeignObject.get_joining_fields
django.contrib.gis.db.models.ForeignObject.get_reverse_joining_fields
django.contrib.gis.db.models.ForeignObjectRel.get_joining_fields
django.contrib.gis.db.models.Func.allowed_default
django.contrib.gis.db.models.GeneratedField
django.contrib.gis.db.models.Lookup.allowed_default
django.contrib.gis.db.models.Prefetch.get_current_querysets
django.contrib.gis.db.models.Q.identity
Expand Down Expand Up @@ -181,7 +180,6 @@ django.db.models.ForeignObject.get_joining_fields
django.db.models.ForeignObject.get_reverse_joining_fields
django.db.models.ForeignObjectRel.get_joining_fields
django.db.models.Func.allowed_default
django.db.models.GeneratedField
django.db.models.Lookup.allowed_default
django.db.models.Prefetch.get_current_querysets
django.db.models.Q.identity
Expand All @@ -206,7 +204,6 @@ 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.generated
django.db.models.fields.related.ForeignKey.cast_db_type
django.db.models.fields.related.ForeignObject.get_joining_fields
django.db.models.fields.related.ForeignObject.get_reverse_joining_fields
Expand Down

0 comments on commit 2a2231a

Please sign in to comment.