I am using pydantic and strictyaml to read a yaml config file. I am using this class
class CarData(BaseModel):
model: str
length: float
width: float
height: float
fuel_efficiency: conlist(item_type=float, min_items=3, max_items=3) # Ensures exactly 3 values
# fuel_efficiency: list[float]
and I am following what is written in the documentation of conlist, however when I run the script I got
TypeError: conlist() got an unexpected keyword argument 'min_items'
Why is this happening, and how can it be corrected? (My pydantic version is 2.9.2 just in case)
The error is because of you are using the latest version of Pydantic v2.9 but using the old version parameters for conlist
. In the Pydantic version 2.x those parameters have changed to min_lenght
and max_length
according to the latest documentation. However, your link is related to the version 1.10.
Therefore, this issue will be fixed by the following line:
fuel_efficiency: conlist(float, min_length=3, max_length=3)
It's worth mentioning that, you can also have this by Annotated
as follows:
from typing import Annotated
from pydantic import Field
fuel_efficiency: Annotated[list[float], Field(min_length=3, max_length=3)]
Or:
fuel_efficiency: list[float] = Field(min_length=3, max_length=3)