Skip to content

Commit 8fdf532

Browse files
committed
Fixed issue #1305. Added test case to test for trailingPegInfo statistic retrieval
1 parent 5805029 commit 8fdf532

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
@@ -720,6 +720,18 @@ def test_info(self):
720720
self.assertIn("symbol", data.keys(), f"Did not find expected key '{k}' in info dict")
721721
self.assertEqual(self.symbols[0], data["symbol"], "Wrong symbol value in info dict")
722722

723+
def test_complementary_info(self):
724+
# This test is to check that we can successfully retrieve the trailing PEG ratio
725+
726+
# We don't expect this one to have a trailing PEG ratio
727+
data1 = self.tickers[0].info
728+
self.assertEqual(data1['trailingPegRatio'], None)
729+
730+
# This one should have a trailing PEG ratio
731+
data2 = self.tickers[2].info
732+
self.assertEqual(data2['trailingPegRatio'], 1.2713)
733+
pass
734+
723735
# def test_fast_info_matches_info(self):
724736
# fast_info_keys = set()
725737
# for ticker in self.tickers:

yfinance/scrapers/quote.py

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

701701
json_str = self._data.cache_get(url=url, proxy=proxy).text
702702
json_data = json.loads(json_str)
703-
try:
704-
key_stats = json_data["timeseries"]["result"][0]
705-
if k not in key_stats:
706-
# Yahoo website prints N/A, indicates Yahoo lacks necessary data to calculate
707-
v = None
703+
if json_data["timeseries"]["error"] is not None:
704+
raise YFinanceException(f"Failed to parse json response from Yahoo Finance: " + json_data["error"])
705+
for k in keys:
706+
keydict = json_data["timeseries"]["result"][0]
707+
if k in keydict:
708+
self._info[k] = keydict[k][-1]["reportedValue"]["raw"]
708709
else:
709-
# Select most recent (last) raw value in list:
710-
v = key_stats[k][-1]["reportedValue"]["raw"]
711-
except Exception:
712-
v = None
713-
self._info[k] = v
710+
self.info[k] = None
711+

0 commit comments

Comments
 (0)