From 7e95c9776f778335b47645debd1393c44ab0edee Mon Sep 17 00:00:00 2001 From: lekss361 Date: Fri, 15 May 2026 13:24:24 +0300 Subject: [PATCH] fixup(cadastre): use importlib.util.find_spec instead of import (#168 PR3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause Blocker #2: `import app.workers.tasks.scrape_cadastre` at module top-level REBOUND `app` from FastAPI instance (line 14) to the Python package. All subsequent `app.dependency_overrides[get_db] = ...` failed with AttributeError because `app` was now the namespace module. Fix: probe module availability with importlib.util.find_spec — does not import or rebind names. FastAPI `app` instance stays intact. All 28 cadastre tests pass locally. --- backend/tests/api/v1/test_admin_cadastre.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/tests/api/v1/test_admin_cadastre.py b/backend/tests/api/v1/test_admin_cadastre.py index 8ca81225..72116c70 100644 --- a/backend/tests/api/v1/test_admin_cadastre.py +++ b/backend/tests/api/v1/test_admin_cadastre.py @@ -5,6 +5,12 @@ from __future__ import annotations +# scrape_cadastre зависит от app.scrapers.nspd_bulk_client (PR 2/5). +# Используем importlib.util.find_spec вместо прямого import — иначе +# `import app.workers...` пересоздаёт `app` как Python package и +# перебивает FastAPI instance, привязанный в строке выше → AttributeError +# на app.dependency_overrides. +import importlib.util from typing import Any from unittest.mock import MagicMock, patch @@ -13,13 +19,9 @@ from fastapi.testclient import TestClient from app.main import app -# scrape_cadastre зависит от app.scrapers.nspd_bulk_client (PR 2/5). -# Если ветка ещё не rebased на main — тесты которые импортируют task помечаем skip. -_SCRAPE_CADASTRE_AVAILABLE = True -try: - import app.workers.tasks.scrape_cadastre -except ModuleNotFoundError: - _SCRAPE_CADASTRE_AVAILABLE = False +_SCRAPE_CADASTRE_AVAILABLE = ( + importlib.util.find_spec("app.workers.tasks.scrape_cadastre") is not None +) requires_scrape_cadastre = pytest.mark.skipif( not _SCRAPE_CADASTRE_AVAILABLE,