mirror of https://github.com/tiangolo/fastapi.git
Remove code examples for Python 3.8 in `settings`
This commit is contained in:
parent
2e792112bb
commit
786773ac11
|
|
@ -62,7 +62,7 @@ You can use all the same validation features and tools you use for Pydantic mode
|
|||
|
||||
//// tab | Pydantic v2
|
||||
|
||||
{* ../../docs_src/settings/tutorial001.py hl[2,5:8,11] *}
|
||||
{* ../../docs_src/settings/tutorial001_py39.py hl[2,5:8,11] *}
|
||||
|
||||
////
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ In Pydantic v1 you would import `BaseSettings` directly from `pydantic` instead
|
|||
|
||||
///
|
||||
|
||||
{* ../../docs_src/settings/tutorial001_pv1.py hl[2,5:8,11] *}
|
||||
{* ../../docs_src/settings/tutorial001_pv1_py39.py hl[2,5:8,11] *}
|
||||
|
||||
////
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ Next it will convert and validate the data. So, when you use that `settings` obj
|
|||
|
||||
Then you can use the new `settings` object in your application:
|
||||
|
||||
{* ../../docs_src/settings/tutorial001.py hl[18:20] *}
|
||||
{* ../../docs_src/settings/tutorial001_py39.py hl[18:20] *}
|
||||
|
||||
### Run the server { #run-the-server }
|
||||
|
||||
|
|
@ -126,11 +126,11 @@ You could put those settings in another module file as you saw in [Bigger Applic
|
|||
|
||||
For example, you could have a file `config.py` with:
|
||||
|
||||
{* ../../docs_src/settings/app01/config.py *}
|
||||
{* ../../docs_src/settings/app01_py39/config.py *}
|
||||
|
||||
And then use it in a file `main.py`:
|
||||
|
||||
{* ../../docs_src/settings/app01/main.py hl[3,11:13] *}
|
||||
{* ../../docs_src/settings/app01_py39/main.py hl[3,11:13] *}
|
||||
|
||||
/// tip
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
from functools import lru_cache
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
from typing_extensions import Annotated
|
||||
|
||||
from .config import Settings
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings():
|
||||
return Settings()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: Annotated[Settings, Depends(get_settings)]):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from .config import Settings
|
||||
from .main import app, get_settings
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def get_settings_override():
|
||||
return Settings(admin_email="testing_admin@example.com")
|
||||
|
||||
|
||||
app.dependency_overrides[get_settings] = get_settings_override
|
||||
|
||||
|
||||
def test_app():
|
||||
response = client.get("/info")
|
||||
data = response.json()
|
||||
assert data == {
|
||||
"app_name": "Awesome API",
|
||||
"admin_email": "testing_admin@example.com",
|
||||
"items_per_user": 50,
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env")
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
from functools import lru_cache
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
from typing_extensions import Annotated
|
||||
|
||||
from . import config
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings():
|
||||
return config.Settings()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: Annotated[config.Settings, Depends(get_settings)]):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
|
|
@ -4,15 +4,14 @@ from types import ModuleType
|
|||
import pytest
|
||||
from pytest import MonkeyPatch
|
||||
|
||||
from ...utils import needs_py39, needs_pydanticv2
|
||||
from ...utils import needs_pydanticv2
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod_path",
|
||||
params=[
|
||||
pytest.param("app02"),
|
||||
pytest.param("app02_an"),
|
||||
pytest.param("app02_an_py39", marks=needs_py39),
|
||||
pytest.param("app02_py39"),
|
||||
pytest.param("app02_an_py39"),
|
||||
],
|
||||
)
|
||||
def get_mod_path(request: pytest.FixtureRequest):
|
||||
|
|
|
|||
|
|
@ -5,15 +5,14 @@ import pytest
|
|||
from fastapi.testclient import TestClient
|
||||
from pytest import MonkeyPatch
|
||||
|
||||
from ...utils import needs_py39, needs_pydanticv1, needs_pydanticv2
|
||||
from ...utils import needs_pydanticv1, needs_pydanticv2
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod_path",
|
||||
params=[
|
||||
pytest.param("app03"),
|
||||
pytest.param("app03_an"),
|
||||
pytest.param("app03_an_py39", marks=needs_py39),
|
||||
pytest.param("app03_py39"),
|
||||
pytest.param("app03_an_py39"),
|
||||
],
|
||||
)
|
||||
def get_mod_path(request: pytest.FixtureRequest):
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ from ...utils import needs_pydanticv1, needs_pydanticv2
|
|||
@pytest.fixture(
|
||||
name="app",
|
||||
params=[
|
||||
pytest.param("tutorial001", marks=needs_pydanticv2),
|
||||
pytest.param("tutorial001_pv1", marks=needs_pydanticv1),
|
||||
pytest.param("tutorial001_py39", marks=needs_pydanticv2),
|
||||
pytest.param("tutorial001_pv1_py39", marks=needs_pydanticv1),
|
||||
],
|
||||
)
|
||||
def get_app(request: pytest.FixtureRequest, monkeypatch: MonkeyPatch):
|
||||
|
|
|
|||
Loading…
Reference in New Issue