Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid constrained types such as conint() #2313

Open
multimeric opened this issue Feb 9, 2025 · 0 comments
Open

Avoid constrained types such as conint() #2313

multimeric opened this issue Feb 9, 2025 · 0 comments

Comments

@multimeric
Copy link

Describe the bug
Python's type specification no longer allows for function calls directly in type annotations, as discussed here. However, in an effort to keep the generator compatible with Pydantic 1, constructions such as x: conint() are still created, which is not actually valid Python.

I think there needs to be a Pydantic 2+ flag that can relax the need for backwards compatibility, and use newer more valid constructs like Annotated[]

To Reproduce

Example schema:

openapi: 3.1.0
info:
  title: Constraints
  version: '1.0'
components:
  schemas:
    dayInYear:
      type: integer
      minimum: 1
      maximum: 365

Used commandline:

datamodel-codegen --input openapi.yml

Actual output

from __future__ import annotations

from pydantic import BaseModel, conint


class DayInYear(BaseModel):
    __root__: conint(ge=1, le=365)

Expected behavior
There should be a flag such as --pydantic-2 or --pydantic 2+ that instead outputs:

from typing_extensions import Annotated
from pydantic import BaseModel, Field

class DayInYear(BaseModel):
    __root__: Annotated[int, Field(ge=1, le=365)

Version:

  • OS: Pop!_OS 22.04 LTS
  • Python version: Python 3.13.1
  • datamodel-code-generator version: 0.27.2

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant