koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.63k stars 294 forks source link

Generation of confloat warnings #1812

Open camstuart opened 8 months ago

camstuart commented 8 months ago

Describe the bug Pydantic 2.5.3 complains about incorrect type definition confloat:

This function is discouraged in favor of using Annotated with [Field][pydantic.fields.Field] instead.

To Reproduce

Example schema:

{
  "$id": "https://example.com/geographical-location.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Longitude and Latitude Values",
  "description": "A geographical coordinate.",
  "required": [ "latitude", "longitude" ],
  "type": "object",
  "properties": {
    "latitude": {
      "type": "number",
      "minimum": -90,
      "maximum": 90
    },
    "longitude": {
      "type": "number",
      "minimum": -180,
      "maximum": 180
    }
  }
}

Used commandline:

$ poetry run datamodel-codegen  --input ../schemas/geo-point.schema.json --input-file-type jsonschema --output typedef/geo_point.py

Output:

from __future__ import annotations

from pydantic import BaseModel, confloat

class LongitudeAndLatitudeValues(BaseModel):
    latitude: confloat(ge=-90.0, le=90.0)
    longitude: confloat(ge=-180.0, le=180.0)

Warning in full:

def confloat(*, strict: bool | None = None, gt: float | None = None, ge: float | None = None, lt: float | None = None, le: float | None = None, multiple_of: float | None = None, allow_inf_nan: bool | None = None) -> Type[float] !!! warning "Discouraged" This function is discouraged in favor of using Annotated with [Field][pydantic.fields.Field] instead. This function will be deprecated in Pydantic 3.0. The reason is that confloat returns a type, which doesn't play well with static analysis tools. === ":x: Don't do this" py from pydantic import BaseModel, confloat class Foo(BaseModel): bar: confloat(strict=True, gt=0) === ":white_check_mark: Do this" py from typing_extensions import Annotated from pydantic import BaseModel, Field class Foo(BaseModel): bar: Annotated[float, Field(strict=True, gt=0)] A wrapper around float that allows for additional constraints. Args: strict: Whether to validate the float in strict mode. gt: The value must be greater than this. ge: The value must be greater than or equal to this. lt: The value must be less than this. le: The value must be less than or equal to this. multiple_of: The value must be a multiple of this. allow_inf_nan: Whether to allow -inf, inf, and nan. Returns: The wrapped float type. py from pydantic import BaseModel, ValidationError, confloat class ConstrainedExample(BaseModel): constrained_float: confloat(gt=1.0) m = ConstrainedExample(constrained_float=1.1) print(repr(m)) #> ConstrainedExample(constrained_float=1.1) try: ConstrainedExample(constrained_float=0.9) except ValidationError as e: print(e.errors()) ''' [ { 'type': 'greater_than', 'loc': ('constrained_float',), 'msg': 'Input should be greater than 1', 'input': 0.9, 'ctx': {'gt': 1.0}, 'url': 'https://errors.pydantic.dev/2/v/greater_than', } ] ''' Params: strict – Whether to validate the float in strict mode. gt – The value must be greater than this. ge – The value must be greater than or equal to this. lt – The value must be less than this. le – The value must be less than or equal to this. multiple_of – The value must be a multiple of this. allow_inf_nan – Whether to allow -inf, inf, and nan. Returns: The wrapped float type.

psysrc commented 8 months ago

This is because the default output from datamodel-codegen uses con... Pydantic types, which is now deprecated. I recommend trying the command again with the --field-constraints flag, which will switch the output to use Pydantic's new Field type.

From the --help documentation:

--field-constraints Use field constraints and not con* annotations