Skip to content

Support ClassVar for Pydantic v2 #2919

@ubaumann

Description

@ubaumann

Is your feature request related to a problem? Please describe.
I would like to be able to define Pydantic v2 ClassVars using JSON Schema.

Describe the solution you'd like
After some investigation, I think the cleanest approach is to use a JSON Schema extra property with an x- prefix.

Proposed Schema

{
    "type": "object",
    "properties": {
        "namespace": {
            "type": "string",
            "x-is_classvar": true,
            "default": "test"
        }
    }
}

Is generating a class variable:

class Model(BaseModel):
    namespace: ClassVar[str | None] = 'test'

Describe alternatives you've considered
Another option is to add a new command parameter, but it would be nice to have it inside the schema.

Additional context
I am happy to discuss and create a PR

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