From b3aef57f213d3d8b36f71cc2c813b68bf26e9ce0 Mon Sep 17 00:00:00 2001 From: underthestars-zhy Date: Mon, 8 Jun 2026 16:23:05 -0700 Subject: [PATCH] refactor(photon): use TYPE_CHECKING for httpx import and fix client ref --- plugins/platforms/photon/adapter.py | 24 ++++++++++++++++-------- plugins/platforms/photon/plugin.yaml | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/plugins/platforms/photon/adapter.py b/plugins/platforms/photon/adapter.py index 455d8750d77..3a057b17c39 100644 --- a/plugins/platforms/photon/adapter.py +++ b/plugins/platforms/photon/adapter.py @@ -36,14 +36,21 @@ import sys import time from datetime import datetime, timezone from pathlib import Path -from typing import Any, Dict, Optional +from typing import TYPE_CHECKING, Any, Dict, Optional -try: +if TYPE_CHECKING: + # Type checkers see ``httpx`` as the always-imported module, so every use + # site type-checks cleanly. The runtime fallback below keeps the optional + # dependency truly optional (each use site is guarded by HTTPX_AVAILABLE). import httpx HTTPX_AVAILABLE = True -except ImportError: # pragma: no cover - httpx is already a Hermes dep - HTTPX_AVAILABLE = False - httpx = None # type: ignore[assignment] +else: + try: + import httpx + HTTPX_AVAILABLE = True + except ImportError: # pragma: no cover - httpx is already a Hermes dep + HTTPX_AVAILABLE = False + httpx = None from gateway.config import Platform, PlatformConfig from gateway.platforms.base import ( @@ -281,7 +288,8 @@ class PhotonAdapter(BasePlatformAdapter): ) return False - self._http_client = httpx.AsyncClient(timeout=30.0) + client = httpx.AsyncClient(timeout=30.0) + self._http_client = client # The sidecar holds the gRPC stream for BOTH directions, so it is # required now (not just for outbound). @@ -294,7 +302,7 @@ class PhotonAdapter(BasePlatformAdapter): f"failed to start Photon sidecar: {e}", retryable=True, ) - await self._http_client.aclose() + await client.aclose() self._http_client = None return False else: @@ -903,7 +911,7 @@ def register(ctx) -> None: ctx.register_platform( name="photon", - label="Photon iMessage", + label="iMessage via Photon", adapter_factory=lambda cfg: PhotonAdapter(cfg), check_fn=check_requirements, validate_config=validate_config, diff --git a/plugins/platforms/photon/plugin.yaml b/plugins/platforms/photon/plugin.yaml index 21eae3cdbf2..388bfc72dc5 100644 --- a/plugins/platforms/photon/plugin.yaml +++ b/plugins/platforms/photon/plugin.yaml @@ -1,5 +1,5 @@ name: photon-platform -label: Photon iMessage +label: iMessage via Photon kind: platform version: 0.2.0 description: >