Skip to content

Commit

Permalink
Add a warning check for typing.Annotated usage as schema= argument
Browse files Browse the repository at this point in the history
  • Loading branch information
surenkov committed Mar 24, 2024
1 parent 82a1b89 commit 5f2f9a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions django_pydantic_field/v2/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import typing as ty
import typing_extensions as te

import pydantic
from django.core import checks, exceptions
Expand All @@ -19,7 +20,6 @@
if ty.TYPE_CHECKING:
import json

import typing_extensions as te
from django.db.models import Model

class _SchemaFieldKwargs(types.ExportKwargs, total=False):
Expand Down Expand Up @@ -109,6 +109,15 @@ def contribute_to_class(self, cls: types.DjangoModelType, name: str, private_onl
def check(self, **kwargs: ty.Any) -> list[checks.CheckMessage]:
# Remove checks of using mutable datastructure instances as `default` values, since they'll be adapted anyway.
performed_checks = [check for check in super().check(**kwargs) if check.id != "fields.E010"]
if isinstance(self.schema, te._AnnotatedAlias):
message = "typing.Annotated[...] is not supported as the SchemaField(schema=...) argument."
annot_hint = f"{self.attname}: typing.Annotated[{self.schema.__origin__!r}, ...]"
hint = (
f"Please consider using field annotation syntax, e.g. `{annot_hint} = SchemaField(...)`; "
"Or a fallback to `pydantic.RootModel` with annotation instead."
)
performed_checks.append(checks.Warning(message, obj=self, hint=hint, id="pydantic.W004"))

try:
# Test that the schema could be resolved in runtime, even if it contains forward references.
self.adapter.validate_schema()
Expand Down Expand Up @@ -141,7 +150,7 @@ def check(self, **kwargs: ty.Any) -> list[checks.CheckMessage]:
except pydantic.ValidationError as exc:
message = f"Export arguments may lead to data integrity problems. Pydantic error: \n{str(exc)}"
hint = "Please review `import` and `export` arguments."
performed_checks.append(checks.Warning(message, obj=self, hint=hint, id="pydantic.E003"))
performed_checks.append(checks.Warning(message, obj=self, hint=hint, id="pydantic.W003"))

return performed_checks

Expand Down
6 changes: 6 additions & 0 deletions tests/test_app/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing as t
import typing_extensions as te

import pydantic
from django.db import models
Expand Down Expand Up @@ -51,3 +52,8 @@ class RootSchema(pydantic.BaseModel):

class SampleModelWithRoot(models.Model):
root_field = SchemaField(schema=RootSchema, default=list)


class SampleModelAnnotated(models.Model):
annotated_field: te.Annotated[t.Union[int, float], pydantic.Field(gt=0)] = SchemaField()
arg_field = SchemaField(schema=te.Annotated[t.Union[int, float], pydantic.Field(lt=0)])

0 comments on commit 5f2f9a1

Please sign in to comment.