From 75ee76b60e7b14881cc307d8edca4598ae229768 Mon Sep 17 00:00:00 2001 From: bohd4nx Date: Fri, 20 Mar 2026 20:22:33 +0200 Subject: [PATCH] fix: cast cookies to dict for type safety and improve error handling in FragmentClient --- pyfragment/client.py | 5 +++-- pyfragment/types/constants.py | 4 ++-- pyfragment/utils/wallet.py | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyfragment/client.py b/pyfragment/client.py index 9436aed..066f444 100644 --- a/pyfragment/client.py +++ b/pyfragment/client.py @@ -1,4 +1,5 @@ import json +from typing import cast from pyfragment.methods.premium import purchase_premium from pyfragment.methods.stars import purchase_stars @@ -71,7 +72,7 @@ class FragmentClient: except Exception as exc: raise CookieError(CookieError.READ_FAILED.format(exc=exc)) from exc - missing_keys = [k for k in REQUIRED_COOKIE_KEYS if not str(cookies.get(k, "")).strip()] + missing_keys = [k for k in REQUIRED_COOKIE_KEYS if not str(cast(dict, cookies).get(k, "")).strip()] if missing_keys: raise CookieError(CookieError.MISSING_KEYS.format(keys=", ".join(missing_keys))) @@ -85,7 +86,7 @@ class FragmentClient: self.seed: str = seed.strip() self.api_key: str = api_key.strip() - self.cookies: dict = cookies + self.cookies: dict = cast(dict, cookies) self.wallet_version: WalletVersion = version # type: ignore[assignment] self.timeout: float = timeout diff --git a/pyfragment/types/constants.py b/pyfragment/types/constants.py index 74a8660..362a850 100644 --- a/pyfragment/types/constants.py +++ b/pyfragment/types/constants.py @@ -1,5 +1,5 @@ import json -from typing import Literal, get_args +from typing import Any, Literal, get_args from tonutils.contracts.wallet import WalletV4R2, WalletV5R1 @@ -8,7 +8,7 @@ WalletVersion = Literal["V4R2", "V5R1"] SUPPORTED_WALLET_VERSIONS: frozenset[str] = frozenset(get_args(WalletVersion)) # Wallet class map — used to resolve the correct contract from WALLET_VERSION -WALLET_CLASSES: dict[str, type] = {"V4R2": WalletV4R2, "V5R1": WalletV5R1} +WALLET_CLASSES: dict[str, Any] = {"V4R2": WalletV4R2, "V5R1": WalletV5R1} # Minimum wallet balance required to cover TON network gas fees. MIN_TON_BALANCE: float = 0.056 diff --git a/pyfragment/utils/wallet.py b/pyfragment/utils/wallet.py index 42d2167..0a453ea 100644 --- a/pyfragment/utils/wallet.py +++ b/pyfragment/utils/wallet.py @@ -78,6 +78,8 @@ async def process_transaction(client: "FragmentClient", transaction_data: dict) except Exception as exc: raise TransactionError(TransactionError.BROADCAST_FAILED.format(exc=exc)) from exc + raise TransactionError(TransactionError.BROADCAST_FAILED.format(exc="transfer loop exited without result")) + async def get_account_info(client: "FragmentClient") -> dict[str, Any]: """Fetch wallet address, public key, and state-init for the Fragment API.