Skip to content

[BUG] Python 3.14 annotations for "custom_fields" in ModelSchemaMetaclass not compatible #1652

@simonpercivall

Description

@simonpercivall

Description
Let's say I have a ModelSchema:

class UserSchema(ModelSchema):
    password: SecretStr

    class Meta:
        model = User
        fields = ["username", "password"]

What django-ninja does, in ModelSchemaMetaclass.__new__ is:

custom_fields = []
annotations = namespace.get("__annotations__", {})
for attr_name, type in annotations.items():
    ...
    custom_fields.append((attr_name, type, default))

However, in Python 3.14, namespace will not have __annotations__, it will have an __annotate_func.

So in Python 3.14, custom_fields == [] after that loop.

I'm unsure what the "best practice" is for this, what we're supposed to do. But I've seen an least one other library doing:

ann = annotationlib.get_annotate_from_class_namespace(namespace)
annotations = annotationlib.call_annotate_function(
    ann, format=annotationlib.Format.VALUE
) if ann else {}

cf.: stdlib:annotationlib

Versions:

  • Python version: 3.14
  • Django version: 6.0
  • Django-Ninja version: 1.5.3
  • Pydantic version: 2.12.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions