Home / File/ test_ambiguous_params.py — fastapi Source File

test_ambiguous_params.py — fastapi Source File

Architecture documentation for test_ambiguous_params.py, a python file in the fastapi codebase. 6 imports, 0 dependents.

File python FastAPI Responses 6 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  9588c53b_26fc_238d_e474_2a9ae27eef2d["test_ambiguous_params.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> 0dda2280_3359_8460_301c_e98c77e78185
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  24a9a43e_697f_81ce_6a7c_28a423a6f93b["param_functions.py"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> 24a9a43e_697f_81ce_6a7c_28a423a6f93b
  8864e1f8_8173_ce5c_602b_445aa33f20f8["Query"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> 8864e1f8_8173_ce5c_602b_445aa33f20f8
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  9588c53b_26fc_238d_e474_2a9ae27eef2d --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 9588c53b_26fc_238d_e474_2a9ae27eef2d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Annotated

import pytest
from fastapi import Depends, FastAPI, Path
from fastapi.param_functions import Query
from fastapi.testclient import TestClient

app = FastAPI()


def test_no_annotated_defaults():
    with pytest.raises(
        AssertionError, match="Path parameters cannot have a default value"
    ):

        @app.get("/items/{item_id}/")
        async def get_item(item_id: Annotated[int, Path(default=1)]):
            pass  # pragma: nocover

    with pytest.raises(
        AssertionError,
        match=(
            "`Query` default value cannot be set in `Annotated` for 'item_id'. Set the"
            " default value with `=` instead."
        ),
    ):

        @app.get("/")
        async def get(item_id: Annotated[int, Query(default=1)]):
            pass  # pragma: nocover


def test_multiple_annotations():
    async def dep():
        pass  # pragma: nocover

    @app.get("/multi-query")
    async def get(foo: Annotated[int, Query(gt=2), Query(lt=10)]):
        return foo

    with pytest.raises(
        AssertionError,
        match=(
            "Cannot specify `Depends` in `Annotated` and default value"
            " together for 'foo'"
        ),
    ):

        @app.get("/")
        async def get2(foo: Annotated[int, Depends(dep)] = Depends(dep)):
            pass  # pragma: nocover

    with pytest.raises(
        AssertionError,
        match=(
            "Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a"
            " default value together for 'foo'"
        ),
    ):

        @app.get("/")
        async def get3(foo: Annotated[int, Query(min_length=1)] = Depends(dep)):
            pass  # pragma: nocover

    client = TestClient(app)
    response = client.get("/multi-query", params={"foo": "5"})
    assert response.status_code == 200
    assert response.json() == 5

    response = client.get("/multi-query", params={"foo": "123"})
    assert response.status_code == 422

    response = client.get("/multi-query", params={"foo": "1"})
    assert response.status_code == 422

Domain

Subdomains

Frequently Asked Questions

What does test_ambiguous_params.py do?
test_ambiguous_params.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_ambiguous_params.py?
test_ambiguous_params.py defines 2 function(s): test_multiple_annotations, test_no_annotated_defaults.
What does test_ambiguous_params.py depend on?
test_ambiguous_params.py imports 6 module(s): Query, __init__.py, param_functions.py, pytest, testclient.py, typing.
Where is test_ambiguous_params.py in the architecture?
test_ambiguous_params.py is located at tests/test_ambiguous_params.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free