Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Pydantic Model "TypeError: 'type' object is not iterable"

I'm using FastAPI to build a data API. I want the clients to post 2 lists of 24 floats that later I will save into a database.

When I try to create the Pydantic model:

from pydantic import BaseModel

class Prices(BaseModel):
    buying_price: list(float)=[]
    selling_price: list(float)=[]

I get the following error:

File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 3, in <module>
    class Prices(BaseModel):
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 4, in Prices
    buying_price: list(float)=[]
TypeError: 'type' object is not iterable

Even though the error is self-explanatory, I don't understand it.

Then, looking at the documentation I found the following way:

from pydantic import BaseModel
from typing import List

class Prices(BaseModel):
    buying_price: List(float)=[]
    selling_price: List(float)=[]

But I got the following error.

File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 4, in <module>
    class Prices(BaseModel):
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 5, in Prices
    buying_price: List(float)=[]
File "C:\Users\Amin y Lubna\anaconda3\lib\typing.py", line 727, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead

I've been struggling with the error a couple days and I'm not able to find a solution to the issues.

like image 487
Armen Firman Avatar asked Oct 25 '25 09:10

Armen Firman


1 Answers

You need to use list[float], not list(float)

like image 180
Zaffer Avatar answered Oct 27 '25 22:10

Zaffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!