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.
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