From e8221c6334101f9ee8a4c179ea4a5ce4f56ab499 Mon Sep 17 00:00:00 2001 From: Kirill Mokevnin Date: Wed, 12 Aug 2026 20:16:33 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B1=D0=BE=D1=80=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BB=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B0=20=D1=8F?= =?UTF-8?q?=D0=B2=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Без select ruff берёт набор по умолчанию, а он меняется вместе с версией: на 0.16 добавилась сортировка импортов и часть проверок pycodestyle, и обновление ruff роняло CI на коде, который никто не трогал. Набор задан такой же, как у остальной python-пачки: E, F, C90. Найденное этим набором исправлено: пустые строки и пробелы вокруг равно автоправкой ruff, три длинные строки разбиты руками. Склейка user_agent в тесте даёт ровно ту же строку, что была. Co-Authored-By: Claude Opus 5 (1M context) --- app/server.py | 21 ++++++++++++++++----- ruff.toml | 9 +++++++++ tests/test_server.py | 7 ++++++- 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 ruff.toml diff --git a/app/server.py b/app/server.py index 713e1db..91e602a 100644 --- a/app/server.py +++ b/app/server.py @@ -23,10 +23,14 @@ format="%(asctime)s %(levelname)s %(name)s %(message)s", ) + @asynccontextmanager async def lifespan(app: FastAPI): if not DATABASE_URL: - raise RuntimeError("DATABASE_URL is not set. Define it in the environment or .env file.") + raise RuntimeError( + "DATABASE_URL is not set. " + "Define it in the environment or .env file." + ) try: app.state.pool = await asyncpg.create_pool( DATABASE_URL, @@ -36,14 +40,19 @@ async def lifespan(app: FastAPI): logging.info("Connected to database successfully") except Exception as e: logging.error(f"Failed to connect to the database: {str(e)}") - raise RuntimeError("Failed to connect to the database. Check DATABASE_URL and DB availability.") from e + raise RuntimeError( + "Failed to connect to the database. " + "Check DATABASE_URL and DB availability." + ) from e FastAPICache.init(InMemoryBackend()) yield await app.state.pool.close() + app = FastAPI(lifespan=lifespan) app.add_middleware(GZipMiddleware, minimum_size=1000) + async def get_db(): async with app.state.pool.acquire() as conn: yield conn @@ -53,8 +62,9 @@ async def get_db(): async def index(): return {"status": "It Works"} + @app.get("/health") -async def health(db = Depends(get_db)): +async def health(db=Depends(get_db)): try: await db.execute("SELECT 1") return {"status": "ok", "db": "ok"} @@ -100,7 +110,7 @@ async def generator(): async def get_visits( begin: str = Query(..., description="Start date in ISO format"), end: str = Query(..., description="End date in ISO format"), - db = Depends(get_db) + db=Depends(get_db) ): begin = datetime.fromisoformat(begin) end = datetime.fromisoformat(end) @@ -118,11 +128,12 @@ async def get_visits( detail="Internal server error occurred while fetching visits" ) + @app.get("/registrations") async def get_registrations( begin: str = Query(..., description="Start date in ISO format"), end: str = Query(..., description="End date in ISO format"), - db = Depends(get_db) + db=Depends(get_db) ): begin = datetime.fromisoformat(begin) end = datetime.fromisoformat(end) diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..4677d44 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,9 @@ +line-length = 80 + +[lint.per-file-ignores] +# init modules can contain the local imports, logic, unused imports +"__init__.py" = ["F401"] + +[lint] +preview = true +select = ["E", "F", "C90"] diff --git a/tests/test_server.py b/tests/test_server.py index b517336..5678481 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -4,6 +4,7 @@ PORT = os.getenv('PORT') + def test_server(): response = requests.get('http://localhost:4000/') assert response.status_code == 200 @@ -14,6 +15,10 @@ def test_server(): assert response.json()[0] == { "datetime": "2023-03-01T10:36:22", "platform": "web", - "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.6", + "user_agent": ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.6" + ), "visit_id": "1de9ea66-70d3-4a1f-8735-df5ef7697fb9" }