Python
refactor(dataflows): decide in one place whether a run is dated in the past
Yijia-Xiao660966d168b9
diff --git a/tests/test_undated_tools_as_of.py b/tests/test_undated_tools_as_of.pyindex 3252f928a..a75a7276c 100644--- a/tests/test_undated_tools_as_of.py+++ b/tests/test_undated_tools_as_of.py@@ -14,6 +14,7 @@ import pandas as pdimport pytestfrom tradingagents.agents import tools+from tradingagents.dataflows.date_window import get_current_datefrom tradingagents.dataflows.vendors import polymarketfrom tradingagents.dataflows.vendors.alpha_vantage import news as alpha_vantage_newsfrom tradingagents.dataflows.vendors.yahoo import (@@ -77,7 +78,7 @@ def test_polymarket_withholds_live_odds_from_a_historical_run():@pytest.mark.unitdef test_polymarket_serves_a_current_run():with mock.patch.object(polymarket, "_request", return_value={"events": []}) as req:- polymarket.get_prediction_markets("Fed rate cut", as_of_date=polymarket.get_current_date())+ polymarket.get_prediction_markets("Fed rate cut", as_of_date=get_current_date())req.assert_called_once()diff --git a/tradingagents/agents/context.py b/tradingagents/agents/context.pyindex c3eecafca..88a01b9d2 100644--- a/tradingagents/agents/context.py+++ b/tradingagents/agents/context.py@@ -5,7 +5,7 @@ import loggingfrom collections.abc import Mappingfrom typing import Any-from tradingagents.dataflows.date_window import get_current_date+from tradingagents.dataflows.date_window import is_historicalfrom tradingagents.dataflows.vendors.yahoo.fundamentals import get_company_profilelogger = logging.getLogger(__name__)@@ -135,7 +135,7 @@ def build_instrument_context(name = identity.get("company_name") or identity.get("name")label = "Name" if is_crypto else "Company"details = []- if trade_date and str(trade_date) < get_current_date():+ if is_historical(trade_date):if name:details.append(f"{label}: {name} (its current name, given only to identify it; "diff --git a/tradingagents/dataflows/date_window.py b/tradingagents/dataflows/date_window.pyindex b74202d14..e799914a0 100644--- a/tradingagents/dataflows/date_window.py+++ b/tradingagents/dataflows/date_window.py@@ -36,6 +36,11 @@ def get_current_date() -> str:return date.today().strftime("%Y-%m-%d")+def is_historical(run_date) -> bool:+ """Whether a run is dated before today, so live-only data would come from after it."""+ return bool(run_date) and str(run_date) < get_current_date()++def coverage_gap(dates, start_date: str, end_date: str, source: str, subject: str) -> str | None:@@ -106,10 +111,7 @@ def withhold_live_profile(as_of_date: str | None, label: str) -> str | None:Every fundamentals vendor withholds on this rule, so switching between themcannot reintroduce the leak."""- if not as_of_date:- return None- today = get_current_date()- if as_of_date >= today:+ if not is_historical(as_of_date):return Nonereturn (f"# Company Fundamentals for {label}\n"diff --git a/tradingagents/dataflows/vendors/polymarket.py b/tradingagents/dataflows/vendors/polymarket.pyindex 0e5303dd1..2dae8f367 100644--- a/tradingagents/dataflows/vendors/polymarket.py+++ b/tradingagents/dataflows/vendors/polymarket.py@@ -15,7 +15,7 @@ from datetime import datetime, timezoneimport requests-from tradingagents.dataflows.date_window import get_current_date+from tradingagents.dataflows.date_window import is_historicallogger = logging.getLogger(__name__)@@ -83,7 +83,7 @@ def get_prediction_markets(topic: str, limit: int | None = None, as_of_date: streach with its implied probability, traded volume, resolution date, andrecent (1-week) move."""- if as_of_date and as_of_date < get_current_date():+ if is_historical(as_of_date):return (f"Prediction-market odds are withheld for {as_of_date}. Polymarket serves "f"only live odds on open markets, with no historical vintage, so serving "diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.pyindex 820398c18..d2e29bdb6 100644--- a/tradingagents/graph/trading_graph.py+++ b/tradingagents/graph/trading_graph.py@@ -11,7 +11,7 @@ import tradingagentsfrom tradingagents.agents.context import build_instrument_context, resolve_instrument_identityfrom tradingagents.agents.rating import run_ratingfrom tradingagents.dataflows.config import run_config, run_config_context, set_config-from tradingagents.dataflows.date_window import get_current_date+from tradingagents.dataflows.date_window import get_current_date, is_historicalfrom tradingagents.dataflows.symbols import safe_ticker_componentfrom tradingagents.default_config import DEFAULT_CONFIGfrom tradingagents.llm_clients import build_llm_kwargs, create_llm_client@@ -152,8 +152,7 @@ class TradingAgentsGraph:None, disabling the filter so live behavior and pre-migration entries(which have no stored resolution date) are unaffected."""- td = str(trade_date)- return td if td < datetime.now().strftime("%Y-%m-%d") else None+ return str(trade_date) if is_historical(trade_date) else Nonedef _run_signature(self, asset_type: str, portfolio=None) -> str:"""Run inputs that must invalidate a checkpoint if changed.