Remove code examples for Python 3.8 in `path_operation_advanced_configuration`

This commit is contained in:
Yurii Motov 2025-12-11 09:24:49 +01:00
parent d3280749f1
commit f310e923bc
17 changed files with 16 additions and 117 deletions

View File

@ -12,7 +12,7 @@ You can set the OpenAPI `operationId` to be used in your *path operation* with t
You would have to make sure that it is unique for each operation. You would have to make sure that it is unique for each operation.
{* ../../docs_src/path_operation_advanced_configuration/tutorial001.py hl[6] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial001_py39.py hl[6] *}
### Using the *path operation function* name as the operationId { #using-the-path-operation-function-name-as-the-operationid } ### Using the *path operation function* name as the operationId { #using-the-path-operation-function-name-as-the-operationid }
@ -20,7 +20,7 @@ If you want to use your APIs' function names as `operationId`s, you can iterate
You should do it after adding all your *path operations*. You should do it after adding all your *path operations*.
{* ../../docs_src/path_operation_advanced_configuration/tutorial002.py hl[2, 12:21, 24] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial002_py39.py hl[2, 12:21, 24] *}
/// tip /// tip
@ -40,7 +40,7 @@ Even if they are in different modules (Python files).
To exclude a *path operation* from the generated OpenAPI schema (and thus, from the automatic documentation systems), use the parameter `include_in_schema` and set it to `False`: To exclude a *path operation* from the generated OpenAPI schema (and thus, from the automatic documentation systems), use the parameter `include_in_schema` and set it to `False`:
{* ../../docs_src/path_operation_advanced_configuration/tutorial003.py hl[6] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial003_py39.py hl[6] *}
## Advanced description from docstring { #advanced-description-from-docstring } ## Advanced description from docstring { #advanced-description-from-docstring }
@ -92,7 +92,7 @@ You can extend the OpenAPI schema for a *path operation* using the parameter `op
This `openapi_extra` can be helpful, for example, to declare [OpenAPI Extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions): This `openapi_extra` can be helpful, for example, to declare [OpenAPI Extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions):
{* ../../docs_src/path_operation_advanced_configuration/tutorial005.py hl[6] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial005_py39.py hl[6] *}
If you open the automatic API docs, your extension will show up at the bottom of the specific *path operation*. If you open the automatic API docs, your extension will show up at the bottom of the specific *path operation*.
@ -139,7 +139,7 @@ For example, you could decide to read and validate the request with your own cod
You could do that with `openapi_extra`: You could do that with `openapi_extra`:
{* ../../docs_src/path_operation_advanced_configuration/tutorial006.py hl[19:36, 39:40] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial006_py39.py hl[19:36, 39:40] *}
In this example, we didn't declare any Pydantic model. In fact, the request body is not even <abbr title="converted from some plain format, like bytes, into Python objects">parsed</abbr> as JSON, it is read directly as `bytes`, and the function `magic_data_reader()` would be in charge of parsing it in some way. In this example, we didn't declare any Pydantic model. In fact, the request body is not even <abbr title="converted from some plain format, like bytes, into Python objects">parsed</abbr> as JSON, it is read directly as `bytes`, and the function `magic_data_reader()` would be in charge of parsing it in some way.

View File

@ -1,30 +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.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
\f
:param item: User input.
"""
return item

View File

@ -1,34 +0,0 @@
from typing import List
import yaml
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, ValidationError
app = FastAPI()
class Item(BaseModel):
name: str
tags: List[str]
@app.post(
"/items/",
openapi_extra={
"requestBody": {
"content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
"required": True,
},
},
)
async def create_item(request: Request):
raw_body = await request.body()
try:
data = yaml.safe_load(raw_body)
except yaml.YAMLError:
raise HTTPException(status_code=422, detail="Invalid YAML")
try:
item = Item.model_validate(data)
except ValidationError as e:
raise HTTPException(status_code=422, detail=e.errors(include_url=False))
return item

View File

@ -1,34 +0,0 @@
from typing import List
import yaml
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, ValidationError
app = FastAPI()
class Item(BaseModel):
name: str
tags: List[str]
@app.post(
"/items/",
openapi_extra={
"requestBody": {
"content": {"application/x-yaml": {"schema": Item.schema()}},
"required": True,
},
},
)
async def create_item(request: Request):
raw_body = await request.body()
try:
data = yaml.safe_load(raw_body)
except yaml.YAMLError:
raise HTTPException(status_code=422, detail="Invalid YAML")
try:
item = Item.parse_obj(data)
except ValidationError as e:
raise HTTPException(status_code=422, detail=e.errors())
return item

View File

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial001 import app from docs_src.path_operation_advanced_configuration.tutorial001_py39 import app
client = TestClient(app) client = TestClient(app)

View File

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial002 import app from docs_src.path_operation_advanced_configuration.tutorial002_py39 import app
client = TestClient(app) client = TestClient(app)

View File

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial003 import app from docs_src.path_operation_advanced_configuration.tutorial003_py39 import app
client = TestClient(app) client = TestClient(app)

View File

@ -3,14 +3,13 @@ import importlib
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from ...utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2 from ...utils import needs_py310, needs_pydanticv1, needs_pydanticv2
@pytest.fixture( @pytest.fixture(
name="client", name="client",
params=[ params=[
pytest.param("tutorial004"), pytest.param("tutorial004_py39"),
pytest.param("tutorial004_py39", marks=needs_py39),
pytest.param("tutorial004_py310", marks=needs_py310), pytest.param("tutorial004_py310", marks=needs_py310),
], ],
) )

View File

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial005 import app from docs_src.path_operation_advanced_configuration.tutorial005_py39 import app
client = TestClient(app) client = TestClient(app)

View File

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial006 import app from docs_src.path_operation_advanced_configuration.tutorial006_py39 import app
client = TestClient(app) client = TestClient(app)

View File

@ -3,14 +3,13 @@ import importlib
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from ...utils import needs_py39, needs_pydanticv2 from ...utils import needs_pydanticv2
@pytest.fixture( @pytest.fixture(
name="client", name="client",
params=[ params=[
pytest.param("tutorial007"), pytest.param("tutorial007_py39"),
pytest.param("tutorial007_py39", marks=needs_py39),
], ],
) )
def get_client(request: pytest.FixtureRequest): def get_client(request: pytest.FixtureRequest):

View File

@ -3,14 +3,13 @@ import importlib
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from ...utils import needs_py39, needs_pydanticv1 from ...utils import needs_pydanticv1
@pytest.fixture( @pytest.fixture(
name="client", name="client",
params=[ params=[
pytest.param("tutorial007_pv1"), pytest.param("tutorial007_pv1_py39"),
pytest.param("tutorial007_pv1_py39", marks=needs_py39),
], ],
) )
def get_client(request: pytest.FixtureRequest): def get_client(request: pytest.FixtureRequest):