Skip to content

Commit 2eb70a2

Browse files
authored
Merge pull request ranaroussi#2794 from Jaypatel1511/fix/chart-none-guard
Fix TypeError when data['chart'] is None in history.py (ranaroussi#2670)
2 parents 9d989d8 + 212a5ea commit 2eb70a2

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎tests/test_prices.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,14 @@ def test_transient_error_detection(self):
472472
self.assertFalse(_is_transient_error(YFPricesMissingError('INVALID', '')))
473473
self.assertFalse(_is_transient_error(KeyError("key")))
474474

475+
def test_invalid_ticker_raises_prices_missing_not_type_error(self):
476+
# Regression for #2670: when Yahoo returns {"chart": null},
477+
# history() must raise YFPricesMissingError not TypeError.
478+
from yfinance.exceptions import YFPricesMissingError
479+
dat = yf.Ticker("TICKER_DOES_NOT_EXIST_XYZ", session=self.session)
480+
with self.assertRaises(YFPricesMissingError):
481+
dat.history(period="1mo", raise_errors=True)
482+
475483

476484
if __name__ == '__main__':
477485
unittest.main()

‎yfinance/scrapers/history.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ def history(self, period=period_default, interval="1d",
271271
_price_data_debug += f"(Yahoo status_code = {data['status_code']})"
272272
_exception = YFPricesMissingError(self.ticker, _price_data_debug)
273273
fail = True
274-
elif "chart" in data and data["chart"]["error"]:
274+
elif "chart" in data and data["chart"] and data["chart"]["error"]:
275275
_price_data_debug += ' (Yahoo error = "' + data["chart"]["error"]["description"] + '")'
276276
_exception = YFPricesMissingError(self.ticker, _price_data_debug)
277277
fail = True
278-
elif "chart" not in data or data["chart"]["result"] is None or not data["chart"]["result"] or not data["chart"]["result"][0]["indicators"]["quote"][0]:
278+
elif "chart" not in data or not data["chart"] or data["chart"]["result"] is None or not data["chart"]["result"] or not data["chart"]["result"][0]["indicators"]["quote"][0]:
279279
_exception = YFPricesMissingError(self.ticker, _price_data_debug)
280280
fail = True
281281
elif period and period not in self._history_metadata['validRanges'] and not utils.is_valid_period_format(period):

0 commit comments

Comments
 (0)