From 40d2ffe0f762739aae1efb41b141f2826be10b89 Mon Sep 17 00:00:00 2001 From: Hermes CI Fix Date: Sun, 4 Oct 2026 18:03:34 +0800 Subject: [PATCH] fix(dupont): None guard for raw formatting in /reports/dupont insight detail Non-ending stock windows (e.g. ?period=2026-Q2 / 2026-05 / custom) return raw total_assets/equity as None when the window has no subject_balances level-1 rows; identity guard reports status 'unavailable' (not 'identity_broken') and execution continues into the insight detail f-string, where .2f on None raised TypeError -> HTTP 500. Add explicit fmt() ('-' for None) for the four raw columns in detail. Task: fullstack-20261004-1544 (P1; proposer wecom-fullstack; ASK-20261004-07 follow-up). No DDL. Single file. --- backend/app/api/reports.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/app/api/reports.py b/backend/app/api/reports.py index 5f64fa63..5fedd5a0 100644 --- a/backend/app/api/reports.py +++ b/backend/app/api/reports.py @@ -2414,11 +2414,15 @@ def get_dupont_analysis( roe_exact = f_ann.get("roe") roe_change = None if (roe_exact is None or prev_roe is None) else round(roe_exact - prev_roe, 4) + def _fmt(v): + # 存量/流量缺失时 raw 值为 None(如博海存量仅到区间外)⇒ 显式「-」,不得 None 格式化 + return "-" if v is None else f"{v:.2f}" + detail = (f"{label}口径(流量累计 {win['period_start']}~{win['period_end']}," f"存量{'平均' if cur['basis'] == 'average' else '期末回落'}" f"{',年化 ×12/' + str(win['months']) if annualize else ',非年化'}):" - f"净利润 {cur['raw']['net_profit']:.2f}万 / 营业收入 {cur['raw']['revenue']:.2f}万 / " - f"平均总资产 {cur['raw']['total_assets']:.2f}万 / 平均净资产 {cur['raw']['equity']:.2f}万;" + f"净利润 {_fmt(cur['raw']['net_profit'])}万 / 营业收入 {_fmt(cur['raw']['revenue'])}万 / " + f"平均总资产 {_fmt(cur['raw']['total_assets'])}万 / 平均净资产 {_fmt(cur['raw']['equity'])}万;" f"ROE = {factors['net_profit_margin']['value']}% × {factors['asset_turnover']['value']} × " f"{factors['equity_multiplier']['value']} = {None if roe_exact is None else round(roe_exact, 2)}%" f"(恒等式 {'通过' if identity.get('status') == 'pass' else identity.get('status')},"