From 9c554d54cd401d213e17b153a4718adfa78d0451 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 29 Apr 2026 01:12:02 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/cbx.py | 5 ++--- tests/test_cbx.py | 37 +++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/fastapi/cbx.py b/fastapi/cbx.py index cdf6184d6d..263bc3d49d 100644 --- a/fastapi/cbx.py +++ b/fastapi/cbx.py @@ -57,8 +57,7 @@ class CBR(Generic[T]): self.instance = self.cls(*args, **kwargs) for _name, endpoint in inspect.getmembers( - self.instance, lambda x: inspect.ismethod( - x) or inspect.isfunction(x) + self.instance, lambda x: inspect.ismethod(x) or inspect.isfunction(x) ): if cbx_router := endpoint.__annotations__.get("cbx_router"): for router in cbx_router: @@ -100,7 +99,7 @@ class cbr(Generic[T]): else: endpoint.__annotations__.setdefault( "cbx_router", - [{"method": self.method, "path": self.path, "kwargs": self.kwargs}] + [{"method": self.method, "path": self.path, "kwargs": self.kwargs}], ) return endpoint diff --git a/tests/test_cbx.py b/tests/test_cbx.py index 45e0ef0c3f..6bf34e7593 100644 --- a/tests/test_cbx.py +++ b/tests/test_cbx.py @@ -1,23 +1,21 @@ -import logging import asyncio -from fastapi.cbx import cbv, cbr +import logging + +from fastapi import APIRouter, Cookie, Depends, FastAPI, HTTPException, Response, status +from fastapi.cbx import cbr, cbv from fastapi.testclient import TestClient -from fastapi import APIRouter -from typing import Dict -from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException from pydantic import BaseModel -@cbv(router=APIRouter(prefix='/cbv')) +@cbv(router=APIRouter(prefix="/cbv")) class MyCBV: - logger = logging.getLogger(__qualname__) class CBVModel(BaseModel): key: str = "cbv" value: str = "Class-based view for CRUD operations with singleton global dependency injection" - def __init__(self, **kwargs: Dict[str, str]): + def __init__(self, **kwargs: dict[str, str]): self.heavies = { "name": "fastapi-cbx", "description": "Minimal class-based routing extension for FastAPI", @@ -34,22 +32,23 @@ class MyCBV: async def get(self, key: str) -> CBVModel: self.logger.info(f"GET {key}") await asyncio.sleep(1) - return self.CBVModel(key=key, value=self.heavies.get(key, "One scenario, one route")) + return self.CBVModel( + key=key, value=self.heavies.get(key, "One scenario, one route") + ) @cbr(router=APIRouter(prefix="/cbr")) class MyCBR: - logger = logging.getLogger(__qualname__) class CBRModel(BaseModel): key: str = "CBR" value: str = "Class-based route for complex business logic with multiple endpoints and method-level dependencies" - def __init__(self, **kwargs: Dict[str, str]): + def __init__(self, **kwargs: dict[str, str]): self.heavies = { "name": "fastapi-cbx", - "description": "Minimal class-based routing extension for FastAPI" + "description": "Minimal class-based routing extension for FastAPI", } self.heavies.update(kwargs) @@ -63,13 +62,16 @@ class MyCBR: @cbr.get("/heavies", summary="Get heavies by key") async def get_heavies(self, key: str) -> CBRModel: self.logger.info(f"GET {key}") - return self.CBRModel(key=key, value=self.heavies.get(key, "One scenario, one route")) + return self.CBRModel( + key=key, value=self.heavies.get(key, "One scenario, one route") + ) @staticmethod def session(token: str = Cookie(default="", alias="token")) -> str: if token != "fastapi-cbx": raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") + status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token" + ) return token @cbr.post("/heavies", summary="Set heavies") @@ -106,6 +108,7 @@ client = TestClient(app) # ==================== 100% Test Suite ==================== + def test_cbv(): asyncio.run(MyCBV.head(Response())) print(dir(MyCBV)) @@ -118,15 +121,13 @@ def test_cbr(): print(dir(MyCBR)) response = client.get("/cbr/heavies?key=name") assert response.status_code == 200 - response = client.post( - "/cbr/heavies", json={"key": "test", "value": "test_value"}) + response = client.post("/cbr/heavies", json={"key": "test", "value": "test_value"}) assert response.status_code == 401 assert response.json()["detail"] == "Invalid token" with TestClient(app, cookies={"token": "fastapi-cbx"}) as auth_client: response = auth_client.post( - "/cbr/heavies", - json={"key": "test", "value": "test"} + "/cbr/heavies", json={"key": "test", "value": "test"} ) assert response.status_code == 200 response = client.delete("/cbr/heavies?name=test")