From 8fdf53233f6283b60ab9055341799fc7f49cf2b9 Mon Sep 17 00:00:00 2001 From: Filip Kostic Date: Sun, 10 Dec 2023 17:54:08 -0500 Subject: [PATCH 1/2] Fixed issue #1305. Added test case to test for trailingPegInfo statistic retrieval --- tests/ticker.py | 12 ++++++++++++ yfinance/scrapers/quote.py | 18 ++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/ticker.py b/tests/ticker.py index ce8741dbb..24215e3d7 100644 --- a/tests/ticker.py +++ b/tests/ticker.py @@ -720,6 +720,18 @@ def test_info(self): self.assertIn("symbol", data.keys(), f"Did not find expected key '{k}' in info dict") self.assertEqual(self.symbols[0], data["symbol"], "Wrong symbol value in info dict") + def test_complementary_info(self): + # This test is to check that we can successfully retrieve the trailing PEG ratio + + # We don't expect this one to have a trailing PEG ratio + data1 = self.tickers[0].info + self.assertEqual(data1['trailingPegRatio'], None) + + # This one should have a trailing PEG ratio + data2 = self.tickers[2].info + self.assertEqual(data2['trailingPegRatio'], 1.2713) + pass + # def test_fast_info_matches_info(self): # fast_info_keys = set() # for ticker in self.tickers: diff --git a/yfinance/scrapers/quote.py b/yfinance/scrapers/quote.py index c09cc8d36..8d234f923 100644 --- a/yfinance/scrapers/quote.py +++ b/yfinance/scrapers/quote.py @@ -700,14 +700,12 @@ def _fetch_complementary(self, proxy): json_str = self._data.cache_get(url=url, proxy=proxy).text json_data = json.loads(json_str) - try: - key_stats = json_data["timeseries"]["result"][0] - if k not in key_stats: - # Yahoo website prints N/A, indicates Yahoo lacks necessary data to calculate - v = None + if json_data["timeseries"]["error"] is not None: + raise YFinanceException(f"Failed to parse json response from Yahoo Finance: " + json_data["error"]) + for k in keys: + keydict = json_data["timeseries"]["result"][0] + if k in keydict: + self._info[k] = keydict[k][-1]["reportedValue"]["raw"] else: - # Select most recent (last) raw value in list: - v = key_stats[k][-1]["reportedValue"]["raw"] - except Exception: - v = None - self._info[k] = v + self.info[k] = None + From 122269cf53c08a7ae20e257a8233e1549ae2f795 Mon Sep 17 00:00:00 2001 From: Filip Kostic Date: Wed, 13 Dec 2023 19:45:47 -0500 Subject: [PATCH 2/2] Fixed fstring error --- yfinance/scrapers/quote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yfinance/scrapers/quote.py b/yfinance/scrapers/quote.py index 8d234f923..9616f2666 100644 --- a/yfinance/scrapers/quote.py +++ b/yfinance/scrapers/quote.py @@ -701,7 +701,7 @@ def _fetch_complementary(self, proxy): json_str = self._data.cache_get(url=url, proxy=proxy).text json_data = json.loads(json_str) if json_data["timeseries"]["error"] is not None: - raise YFinanceException(f"Failed to parse json response from Yahoo Finance: " + json_data["error"]) + raise YFinanceException("Failed to parse json response from Yahoo Finance: " + json_data["error"]) for k in keys: keydict = json_data["timeseries"]["result"][0] if k in keydict: