From 2462be65d0d52869949b15bac7e81aadf3375758 Mon Sep 17 00:00:00 2001 From: ymyke Date: Mon, 16 Oct 2023 15:45:17 +0200 Subject: [PATCH] Expose/raise all yfinance exceptions Otherwise, we potentially run into problems later on. See #19 for an example. Unfortunately, yfinance currently doesn't give very useful information in its exceptions and therefore doesn't help to understand the underlying issue (i.e., symbol not found vs no network connectivity at all vs ...). Therefore we no longer raise a `SymbolNotFoundException` any longer here but simply pass through what yfinance raises. Resolves #19 --- tessa/price/yahoo.py | 8 ++------ tests/price/test_yahoo_api.py | 4 +--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/tessa/price/yahoo.py b/tessa/price/yahoo.py index 8707c7e..f4f9353 100644 --- a/tessa/price/yahoo.py +++ b/tessa/price/yahoo.py @@ -2,7 +2,7 @@ import pandas as pd import yfinance as yf -from .types import PriceHistory, SymbolNotFoundError +from .types import PriceHistory START_FROM = "2000-01-01" """Adjust this date if you need to get historical data further in the past. Note that @@ -18,11 +18,7 @@ def get_price_history( that ticker. """ ticker = yf.Ticker(query) - try: - df = ticker.history(start=START_FROM, raise_errors=True) - except Exception as exc: # pylint: disable=broad-except - if "No timezone found" in str(exc): - raise SymbolNotFoundError(source="yahoo", query=query) from exc + df = ticker.history(start=START_FROM, raise_errors=True) # Simplify dataframe: df = df.copy() diff --git a/tests/price/test_yahoo_api.py b/tests/price/test_yahoo_api.py index 5c58106..0ea7f94 100644 --- a/tests/price/test_yahoo_api.py +++ b/tests/price/test_yahoo_api.py @@ -4,7 +4,6 @@ import pytest from tessa.price import yahoo -from tessa.price.types import SymbolNotFoundError @pytest.mark.parametrize( @@ -28,6 +27,5 @@ def test_api_returns_reasonable_data(query: str, expected_currency: str): @pytest.mark.net def test_non_existing_query_raises(): - with pytest.raises(SymbolNotFoundError) as excinfo: + with pytest.raises(Exception): yahoo.get_price_history("thisshouldntexistreally") - assert "No symbol found" in str(excinfo.value)