Repository: ranaroussi/yfinance

Issue:
Code seems faulty
I have not seen a bug in execution, but reading this code in ranaroussi/yfinance/feature/prune-info/yfinance/scrapers/quote.py, it appears wrong to me.

I do not understand where this `k` gets a value in `_scrape_complementary()`
line 293:  `if k not in key_stats:`

ranaroussi/yfinance/feature/prune-info/yfinance/scrapers/quote.py: 290 .. 299
```            json_str = self._data.cache_get(url=url, proxy=proxy).text
            json_data = json.loads(json_str)
            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
            else:
                # Select most recent (last) raw value in list:
                v = key_stats[k][-1]["reportedValue"]["raw"]
            self._info[k] = v
```




Discussion:
Should be in a `for k in keys:` . Do you mind submitting a fix?
I would not mind submitting a fix, but I am not now set up to do development on yfinance. I know it is basically a git clone, then edit the file, push, and test. However, my code files and virtual environments are such a mess that it will take a while before that can be sanitary.
I just noticed this whilst browsing the code. I do not even have a test case.
I will eventually get straightened out, but thought it best to expose the issue sooner.

Resolving patch:
diff --git a/yfinance/scrapers/quote.py b/yfinance/scrapers/quote.py
--- 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("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
+


Test patch:
diff --git a/tests/ticker.py b/tests/ticker.py
--- 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: