diff --git a/django_pydantic_field/v2/fields.py b/django_pydantic_field/v2/fields.py index 5cac0f6..5c0fe98 100644 --- a/django_pydantic_field/v2/fields.py +++ b/django_pydantic_field/v2/fields.py @@ -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 @@ -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): @@ -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() @@ -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 diff --git a/tests/test_app/models.py b/tests/test_app/models.py index 56fb317..85464ec 100644 --- a/tests/test_app/models.py +++ b/tests/test_app/models.py @@ -1,4 +1,5 @@ import typing as t +import typing_extensions as te import pydantic from django.db import models @@ -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)])