Debian Codegraph @

tests/test_compatibility.py

raw symbols SCIP
1 """Test Python 3.14 compatibility for ModelSchema annotations.
2
3 Python 3.14 no longer puts __annotations__ in the class namespace during
4 metaclass __new__ (PEP 749). This broke ModelSchemaMetaclass which reads
5 annotations from namespace to build custom_fields.
6 issues 1652, 1580
7 """
8
9 from django.db import models
10
11 from ninja import ModelSchema, Schema
12
13
14 def test_modelschema_custom_annotation():
15 """Custom type annotation on ModelSchema field should override the Django field type."""
16
17 class CompatTestModel(models.Model):
18 name = models.CharField(max_length=100)
19 value = models.CharField(max_length=100)
20
21 class Meta:
22 app_label = "tests"
23
24 class CompatTestModelSchema(ModelSchema):
25 value: int # override CharField -> int
26
27 class Meta:
28 model = CompatTestModel
29 fields = ["name", "value"]
30
31 assert CompatTestModelSchema.model_fields["value"].annotation is int
32
33
34 def test_modelschema_fk_schema_annotation():
35 """FK field annotated with a nested Schema should use that schema, not the raw DB type."""
36
37 class CompatParentModel(models.Model):
38 title = models.CharField(max_length=100)
39
40 class Meta:
41 app_label = "tests"
42
43 class CompatChildModel(models.Model):
44 name = models.CharField(max_length=100)
45 parent = models.ForeignKey(CompatParentModel, on_delete=models.CASCADE)
46
47 class Meta:
48 app_label = "tests"
49
50 class CompatParentSchema(Schema):
51 title: str
52
53 class CompatChildSchema(ModelSchema):
54 parent: CompatParentSchema
55
56 class Meta:
57 model = CompatChildModel
58 fields = ["name", "parent"]
59
60 assert CompatChildSchema.model_fields["parent"].annotation is CompatParentSchema