Skip to content

Commit a679060

Browse files
authored
Merge pull request #1774 from coskos-ops/fix/complementaryinfo
Fixed incorrect code for ticker complementary info retrieval
2 parents a914647 + 122269c commit a679060

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

tests/ticker.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,18 @@ def test_info(self):
762762
self.assertIn("symbol", data.keys(), f"Did not find expected key '{k}' in info dict")
763763
self.assertEqual(self.symbols[0], data["symbol"], "Wrong symbol value in info dict")
764764

765+
def test_complementary_info(self):
766+
# This test is to check that we can successfully retrieve the trailing PEG ratio
767+
768+
# We don't expect this one to have a trailing PEG ratio
769+
data1 = self.tickers[0].info
770+
self.assertEqual(data1['trailingPegRatio'], None)
771+
772+
# This one should have a trailing PEG ratio
773+
data2 = self.tickers[2].info
774+
self.assertEqual(data2['trailingPegRatio'], 1.2713)
775+
pass
776+
765777
# def test_fast_info_matches_info(self):
766778
# fast_info_keys = set()
767779
# for ticker in self.tickers:

yfinance/scrapers/quote.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -718,14 +718,12 @@ def _fetch_complementary(self, proxy):
718718

719719
json_str = self._data.cache_get(url=url, proxy=proxy).text
720720
json_data = json.loads(json_str)
721-
try:
722-
key_stats = json_data["timeseries"]["result"][0]
723-
if k not in key_stats:
724-
# Yahoo website prints N/A, indicates Yahoo lacks necessary data to calculate
725-
v = None
721+
if json_data["timeseries"]["error"] is not None:
722+
raise YFinanceException("Failed to parse json response from Yahoo Finance: " + json_data["error"])
723+
for k in keys:
724+
keydict = json_data["timeseries"]["result"][0]
725+
if k in keydict:
726+
self._info[k] = keydict[k][-1]["reportedValue"]["raw"]
726727
else:
727-
# Select most recent (last) raw value in list:
728-
v = key_stats[k][-1]["reportedValue"]["raw"]
729-
except Exception:
730-
v = None
731-
self._info[k] = v
728+
self.info[k] = None
729+

0 commit comments

Comments
 (0)