pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

Improvement in generic schema generation

Open adriangb opened this issue 1 year ago • 1 comments

Currently if you have a generic model and later substitute it we re-generate the entire schema by walking all of the types. This makes schema generation very expensive.

I propose that instead we wrap types as follows:

generic_model = {
    "type": "definitions",
    "schema": {
        "type": "model",
        "fields": {
            "foo": {
                "type": "definitions-ref",
                "ref": "T::some-ref",
            }
        }
    },
    "definitions": [
        {
            "type": "any",
            "ref": "T::some-ref",
        }
    ]
}

concrete_model = {
    "type": "definitions",
    "schema": generic_model,
    "definitions": [
        {
            "type": "int",
            "ref": "T::some-ref",
        }
    ]
}

This has the potential to greatly improve generics schema generation performance.

adriangb avatar Jul 17 '24 16:07 adriangb