Skip to content

Doesn't work to combine --use-annotated and --use-non-positive-negative-number-constrained-types #3014

@torarvid

Description

@torarvid

Hello hello 👋🏻

If I combine these two flags --use-annotated and --use-non-positive-negative-number-constrained-types, I basically get the same behavior as only using --use-annotated.

To Reproduce

Use the already included file tests/data/jsonschema/use_non_positive_negative.json, and run:

uv run datamodel-codegen --input ./tests/data/jsonschema/use_non_positive_negative.json --use-annotated --use-non-positive-negative-number-constrained-types

You'll see:

Current output
# generated by datamodel-codegen:
#   filename:  use_non_positive_negative.json
#   timestamp: 2026-03-02T13:02:36+00:00

from __future__ import annotations

from typing import Annotated

from pydantic import BaseModel, Field


class NumberConstraints(BaseModel):
    non_negative_count: Annotated[
        int | None, Field(description='A count that cannot be negative', ge=0)
    ] = None
    non_positive_balance: Annotated[
        int | None, Field(description='A balance that cannot be positive', le=0)
    ] = None
    non_negative_amount: Annotated[
        float | None, Field(description='An amount that cannot be negative', ge=0.0)
    ] = None
    non_positive_score: Annotated[
        float | None, Field(description='A score that cannot be positive', le=0.0)
    ] = None

Expected behavior

Expected output
# generated by datamodel-codegen:
#   filename:  use_non_positive_negative.json
#   timestamp: 2026-03-02T13:02:27+00:00

from __future__ import annotations

from typing import Annotated

from pydantic import (
    BaseModel,
    Field,
    NonNegativeFloat,
    NonNegativeInt,
    NonPositiveFloat,
    NonPositiveInt,
)


class NumberConstraints(BaseModel):
    non_negative_count: Annotated[
        NonNegativeInt | None, Field(description='A count that cannot be negative')
    ] = None
    non_positive_balance: Annotated[
        NonPositiveInt | None, Field(description='A balance that cannot be positive')
    ] = None
    non_negative_amount: Annotated[
        NonNegativeFloat | None, Field(description='An amount that cannot be negative')
    ] = None
    non_positive_score: Annotated[
        NonPositiveFloat | None, Field(description='A score that cannot be positive')
    ] = None

Version:

  • OS: macOS 26.3
  • Python version: 3.12.1
  • datamodel-code-generator version: 0.54.1

Additional context
I have already tried to make an improvement for this. Will make a PR soon

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