Browse Source

Remove code examples for Python 3.8 in `body_nested_models`

pull/14510/head
Yurii Motov 7 months ago
parent
commit
4ffc89e383
  1. 30
      docs/en/docs/tutorial/body-nested-models.md
  2. 0
      docs_src/body_nested_models/tutorial001_py39.py
  3. 20
      docs_src/body_nested_models/tutorial002.py
  4. 20
      docs_src/body_nested_models/tutorial003.py
  5. 26
      docs_src/body_nested_models/tutorial004.py
  6. 26
      docs_src/body_nested_models/tutorial005.py
  7. 26
      docs_src/body_nested_models/tutorial006.py
  8. 32
      docs_src/body_nested_models/tutorial007.py
  9. 16
      docs_src/body_nested_models/tutorial008.py
  10. 10
      docs_src/body_nested_models/tutorial009.py
  11. 5
      tests/test_tutorial/test_body_nested_models/test_tutorial009.py

30
docs/en/docs/tutorial/body-nested-models.md

@ -14,35 +14,15 @@ This will make `tags` be a list, although it doesn't declare the type of the ele
But Python has a specific way to declare lists with internal types, or "type parameters": But Python has a specific way to declare lists with internal types, or "type parameters":
### Import typing's `List` { #import-typings-list }
In Python 3.9 and above you can use the standard `list` to declare these type annotations as we'll see below. 💡
But in Python versions before 3.9 (3.6 and above), you first need to import `List` from standard Python's `typing` module:
{* ../../docs_src/body_nested_models/tutorial002.py hl[1] *}
### Declare a `list` with a type parameter { #declare-a-list-with-a-type-parameter } ### Declare a `list` with a type parameter { #declare-a-list-with-a-type-parameter }
To declare types that have type parameters (internal types), like `list`, `dict`, `tuple`: To declare types that have type parameters (internal types), like `list`, `dict`, `tuple`,
pass the internal type(s) as "type parameters" using square brackets: `[` and `]`
* If you are in a Python version lower than 3.9, import their equivalent version from the `typing` module
* Pass the internal type(s) as "type parameters" using square brackets: `[` and `]`
In Python 3.9 it would be:
```Python ```Python
my_list: list[str] my_list: list[str]
``` ```
In versions of Python before 3.9, it would be:
```Python
from typing import List
my_list: List[str]
```
That's all standard Python syntax for type declarations. That's all standard Python syntax for type declarations.
Use that same standard syntax for model attributes with internal types. Use that same standard syntax for model attributes with internal types.
@ -178,12 +158,6 @@ Notice how `Offer` has a list of `Item`s, which in turn have an optional list of
If the top level value of the JSON body you expect is a JSON `array` (a Python `list`), you can declare the type in the parameter of the function, the same as in Pydantic models: If the top level value of the JSON body you expect is a JSON `array` (a Python `list`), you can declare the type in the parameter of the function, the same as in Pydantic models:
```Python
images: List[Image]
```
or in Python 3.9 and above:
```Python ```Python
images: list[Image] images: list[Image]
``` ```

0
docs_src/body_nested_models/tutorial001.py → docs_src/body_nested_models/tutorial001_py39.py

20
docs_src/body_nested_models/tutorial002.py

@ -1,20 +0,0 @@
from typing import List, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: List[str] = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

20
docs_src/body_nested_models/tutorial003.py

@ -1,20 +0,0 @@
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

26
docs_src/body_nested_models/tutorial004.py

@ -1,26 +0,0 @@
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

26
docs_src/body_nested_models/tutorial005.py

@ -1,26 +0,0 @@
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

26
docs_src/body_nested_models/tutorial006.py

@ -1,26 +0,0 @@
from typing import List, Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
images: Union[List[Image], None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

32
docs_src/body_nested_models/tutorial007.py

@ -1,32 +0,0 @@
from typing import List, Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
images: Union[List[Image], None] = None
class Offer(BaseModel):
name: str
description: Union[str, None] = None
price: float
items: List[Item]
@app.post("/offers/")
async def create_offer(offer: Offer):
return offer

16
docs_src/body_nested_models/tutorial008.py

@ -1,16 +0,0 @@
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
@app.post("/images/multiple/")
async def create_multiple_images(images: List[Image]):
return images

10
docs_src/body_nested_models/tutorial009.py

@ -1,10 +0,0 @@
from typing import Dict
from fastapi import FastAPI
app = FastAPI()
@app.post("/index-weights/")
async def create_index_weights(weights: Dict[int, float]):
return weights

5
tests/test_tutorial/test_body_nested_models/test_tutorial009.py

@ -4,14 +4,11 @@ import pytest
from dirty_equals import IsDict from dirty_equals import IsDict
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from ...utils import needs_py39
@pytest.fixture( @pytest.fixture(
name="client", name="client",
params=[ params=[
"tutorial009", "tutorial009_py39",
pytest.param("tutorial009_py39", marks=needs_py39),
], ],
) )
def get_client(request: pytest.FixtureRequest): def get_client(request: pytest.FixtureRequest):

Loading…
Cancel
Save