refactor(V2): IBKR settings — TypedDict return + docstrings

Code review polish:
- credentials() returns IBKRCredentials TypedDict (was bare dict)
- Method docstring matching Deribit pattern
- Inline comment explaining account_id env-only design

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
root
2026-05-03 20:04:08 +00:00
parent 3a85ff05e6
commit 92cc45c896
+19 -1
View File
@@ -1,10 +1,22 @@
"""Pydantic Settings: legge .env e variabili d'ambiente."""
from __future__ import annotations
from typing import TypedDict
from pydantic import Field, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict
class IBKRCredentials(TypedDict):
consumer_key: str
access_token: str
access_token_secret: str
signature_key_path: str
encryption_key_path: str
account_id: str
dh_prime: str
class _Sub(BaseSettings):
"""Base per sub-settings, condivide model_config con env_file."""
model_config = SettingsConfigDict(
@@ -111,6 +123,7 @@ class IBKRSettings(_Sub):
access_token_secret_testnet: SecretStr | None = None
signature_key_path_testnet: str | None = None
encryption_key_path_testnet: str | None = None
# account_id has no base variant: paper and live accounts are always distinct
account_id_testnet: str | None = None
consumer_key_live: str | None = None
@@ -128,7 +141,12 @@ class IBKRSettings(_Sub):
ws_max_subscriptions: int = 80
ws_idle_timeout_s: int = 300
def credentials(self, env: str) -> dict:
def credentials(self, env: str) -> IBKRCredentials:
"""Return credential dict for given env.
Prefers env-specific (_TESTNET / _LIVE) values; falls back to base
(IBKR_CONSUMER_KEY etc.) for legacy single-pair setups.
ValueError if any required field missing.
"""
if env == "testnet":
ck = self.consumer_key_testnet or self.consumer_key
at = self.access_token_testnet or self.access_token