mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 05:15:46 -04:00
📝 Update source examples and docs from Python 3.9 to 3.10 (#14900)
This commit is contained in:
committed by
GitHub
parent
d06ab3f5c7
commit
c9e2277d8b
0
docs_src/settings/app01_py310/__init__.py
Normal file
0
docs_src/settings/app01_py310/__init__.py
Normal file
@@ -1,4 +1,4 @@
|
||||
from pydantic.v1 import BaseSettings
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
@@ -6,5 +6,5 @@ class Settings(BaseSettings):
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
settings = Settings()
|
||||
14
docs_src/settings/app01_py310/main.py
Normal file
14
docs_src/settings/app01_py310/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .config import settings
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info():
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
0
docs_src/settings/app02_an_py310/__init__.py
Normal file
0
docs_src/settings/app02_an_py310/__init__.py
Normal file
@@ -1,10 +1,7 @@
|
||||
from pydantic.v1 import BaseSettings
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
22
docs_src/settings/app02_an_py310/main.py
Normal file
22
docs_src/settings/app02_an_py310/main.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from functools import lru_cache
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
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,
|
||||
}
|
||||
23
docs_src/settings/app02_an_py310/test_main.py
Normal file
23
docs_src/settings/app02_an_py310/test_main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
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,
|
||||
}
|
||||
0
docs_src/settings/app02_py310/__init__.py
Normal file
0
docs_src/settings/app02_py310/__init__.py
Normal file
7
docs_src/settings/app02_py310/config.py
Normal file
7
docs_src/settings/app02_py310/config.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "Awesome API"
|
||||
admin_email: str
|
||||
items_per_user: int = 50
|
||||
21
docs_src/settings/app02_py310/main.py
Normal file
21
docs_src/settings/app02_py310/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from .config import Settings
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings():
|
||||
return Settings()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: Settings = Depends(get_settings)):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
23
docs_src/settings/app02_py310/test_main.py
Normal file
23
docs_src/settings/app02_py310/test_main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
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,
|
||||
}
|
||||
0
docs_src/settings/app03_an_py310/__init__.py
Normal file
0
docs_src/settings/app03_an_py310/__init__.py
Normal file
9
docs_src/settings/app03_an_py310/config.py
Normal file
9
docs_src/settings/app03_an_py310/config.py
Normal file
@@ -0,0 +1,9 @@
|
||||
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")
|
||||
22
docs_src/settings/app03_an_py310/main.py
Normal file
22
docs_src/settings/app03_an_py310/main.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from functools import lru_cache
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
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,
|
||||
}
|
||||
0
docs_src/settings/app03_py310/__init__.py
Normal file
0
docs_src/settings/app03_py310/__init__.py
Normal file
9
docs_src/settings/app03_py310/config.py
Normal file
9
docs_src/settings/app03_py310/config.py
Normal file
@@ -0,0 +1,9 @@
|
||||
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")
|
||||
21
docs_src/settings/app03_py310/main.py
Normal file
21
docs_src/settings/app03_py310/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from . import config
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings():
|
||||
return config.Settings()
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info(settings: config.Settings = Depends(get_settings)):
|
||||
return {
|
||||
"app_name": settings.app_name,
|
||||
"admin_email": settings.admin_email,
|
||||
"items_per_user": settings.items_per_user,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
from fastapi import FastAPI
|
||||
from pydantic.v1 import BaseSettings
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
Reference in New Issue
Block a user