fixup(cadastre): use importlib.util.find_spec instead of import (#168 PR3)
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.
This commit is contained in:
parent
32eeda5db1
commit
7e95c9776f
1 changed files with 9 additions and 7 deletions
|
|
@ -5,6 +5,12 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
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 typing import Any
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
|
@ -13,13 +19,9 @@ from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from app.main import app
|
from app.main import app
|
||||||
|
|
||||||
# scrape_cadastre зависит от app.scrapers.nspd_bulk_client (PR 2/5).
|
_SCRAPE_CADASTRE_AVAILABLE = (
|
||||||
# Если ветка ещё не rebased на main — тесты которые импортируют task помечаем skip.
|
importlib.util.find_spec("app.workers.tasks.scrape_cadastre") is not None
|
||||||
_SCRAPE_CADASTRE_AVAILABLE = True
|
)
|
||||||
try:
|
|
||||||
import app.workers.tasks.scrape_cadastre
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
_SCRAPE_CADASTRE_AVAILABLE = False
|
|
||||||
|
|
||||||
requires_scrape_cadastre = pytest.mark.skipif(
|
requires_scrape_cadastre = pytest.mark.skipif(
|
||||||
not _SCRAPE_CADASTRE_AVAILABLE,
|
not _SCRAPE_CADASTRE_AVAILABLE,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue