addisonlynch / iexfinance

Python SDK for IEX Cloud
https://addisonlynch.github.io/iexfinance
Apache License 2.0
648 stars 136 forks source link

Crash when calling market (aggregate) endpoint for `get_beta` #260

Open nima opened 3 years ago

nima commented 3 years ago

Summary (include Python version)

Crash when hitting the market (aggregate) endpoint, for some specific keys.

In [17]: import sys
In [18]: sys.version_info
Out[18]: sys.version_info(major=3, minor=9, micro=1, releaselevel='final', serial=0)

Date/time of issue

In [26]: datetime.datetime.now()
Out[26]: datetime.datetime(2021, 2, 15, 23, 14, 27, 323926)

Expected behavior

It should not crash, and should behave as it does on other keys, such as price:

In [27]: x = api.iex.Stock(['AAPL', 'MSFT'])

In [28]: x.get_price()
Out[28]: {'AAPL': 135.37, 'MSFT': 244.99}

Actual behavior

In [29]: x.get_beta()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-29-67c5fe7c40d7> in <module>
----> 1 x.get_beta()

~/rh/.venv/lib/python3.9/site-packages/iexfinance/stocks/base.py in get_beta(self)
    870 
    871     def get_beta(self):
--> 872         return self._get_field("key_stats", "beta")
    873 
    874     def get_short_interest(self):

~/rh/.venv/lib/python3.9/site-packages/iexfinance/stocks/base.py in _get_field(self, endpoint, field)
     91             raise NotImplementedError("Endpoint %s not implemented." % endpoint)
     92         if field not in data:
---> 93             raise KeyError("Field %s not found in %s." % (field, endpoint))
     94         if self.output_format == "json":
     95             if self.single_symbol:

KeyError: 'Field beta not found in key_stats.'

Additional Info

In [30]: x
Out[30]: Stock(symbols=AAPL,MSFT, output_format='json')

In [31]: api.iex
Out[31]: <module 'iexfinance.stocks' from '/Users/nimbler/rh/.venv/lib/python3.9/site-packages/iexfinance/stocks/__init__.py'>
% pip list|grep iex
iexfinance                                        0.5.0