Describe the bug
When using βstrict-types
and --field-constraints
(or --use-annotated
) together, the field constraints are not added to the Field.
To Reproduce
Example schema:
components:
schemas:
Timestamp:
type: integer
minimum: 1
maximum: 9999999999
Used commandline:
$ datamodel-codegen --input api.yaml --output models.py --input-file-type openapi --output-model-type pydantic_v2.BaseModel --field-constraints --strict-types int
Expected behavior
Generates the model
class Timestamp(RootModel[StrictInt]):
root: StrictInt = Field(..., ge=1, le=9999999999)
Actual behavior
Generates the model
class Timestamp(RootModel[StrictInt]):
root: StrictInt
Version:
Additional context
There's a few workarounds, but both are undesirable:
--field-constraints
option, which uses conint
for enforcing constraints. But it fails to pass MyPy with the error "Invalid type comment or annotation".Timestamp.model_validate('123', strict=True)
. But then the generated model is vulnerable if we forget to pass strict=True
.Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too