Skip to content

Commit

Permalink
Expose/raise all yfinance exceptions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ymyke committed Oct 16, 2023
1 parent 9dcd8c8 commit 2462be6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
8 changes: 2 additions & 6 deletions tessa/price/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions tests/price/test_yahoo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pytest
from tessa.price import yahoo
from tessa.price.types import SymbolNotFoundError


@pytest.mark.parametrize(
Expand All @@ -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)

0 comments on commit 2462be6

Please sign in to comment.