fix(dataflows): serve a past run financial statements only with a known filing date

- Yahoo and Alpha Vantage statements, dated by the period they cover, are withheld from a run dated in the past, with a notice saying why
- statements come from SEC EDGAR by default (fundamental_data "sec_edgar,yfinance"), so a past run reads US filers as filed
This commit is contained in:
Yijia-Xiao
2026-09-29 05:57:18 +00:00
parent 1d92269d35
commit d7ac9461da
9 changed files with 119 additions and 99 deletions
-18
View File
@@ -77,30 +77,12 @@ _FUNDAMENTALS_JSON = json.dumps({
})
@pytest.mark.unit
def test_fundamentals_look_ahead_filter_runs_on_json_string(monkeypatch):
# #1115: the payload arrives as a JSON *string*; the old dict-only guard let
# future-dated fiscal periods leak into historical runs.
monkeypatch.setattr(avf, "_make_api_request", lambda fn, params: _FUNDAMENTALS_JSON)
out = avf.get_balance_sheet("AAPL", as_of_date="2024-01-01")
assert isinstance(out, str) # callers still receive a str
parsed = json.loads(out)
assert [r["fiscalDateEnding"] for r in parsed["annualReports"]] == ["2023-12-31"]
assert [r["fiscalDateEnding"] for r in parsed["quarterlyReports"]] == ["2023-09-30"]
@pytest.mark.unit
def test_fundamentals_no_curr_date_passes_through(monkeypatch):
monkeypatch.setattr(avf, "_make_api_request", lambda fn, params: _FUNDAMENTALS_JSON)
assert avf.get_income_statement("AAPL") == _FUNDAMENTALS_JSON
@pytest.mark.unit
def test_fundamentals_non_json_body_unchanged(monkeypatch):
monkeypatch.setattr(avf, "_make_api_request", lambda fn, params: "not-json")
assert avf.get_cashflow("AAPL", as_of_date="2024-01-01") == "not-json"
# ---------------------------------------------------------------------------
# Date trim (see the rationale on the unguarded trim in alpha_vantage.common)
# ---------------------------------------------------------------------------
+2 -2
View File
@@ -49,7 +49,7 @@ class DataflowsConfigIsolationTests(unittest.TestCase):
fresh = get_config()
self.assertEqual(fresh["data_vendors"]["core_stock_apis"], "alpha_vantage")
self.assertEqual(fresh["data_vendors"]["technical_indicators"], "yfinance")
self.assertEqual(fresh["data_vendors"]["fundamental_data"], "yfinance")
self.assertEqual(fresh["data_vendors"]["fundamental_data"], "sec_edgar,yfinance")
self.assertEqual(fresh["data_vendors"]["news_data"], "yfinance")
def test_nested_dict_updates_merge_one_level_deep(self):
@@ -95,7 +95,7 @@ def test_a_run_reads_its_own_graphs_vendors_not_the_last_graph_built():
set_config(first) # graph A is built
second = _graph(copy.deepcopy(default_config.DEFAULT_CONFIG))
assert _vendors_seen_by_a_run(second) == ["yfinance"]
assert _vendors_seen_by_a_run(second) == ["sec_edgar,yfinance"]
@pytest.mark.unit
+60
View File
@@ -0,0 +1,60 @@
"""A historical run is served statements only with a known filing time.
SEC EDGAR dates every filing, so it serves a past date as filed. Yahoo and Alpha
Vantage date a statement by the period it covers, which a company publishes weeks
later, so a past run is told they are withheld rather than served figures that may
not yet have been public.
"""
import copy
from unittest import mock
import pytest
from tradingagents import default_config
from tradingagents.dataflows import router
from tradingagents.dataflows.config import set_config
from tradingagents.dataflows.vendors.alpha_vantage import fundamentals as alpha_vantage
from tradingagents.dataflows.vendors.yahoo import fundamentals as yahoo
PAST = "2024-03-14"
@pytest.mark.unit
@pytest.mark.parametrize("statement", [yahoo.get_balance_sheet, yahoo.get_cashflow, yahoo.get_income_statement])
def test_yahoo_withholds_statements_from_a_past_run_without_asking(statement):
with mock.patch.object(yahoo, "yf_retry", side_effect=AssertionError("requested")):
out = statement("SAP.DE", "quarterly", PAST)
assert "withheld" in out and PAST in out and "filing date" in out
@pytest.mark.unit
@pytest.mark.parametrize("statement", [alpha_vantage.get_balance_sheet, alpha_vantage.get_cashflow,
alpha_vantage.get_income_statement])
def test_alpha_vantage_withholds_statements_from_a_past_run_without_asking(statement):
with mock.patch.object(alpha_vantage, "_make_api_request", side_effect=AssertionError("requested")):
out = statement("IBM", "quarterly", PAST)
assert "withheld" in out and PAST in out
@pytest.mark.unit
def test_a_current_run_is_still_served_yahoo_statements():
import pandas as pd
from tradingagents.dataflows.date_window import get_current_date
frame = pd.DataFrame({pd.Timestamp("2026-06-30"): [1.0]}, index=["Total Assets"])
with mock.patch.object(yahoo, "yf_retry", return_value=frame):
out = yahoo.get_balance_sheet("SAP.DE", "quarterly", get_current_date())
assert "Total Assets" in out and "withheld" not in out
@pytest.mark.unit
def test_statements_come_from_sec_edgar_first_by_default():
set_config(copy.deepcopy(default_config.DEFAULT_CONFIG))
served = []
chain = {name: (lambda *a, _n=name, **k: served.append(_n) or f"{_n} statements")
for name in router.VENDOR_METHODS["get_balance_sheet"]}
with mock.patch.dict(router.VENDOR_METHODS, {"get_balance_sheet": chain}):
router.route_to_vendor("get_balance_sheet", "AAPL", "quarterly", PAST)
assert served == ["sec_edgar"]
+8 -5
View File
@@ -160,9 +160,10 @@ def test_an_indicator_that_could_not_be_read_is_not_shown_as_a_blank_value():
# A past date withholds the live profile before any request, so the
# fundamentals case is exercised on the date it does fetch.
("get_fundamentals", ("AAPL", None)),
("get_balance_sheet", ("AAPL", "annual", "2026-09-01")),
("get_cashflow", ("AAPL", "annual", "2026-09-01")),
("get_income_statement", ("AAPL", "annual", "2026-09-01")),
# Statements are requested only for a run dated today; a past one withholds them.
("get_balance_sheet", ("AAPL", "annual", get_current_date())),
("get_cashflow", ("AAPL", "annual", get_current_date())),
("get_income_statement", ("AAPL", "annual", get_current_date())),
("get_insider_transactions", ("AAPL", "2026-09-01")),
])
def test_a_yfinance_failure_is_a_vendor_error_not_a_report(func, args):
@@ -206,11 +207,11 @@ def test_an_unreachable_vendor_is_not_reported_as_a_missing_symbol(monkeypatch):
monkeypatch.setattr(ohlcv, "vendor_reachable", lambda url: False)
with pytest.raises(VendorUnavailableError, match="unreachable"):
yahoo_fundamentals.get_balance_sheet("AAPL", "annual", "2026-09-01")
yahoo_fundamentals.get_balance_sheet("AAPL", "annual", get_current_date())
monkeypatch.setattr(ohlcv, "vendor_reachable", lambda url: True)
with pytest.raises(NoMarketDataError):
yahoo_fundamentals.get_balance_sheet("AAPL", "annual", "2026-09-01")
yahoo_fundamentals.get_balance_sheet("AAPL", "annual", get_current_date())
@pytest.mark.unit
@@ -218,11 +219,13 @@ def test_every_vendor_unavailable_says_so_rather_than_crashing(monkeypatch):
"""A throttled or unreachable chain used to raise RuntimeError('No available
vendor'), which ends the run, and never said the vendor was the problem."""
from tradingagents.dataflows import router
from tradingagents.dataflows.config import set_config
from tradingagents.dataflows.errors import VendorUnavailableError
def _down(*a, **k):
raise VendorUnavailableError("Yahoo Finance is unreachable")
set_config({"data_vendors": {"fundamental_data": "yfinance"}})
monkeypatch.setitem(router.VENDOR_METHODS["get_balance_sheet"], "yfinance", _down)
out = router.route_to_vendor("get_balance_sheet", "AAPL", "annual", "2026-09-01")