Merge branch 'fix/pyright-env-and-type-guards' into dev

This commit is contained in:
yml2213
2026-10-08 12:34:11 +08:00
31 changed files with 599 additions and 96 deletions
-1
View File
@@ -57,7 +57,6 @@ from ..services.account_lifecycle import (
set_manual_state,
)
from ..services.account_service import (
cookie_account_ids_query,
parse_and_build_accounts,
)
from ..services.account_value import (
+5 -3
View File
@@ -58,10 +58,12 @@ def _audit_log_out(db: Session, row: AuditLog) -> dict:
for task, account in tasks_and_accounts:
task_result = task.result if isinstance(task.result, dict) else {}
channel = str(task_result.get("recharge_channel") or "")
# 先落到局部变量再判 isinstance:直接写
# `x.get(k) if isinstance(x.get(k), dict) else {}` 是两次独立取值,
# 类型检查器无法据此收窄,会报「可能为 None」。
raw_supplier_order = task_result.get("supplier_order")
supplier_order = (
task_result.get("supplier_order")
if isinstance(task_result.get("supplier_order"), dict)
else {}
raw_supplier_order if isinstance(raw_supplier_order, dict) else {}
)
supplier_order_id = str(
supplier_order.get("order_id")
+13 -1
View File
@@ -544,4 +544,16 @@ def _unbind_device_accounts(accounts: list[str]) -> tuple[list[str], list[str]]:
return removed, missing
__all__ = [name for name in globals() if not name.startswith("__")]
# 运行期导出清单:显式列出「非 dunder」的全部模块级名字(**含下划线开头的内部
# helper**),因为 python 的 `import *` 只要模块定义了 `__all__` 就照单全收。
#
# ⚠️ 六个 huya 路由模块(accounts/cookies/register/workbench/login/devices)原本
# 用 `from ._common import *`,全部已改为**显式导入**。原因不是风格洁癖:pyright
# 不会执行上面的推导式,只能按 PEP 8 保守假设「下划线名不导出」,于是那 108 处
# helper 调用全被报成「未定义」—— 更糟的是,它让这 6 个模块的类型检查彻底失明
# (helper 改名/删除时静态检查一声不吭,只能等运行期 500;曾因此漏掉一个
# `ImportError` 死端点)。新增代码请一律用显式导入。
#
# 这里保留计算式 `__all__` 是为了不改动任何运行期行为:万一还有别处 `import *`,
# 行为与改造前完全一致。pyright 无法静态求值它,故就地忽略该诊断。
__all__ = [name for name in globals() if not name.startswith("__")] # pyright: ignore[reportUnsupportedDunderAll]
+47 -1
View File
@@ -2,7 +2,53 @@
from fastapi import APIRouter
from ._common import *
from ._common import (
DEFAULT_EXCHANGE_TARGET,
AccountAssign,
AccountBulkSelection,
AccountBulkTag,
AccountTag,
BatchAssign,
Depends,
HTTPException,
HuyaAccount,
HuyaBrowserExchangeRequest,
HuyaCookieImport,
HuyaPasswordAccountImport,
HuyaTask,
HuyaWorkbenchAccount,
Query,
Session,
User,
_account_out,
_account_out_light,
_can_view_huya_all,
_can_view_huya_assigned,
_can_view_huya_cookie,
_clear_huya_account_references,
_filter_huya_accounts_query,
_huya_cookie_state_map,
_huya_password_state_map,
_require_huya_perm,
_require_huya_tag_permission,
_selected_huya_account_ids,
_visible_huya_accounts_query,
browser_login_script,
cookie_pairs,
defer,
ensure_huya_config,
exchange_target_options,
exchange_target_url,
func,
get_current_user,
get_db,
huya_config_value,
import_huya_cookies,
import_huya_password_accounts,
normalize_huya_cookie,
or_,
record_audit,
)
router = APIRouter()
+24 -1
View File
@@ -2,7 +2,30 @@
from fastapi import APIRouter
from ._common import *
from ._common import (
Depends,
HTTPException,
HuyaAccount,
Query,
Session,
StreamingResponse,
User,
_can_view_huya_cookie,
_cookie_out,
_cookie_out_light,
_has_huya_perm,
_require_huya_perm,
_visible_huya_accounts_query,
csv,
defer,
get_current_user,
get_db,
io,
normalize_huya_cookie,
or_,
rebuild_web_cookie_from_raw,
record_audit,
)
router = APIRouter()
+19 -1
View File
@@ -2,7 +2,25 @@
from fastapi import APIRouter
from ._common import *
from ._common import (
_DEVICE_BINDINGS_PERMS_VIEW,
Depends,
HTTPException,
HuyaAccount,
HuyaDeviceBindingsBatchDeleteRequest,
Session,
User,
_fp_state_dir,
_load_profile_db,
_mask_hex,
_require_huya_perm,
_save_profile_db,
_unbind_device_accounts,
get_current_user,
get_db,
shutil,
user_has_permission,
)
router = APIRouter()
+42 -3
View File
@@ -4,7 +4,41 @@ import time
from fastapi import APIRouter
from ._common import *
from ._common import (
UTC,
AccountBulkSelection,
Depends,
HTTPException,
HuyaAccount,
HuyaAppLoginError,
HuyaAppPasswordLoginRequest,
HuyaAppQrAuthRequiredError,
HuyaCredentialError,
HuyaLoginError,
HuyaPasswordLoginRequest,
HuyaPasswordLoginSelectedRequest,
HuyaSmsCodeRequest,
HuyaSmsLoginRequest,
HuyaTestSmsUrlRequest,
Session,
User,
_account_out,
_can_view_huya_cookie,
_require_huya_perm,
_selected_huya_account_ids,
_visible_huya_accounts_query,
cast,
datetime,
get_current_user,
get_db,
logger,
login_huya_app_password,
login_huya_password,
login_huya_sms,
save_huya_login_cookie_to_account,
send_huya_sms_code,
upsert_huya_cookie,
)
router = APIRouter()
@@ -287,7 +321,12 @@ def auto_sms_login_account(
sms_url = (account.sms_url or "").strip()
if not phone or not sms_url:
raise HTTPException(status_code=400, detail="该账号缺少手机号或接码链接")
from core.huya.auto_register import auto_register_huya_phone
# ⚠️ 这里原来是 `auto_register_huya_phone` —— 该名字在 core.huya.auto_register
# 里**从来没存在过**(2026-09-06 起就是死的),每次调用都在 import 阶段抛
# ImportError → 500。真实函数名是 register_huya_with_sms_line,返回的
# HuyaAutoRegisterResult 同样有 .success / .cookie / .message。
# 回归用例:tests/test_huya_auto_sms_login.py
from core.huya.auto_register import register_huya_with_sms_line
from core.sms_provider import SmsLine, detect_provider
item = SmsLine(
@@ -296,7 +335,7 @@ def auto_sms_login_account(
provider=detect_provider(sms_url),
raw=f"{phone}----{sms_url}",
)
res = auto_register_huya_phone(item, wait_seconds=90, poll_interval=2)
res = register_huya_with_sms_line(item, wait_seconds=90, poll_interval=2)
if not res.success or not res.cookie:
account.status = "login_failed"
account.updated_at = datetime.now(UTC)
+21 -1
View File
@@ -2,7 +2,27 @@
from fastapi import APIRouter
from ._common import *
from ._common import (
Depends,
HTTPException,
HuyaAutoRegisterBatchOut,
HuyaAutoRegisterRequest,
HuyaAutoRegisterRetryRequest,
HuyaRegisterSuccessLogOut,
ProxyConfig,
Query,
Session,
StreamingResponse,
User,
_require_huya_perm,
export_success_logs_text,
get_current_user,
get_db,
huya_register_registry,
list_success_logs,
parse_sms_lines,
threading,
)
router = APIRouter()
+46 -1
View File
@@ -2,7 +2,52 @@
from fastapi import APIRouter
from ._common import *
from ._common import (
HUYA_CONFIG_FIELDS,
SUPPORTED_TASK_TYPES,
UTC,
Depends,
HTTPException,
HuyaBatchRunner,
HuyaConfigOut,
HuyaConfigUpdate,
HuyaGoodsOut,
HuyaGoodsSnapshot,
HuyaRechargeGoodsOut,
HuyaRechargeGoodsSnapshot,
HuyaTask,
HuyaTaskBatchRequest,
HuyaTaskOut,
HuyaWorkbench,
HuyaWorkbenchAccount,
HuyaWorkbenchAccountsUpdate,
Query,
Session,
SessionLocal,
User,
WebSocket,
WebSocketDisconnect,
_config_out,
_has_huya_perm,
_huya_task_summary,
_require_huya_batch_owner,
_require_huya_task_account_access,
_task_out,
_visible_huya_tasks_query,
_workbench_account_ids,
apply_huya_config_defaults,
asyncio,
authenticate_websocket,
cleanup_orphan_huya_tasks,
create_planned_tasks,
datetime,
ensure_huya_config,
get_db,
huya_batch_registry,
record_audit,
require_permission,
threading,
)
router = APIRouter()