From 757859bc18f563dc31f496f3a5bc62700b6c5d9a Mon Sep 17 00:00:00 2001 From: Justin Gu <97915@qq.com> Date: Sat, 5 Sep 2026 12:18:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20mypy=20=E4=B8=A5=E6=A0=BC=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D=20=E2=80=94=20=E7=83=AD=E7=82=B9?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E7=8A=B6=E6=80=81=E6=94=B6=E7=AA=84=E4=B8=8E?= =?UTF-8?q?=20Task=20=E6=B3=9B=E5=9E=8B=E5=8F=82=E6=95=B0=EF=BC=88CI=20myp?= =?UTF-8?q?y=20job=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/easy_tdx/web/routers/board_mac.py | 31 +++++++++++++++------------ src/easy_tdx/web/sentiment_sampler.py | 4 ++-- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/easy_tdx/web/routers/board_mac.py b/src/easy_tdx/web/routers/board_mac.py index afeefa7..78002c5 100644 --- a/src/easy_tdx/web/routers/board_mac.py +++ b/src/easy_tdx/web/routers/board_mac.py @@ -352,19 +352,23 @@ def _hotspot_history_or_build( cached = _hotspot_history_cache.get(key) if cached is not None and cached[0] == _today_str(): return cached[1], None - state = _hotspot_builds.get(key) - running = state is not None and state.get("task") is not None and not state["task"].done() + state: dict[str, Any] | None = _hotspot_builds.get(key) + task: Any = state.get("task") if state else None + running = task is not None and not task.done() # 需要新建:无状态 / 上次成功但缓存已过期 / 显式重试 - if not running and (retry or state is None or state.get("status") == "ready"): - state = {"status": "building", "progress": 0.0, "task": None, "error": ""} - _hotspot_builds[key] = state - state["task"] = asyncio.create_task(_hotspot_build(key, bt, client)) - running = True + if not running: + need_build = retry or state is None or state.get("status") == "ready" + if need_build: + state = {"status": "building", "progress": 0.0, "task": None, "error": ""} + _hotspot_builds[key] = state + state["task"] = asyncio.create_task(_hotspot_build(key, bt, client)) + running = True + assert state is not None # running 分支的 state 必然存在;error 分支同理 if running: - return None, {"status": "building", "progress": state.get("progress", 0.0)} + return None, {"status": "building", "progress": float(state.get("progress", 0.0))} return None, { "status": "error", - "error": state.get("error") or "热点矩阵构建失败", + "error": str(state.get("error") or "热点矩阵构建失败"), "progress": 1.0, } @@ -397,8 +401,8 @@ async def board_hotspot( key = bt.name history, build_payload = _hotspot_history_or_build(key, bt, client, retry=retry) - if build_payload is not None: - return DictResponse.from_dict(build_payload) + if build_payload is not None or history is None: + return DictResponse.from_dict(build_payload or {"status": "error"}) axis_all: list[str] = history["axis"] pct_map: dict[str, dict[str, float]] = history["pct"] names: dict[str, str] = dict(history["names"]) @@ -533,9 +537,8 @@ async def board_hotspot_correlation( key = bt.name history, build_payload = _hotspot_history_or_build(key, bt, client) - if build_payload is not None: - return DictResponse.from_dict(build_payload) - + if build_payload is not None or history is None: + return DictResponse.from_dict(build_payload or {"status": "error"}) axis_all: list[str] = history["axis"] pct_map: dict[str, dict[str, float]] = history["pct"] names: dict[str, str] = dict(history["names"]) diff --git a/src/easy_tdx/web/sentiment_sampler.py b/src/easy_tdx/web/sentiment_sampler.py index f36e6b5..49f598c 100644 --- a/src/easy_tdx/web/sentiment_sampler.py +++ b/src/easy_tdx/web/sentiment_sampler.py @@ -46,7 +46,7 @@ class SentimentSampler: self._get_stat = client_get_stat self._store = store or get_sentiment_store() self._interval = interval - self._task: asyncio.Task | None = None + self._task: asyncio.Task[None] | None = None self.samples = 0 self.failures = 0 @@ -122,7 +122,7 @@ class FundFlowSampler: self._interval = interval self._top_n = top_n self._keep = keep - self._task: asyncio.Task | None = None + self._task: asyncio.Task[None] | None = None def start(self) -> None: if self._task is None or self._task.done():