Repository: ranaroussi/yfinance

Issue:
ImportError using yf.Ticker
Hello.  Hope everyone is doing well.  Getting an error after upgraded yfinance today (11/28/23) with the following code ```%pip install yfinance --upgrade --no-cache-dir --pre```.  

I am using python version 3.10 and yfinance version 02.32

Error I am getting is as follows:

ImportError: cannot import name 'DO_NOT_CACHE' from 'requests_cache' (C:\Users\17147\anaconda3\envs\ib230729\Lib\site-packages\requests_cache\__init__.py)


Simple code that reproduces the problem:
session = requests_cache.CachedSession('yfinance.cache')
session.headers['User-agent'] = 'my-program/1.0'
msft = yf.Ticker('msft', session=session)
msft.actions


Traceback below:
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[2], line 3
      1 session = requests_cache.CachedSession('yfinance.cache')
      2 session.headers['User-agent'] = 'my-program/1.0'
----> 3 msft = yf.Ticker('msft', session=session)

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\ticker.py:34, in Ticker.__init__(self, ticker, session)
     33 def __init__(self, ticker, session=None):
---> 34     super(Ticker, self).__init__(ticker, session=session)
     35     self._expirations = {}
     36     self._underlying  = {}

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\base.py:71, in TickerBase.__init__(self, ticker, session)
     68 if utils.is_isin(self.ticker):
     69     self.ticker = utils.get_ticker_by_isin(self.ticker, None, session)
---> 71 self._data: YfData = YfData(session=session)
     73 self._analysis = Analysis(self._data, ticker)
     74 self._holders = Holders(self._data, ticker)

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\data.py:47, in SingletonMeta.__call__(cls, *args, **kwargs)
     45 with cls._lock:
     46     if cls not in cls._instances:
---> 47         instance = super().__call__(*args, **kwargs)
     48         cls._instances[cls] = instance
     49     else:

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\data.py:75, in YfData.__init__(self, session)
     70 else:
     71     # Is caching. This is annoying. 
     72     # Can't simply use a non-caching session to fetch cookie & crumb, 
     73     # because then the caching-session won't have cookie.
     74     self._session_is_caching = True
---> 75     from requests_cache import DO_NOT_CACHE
     76     self._expire_after = DO_NOT_CACHE
     77 self._crumb = None

ImportError: cannot import name 'DO_NOT_CACHE' from 'requests_cache' (C:\Users\17147\anaconda3\envs\ib230729\Lib\site-packages\requests_cache\__init__.py)




Discussion:
What version `requests_cache`?
> What version `requests_cache`?

0.4.12
`DO_NOT_CACHE` must be a new feature so fix = update. Because `requests_cache` is optional I'm not sure how to enforce minimum version.
> `DO_NOT_CACHE` must be a new feature so fix = update. Because `requests_cache` is optional I'm not sure how to enforce minimum version.

Should i download a prior version that worked?  If so, which one do u recommend?  Thanks

`pip install requests-cache==1.1.1 yfinance==0.2.32 --upgrade --no-cache-dir`
in your case:
`conda update requests-cache yfinance` 
or just remove reuqests-cache if you don't use it
@ValueRaider
according [documentation](https://requests-cache.readthedocs.io/en/stable/user_guide/general.html#sessions)
 at the file _data.py_ on the line 200:
```
if self._session_is_caching:
    # get_args['expire_after'] = self._expire_after
    with self._session.cache_disabled():
        crumb_response = self._session.get(**get_args)
```



so you don't need import at lines 75-76
`cache_disabled()` is not thread-safe. https://requests-cache.readthedocs.io/en/stable/modules/requests_cache.session.html#requests_cache.session.CachedSession.cache_disabled
So maybe use 
`DO_NOT_CACHE = 0x0D0E0200020704  # Per RFC 4824`
instead import it
Fix will be setting minimum version of `requests_cache`, possible by #1771

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -65,7 +65,7 @@
                       'frozendict>=2.3.4', 'peewee>=3.16.2',
                       'beautifulsoup4>=4.11.1', 'html5lib>=1.1'],
     extras_require={
-        'nospam': ['requests_cache>=1.1.1', 'requests_ratelimiter>=0.4.2'],
+        'nospam': ['requests_cache>=1.0', 'requests_ratelimiter>=0.3.1'],
         'repair': ['scipy>=1.6.3'],
     },
     # Note: Pandas.read_html() needs html5lib & beautifulsoup4


Test patch: