Skip to content

๐Ÿ”ฌ ๐Ÿ”— โฎ๏ธ ๐Ÿ”

๐Ÿ”‘ ๐Ÿ”— โฎ๏ธ ๐Ÿ”ฌ

๐Ÿ“ค ๐Ÿ˜ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿ” ๐Ÿ”— โฎ๏ธ ๐Ÿ”ฌ.

๐Ÿ‘† ๐Ÿšซ ๐Ÿ’š โฎ๏ธ ๐Ÿ”— ๐Ÿƒ (๐Ÿšซ ๐Ÿ™† ๐ŸŽง-๐Ÿ”— โšซ๏ธ ๐Ÿ’ช โœ”๏ธ).

โ†ฉ๏ธ, ๐Ÿ‘† ๐Ÿ’š ๐Ÿšš ๐ŸŽ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ”œ โš™๏ธ ๐Ÿ•ด โฎ๏ธ ๐Ÿ’ฏ (๐ŸŽฒ ๐Ÿ•ด ๐ŸŽฏ ๐Ÿ’ฏ), & ๐Ÿ”œ ๐Ÿšš ๐Ÿ’ฒ ๐Ÿ‘ˆ ๐Ÿ’ช โš™๏ธ ๐ŸŒโ” ๐Ÿ’ฒ โฎ๏ธ ๐Ÿ”— โš™๏ธ.

โš™๏ธ ๐Ÿ’ผ: ๐Ÿ”ข ๐Ÿ•โ€๐Ÿฆบ

๐Ÿ–ผ ๐Ÿ’ช ๐Ÿ‘ˆ ๐Ÿ‘† โœ”๏ธ ๐Ÿ”ข ๐Ÿค ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค™.

๐Ÿ‘† ๐Ÿ“จ โšซ๏ธ ๐Ÿค & โšซ๏ธ ๐Ÿ“จ ๐Ÿ”“ ๐Ÿ‘ฉโ€๐Ÿ’ป.

๐Ÿ‘‰ ๐Ÿ•โ€๐Ÿฆบ 5๏ธโƒฃ๐Ÿ“† ๐Ÿ”Œ ๐Ÿ‘† ๐Ÿ“ ๐Ÿ“จ, & ๐Ÿค™ โšซ๏ธ ๐Ÿ’ช โœŠ โž• ๐Ÿ•ฐ ๐ŸŒ˜ ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿ”ง ๐ŸŽ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ’ฏ.

๐Ÿ‘† ๐ŸŽฒ ๐Ÿ’š ๐Ÿ’ฏ ๐Ÿ”ข ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ•, โœ‹๏ธ ๐Ÿšซ ๐ŸŽฏ ๐Ÿค™ โšซ๏ธ ๐Ÿ”  ๐Ÿ’ฏ ๐Ÿ‘ˆ ๐Ÿƒ.

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ” ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿค™ ๐Ÿ‘ˆ ๐Ÿ•โ€๐Ÿฆบ, & โš™๏ธ ๐Ÿ›ƒ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ“จ ๐ŸŽ ๐Ÿ‘ฉโ€๐Ÿ’ป, ๐Ÿ•ด ๐Ÿ‘† ๐Ÿ’ฏ.

โš™๏ธ app.dependency_overrides ๐Ÿ”ข

๐Ÿ‘ซ ๐Ÿ’ผ, ๐Ÿ‘† FastAPI ๐Ÿˆธ โœ”๏ธ ๐Ÿ”ข app.dependency_overrides, โšซ๏ธ ๐Ÿ™… dict.

๐Ÿ” ๐Ÿ”— ๐Ÿ”ฌ, ๐Ÿ‘† ๐Ÿšฎ ๐Ÿ”‘ โฎ๏ธ ๐Ÿ”— (๐Ÿ”ข), & ๐Ÿ’ฒ, ๐Ÿ‘† ๐Ÿ”— ๐Ÿ” (โž•1๏ธโƒฃ ๐Ÿ”ข).

& โคด๏ธ FastAPI ๐Ÿ”œ ๐Ÿค™ ๐Ÿ‘ˆ ๐Ÿ” โ†ฉ๏ธ โฎ๏ธ ๐Ÿ”—.

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient

app = FastAPI()


async def common_parameters(
    q: Union[str, None] = None, skip: int = 0, limit: int = 100
):
    return {"q": q, "skip": skip, "limit": limit}


@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
    return {"message": "Hello Items!", "params": commons}


@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
    return {"message": "Hello Users!", "params": commons}


client = TestClient(app)


async def override_dependency(q: Union[str, None] = None):
    return {"q": q, "skip": 5, "limit": 10}


app.dependency_overrides[common_parameters] = override_dependency


def test_override_in_items():
    response = client.get("/items/")
    assert response.status_code == 200
    assert response.json() == {
        "message": "Hello Items!",
        "params": {"q": None, "skip": 5, "limit": 10},
    }


def test_override_in_items_with_q():
    response = client.get("/items/?q=foo")
    assert response.status_code == 200
    assert response.json() == {
        "message": "Hello Items!",
        "params": {"q": "foo", "skip": 5, "limit": 10},
    }


def test_override_in_items_with_params():
    response = client.get("/items/?q=foo&skip=100&limit=200")
    assert response.status_code == 200
    assert response.json() == {
        "message": "Hello Items!",
        "params": {"q": "foo", "skip": 5, "limit": 10},
    }

Tip

๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ”— ๐Ÿ” ๐Ÿ”— โš™๏ธ ๐Ÿ™† ๐Ÿ‘† FastAPI ๐Ÿˆธ.

โฎ๏ธ ๐Ÿ”— ๐Ÿ’ช โš™๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ (๐Ÿ•โ” ๐Ÿ‘† ๐Ÿšซ โš™๏ธ ๐Ÿ“จ ๐Ÿ’ฒ), .include_router() ๐Ÿค™, โ™’๏ธ.

FastAPI ๐Ÿ”œ ๐Ÿ’ช ๐Ÿ” โšซ๏ธ.

โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช โฒ ๐Ÿ‘† ๐Ÿ” (โŽ ๐Ÿ‘ซ) โš’ app.dependency_overrides ๐Ÿ› dict:

app.dependency_overrides = {}

Tip

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ” ๐Ÿ”— ๐Ÿ•ด โฎ๏ธ ๐Ÿ’ฏ, ๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ” โ–ถ๏ธ ๐Ÿ’ฏ (๐Ÿ”˜ ๐Ÿ’ฏ ๐Ÿ”ข) & โฒ โšซ๏ธ ๐Ÿ”š (๐Ÿ”š ๐Ÿ’ฏ ๐Ÿ”ข).