In the code below from the official FastAPI tutorials page, I can't wrap my head around the the statements (e.g, name: str).
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
I mean, I understand that they should enforce the type, but how exactly should they do that, as python does not enforce types.
- I saw also the Pydantic manual, but have seen no explanation regarding this particular syntax (e.g,
name: str) in the class declaration.
Could someone please explain this syntax for me, and how can you check for the meant type of a class when you create it in such a way?
Thanks in advance.