mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-31 02:00:34 -05:00
* 📝 Add docs recommending Union over Optional * 📝 Update docs recommending Union over Optional * 📝 Update source examples for docs, recommend Union over Optional * 📝 Update highlighted lines with updated source examples * 📝 Update highlighted lines in Markdown with recent code changes * 📝 Update docs, use Union instead of Optional * ♻️ Update source examples to recommend Union over Optional * 🎨 Update highlighted code in Markdown after moving from Optional to Union
27 lines
552 B
Python
27 lines
552 B
Python
from dataclasses import dataclass, field
|
|
from typing import List, Union
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
@dataclass
|
|
class Item:
|
|
name: str
|
|
price: float
|
|
tags: List[str] = field(default_factory=list)
|
|
description: Union[str, None] = None
|
|
tax: Union[float, None] = None
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/items/next", response_model=Item)
|
|
async def read_next_item():
|
|
return {
|
|
"name": "Island In The Moon",
|
|
"price": 12.99,
|
|
"description": "A place to be be playin' and havin' fun",
|
|
"tags": ["breater"],
|
|
}
|