Open camstuart opened 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
Describe the bug Pydantic
2.5.3
complains about incorrect type definitionconfloat
:This function is discouraged in favor of using
Annotated
with [Field
][pydantic.fields.Field] instead.To Reproduce
Example schema:
Used commandline:
Output:
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 thatconfloat
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 aroundfloat
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
, andnan
. 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
, andnan
. Returns: The wrapped float type.