Guard _fetch_sec_filings and _fetch_calendar against null/empty Yahoo results - #2883
Guard _fetch_sec_filings and _fetch_calendar against null/empty Yahoo results#28838910work-cell wants to merge 1 commit into
Conversation
… result Both methods accessed result["quoteSummary"]["result"][0] with no guard against null or empty list responses from Yahoo Finance. The same null- result pattern was recently fixed in _fetch_info (ranaroussi#2869) and _fetch_complementary (ranaroussi#2863, ranaroussi#2877). - _fetch_sec_filings: wrap bare dict access in try/except(KeyError, IndexError, TypeError); return None on any malformed payload. - _fetch_calendar: add TypeError to the existing except clause so a null result list (not just a missing key or empty list) is handled by the YFDataException path instead of propagating uncaught. Add three network-free regression tests covering the null/empty cases and a happy-path round-trip for sec_filings.
|
When does Yahoo returns a non-None payload? |
|
Good question. Yahoo returns that shape for symbols/modules it has no data for (delisted or invalid tickers, region-restricted symbols, or a module that simply returns nothing): {"quoteSummary": {"result": null, "error": null}}
Reproduced on current from unittest.mock import patch, MagicMock
from yfinance.scrapers.quote import Quote
null_payload = {"quoteSummary": {"result": None, "error": None}}
for name in ("_fetch_sec_filings", "_fetch_calendar"):
q = Quote(MagicMock(), "DELISTED")
with patch.object(Quote, "_fetch", return_value=null_payload):
getattr(q, name)()
# _fetch_sec_filings -> TypeError: 'NoneType' object is not subscriptable
# _fetch_calendar -> TypeError: 'NoneType' object is not subscriptableNote |
|
My point was - I prefer checking for specific values instead of exception. Maybe when that keyerror happens, there is another key with information. Also I expect to speak with a human not a bot. |
Problem
_fetch_sec_filingsrunsfilings = result["quoteSummary"]["result"][0]["secFilings"]["filings"]immediately after theif result is None: return Noneguard. When Yahoo returns a non-None payload whosequoteSummary.resultisnull/empty (or is missing thesecFilingskey), this crashes withTypeError: 'NoneType' object is not subscriptable(orKeyError/IndexError)._fetch_calendarhas the same class of gap: its parse block only catches(KeyError, IndexError), so anullnested structure raises an uncaughtTypeError.Fix
_fetch_sec_filings: wrap the nested lookup intry/except (KeyError, IndexError, TypeError)and returnNone— graceful degradation, consistent with the existingresult is Noneguard._fetch_calendar: addTypeErrorto the existingexcepttuple.Tests
Adds network-free regression tests (mocking
Quote._fetch) intests/test_ticker.py:test_sec_filings_null_result— anullresult degrades toNoneinstead of crashingtest_sec_filings_happy_path— a valid payload still parses correctlytest_calendar_null_result— anullresult raises a cleanYFDataExceptionVerified red→green:
test_sec_filings_null_resultfails on currentdevwithTypeError: 'NoneType' object is not subscriptableand passes with this change.