-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix 'raise_errors' argument ignored in Ticker.history() #1806
Conversation
To have any chance of this being merged you're gonna need to explain your use case, because maybe there is a better way. |
@ValueRaider thanks for the prompt reply 😄 ! I'll try to explain better. Let's consider this: import requests
import yfinance as yf
from pandas import DataFrame
def get_prices(symbol, timeout=(10, 10)):
ticker = yf.Ticker(symbol)
data: DataFrame = ticker.history(
period="5d",
interval="1d",
timeout=timeout,
)
return data
# Network timeout (simulated by using a really short read-timeout).
prices = get_prices("AAPL", timeout=(10, 0.01))
assert prices.empty
# Non-existent symbol.
prices = get_prices("XXXAAPLXXX")
assert prices.empty The current code swallows any exception that With my proposed code change this would be possible: def reraise(exc):
raise exc
def get_prices(symbol, timeout=(10, 10)):
ticker = yf.Ticker(symbol)
data: DataFrame = ticker.history(
period="5d",
interval="1d",
timeout=timeout,
request_exc_hook=reraise,
)
return data
# Network timeout (simulated by using a really short read-timeout).
try:
prices = get_prices("AAPL", timeout=(10, 0.01))
except requests.Timeout:
print("Timeout, retrying one more time...")
... My proposed code change is backward compatible and the new feature is disabled by default, so no change is required by current users, unless they want to use the new feature. I see 2 possible alternatives that do not require introducing the
|
Seems best fix is much simpler - add |
@ValueRaider done 😉! |
Squash commits then rebase to |
d81c25b
to
c94cbb6
Compare
|
I'd like to be able to handle any exception that might happen while
Ticker.history()
performs the HTTP request.I currently do it in a hackish way using a monkey patch.
With this PR I'd like to introduce a hook for a custom function that, when given, it is invoked when such exception happens.
Most basic example I can think of:
Note that my actual case a bit more complex to explain and boring (meaning not really useful for this proposal).