yf176 commited on
Commit ·
00896f8
1
Parent(s): fb8d495
fix(options): expose empty Yahoo response diagnostics
Browse files
src/polyhedra/services/options_data.py
CHANGED
|
@@ -93,6 +93,7 @@ class YFinanceOptionsDataProvider:
|
|
| 93 |
)
|
| 94 |
|
| 95 |
last_error: Exception | None = None
|
|
|
|
| 96 |
for _attempt in range(2):
|
| 97 |
try:
|
| 98 |
# Yahoo occasionally returns an empty expiration list for a valid ETF.
|
|
@@ -100,6 +101,7 @@ class YFinanceOptionsDataProvider:
|
|
| 100 |
ticker = yf.Ticker(symbol)
|
| 101 |
expirations = list(getattr(ticker, "options", []) or [])
|
| 102 |
if not expirations:
|
|
|
|
| 103 |
continue
|
| 104 |
|
| 105 |
checked = expirations[: self.max_expirations]
|
|
@@ -149,14 +151,42 @@ class YFinanceOptionsDataProvider:
|
|
| 149 |
status="error",
|
| 150 |
note=f"Options provider fetch failed after retry: {last_error}",
|
| 151 |
)
|
|
|
|
| 152 |
return OptionsDataSnapshot(
|
| 153 |
symbol=symbol,
|
| 154 |
provider=self.provider,
|
| 155 |
retrieved_at=retrieved_at,
|
| 156 |
-
status=
|
| 157 |
-
note=
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
def _contracts_from_frame(
|
| 161 |
self,
|
| 162 |
symbol: str,
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
last_error: Exception | None = None
|
| 96 |
+
empty_ticker: Any = None
|
| 97 |
for _attempt in range(2):
|
| 98 |
try:
|
| 99 |
# Yahoo occasionally returns an empty expiration list for a valid ETF.
|
|
|
|
| 101 |
ticker = yf.Ticker(symbol)
|
| 102 |
expirations = list(getattr(ticker, "options", []) or [])
|
| 103 |
if not expirations:
|
| 104 |
+
empty_ticker = ticker
|
| 105 |
continue
|
| 106 |
|
| 107 |
checked = expirations[: self.max_expirations]
|
|
|
|
| 151 |
status="error",
|
| 152 |
note=f"Options provider fetch failed after retry: {last_error}",
|
| 153 |
)
|
| 154 |
+
status, note = self._empty_expirations_diagnostic(empty_ticker, symbol)
|
| 155 |
return OptionsDataSnapshot(
|
| 156 |
symbol=symbol,
|
| 157 |
provider=self.provider,
|
| 158 |
retrieved_at=retrieved_at,
|
| 159 |
+
status=status,
|
| 160 |
+
note=note,
|
| 161 |
)
|
| 162 |
|
| 163 |
+
@staticmethod
|
| 164 |
+
def _empty_expirations_diagnostic(ticker: Any, symbol: str) -> tuple[str, str]:
|
| 165 |
+
"""Inspect the same Yahoo response yfinance silently maps to empty options."""
|
| 166 |
+
fallback = "yfinance returned no option expirations after retry; Yahoo response unknown."
|
| 167 |
+
try:
|
| 168 |
+
from yfinance.base import _BASE_URL_
|
| 169 |
+
|
| 170 |
+
response = ticker._data.get(url=f"{_BASE_URL_}/v7/finance/options/{symbol}")
|
| 171 |
+
status_code = response.status_code
|
| 172 |
+
body = response.json()
|
| 173 |
+
option_chain = body.get("optionChain", {}) if isinstance(body, dict) else {}
|
| 174 |
+
error = option_chain.get("error") if isinstance(option_chain, dict) else None
|
| 175 |
+
if isinstance(error, dict):
|
| 176 |
+
code = str(error.get("code") or "UNKNOWN")[:80]
|
| 177 |
+
description = str(error.get("description") or "")[:160]
|
| 178 |
+
return "error", f"Yahoo options HTTP {status_code}: {code}; {description}"
|
| 179 |
+
if status_code >= 400:
|
| 180 |
+
return "error", f"Yahoo options HTTP {status_code} with no structured error."
|
| 181 |
+
if isinstance(option_chain, dict) and not option_chain.get("result"):
|
| 182 |
+
return "unavailable", f"Yahoo options HTTP {status_code} returned no optionChain.result."
|
| 183 |
+
return "error", (
|
| 184 |
+
f"yfinance returned no expirations, but diagnostic Yahoo HTTP {status_code} "
|
| 185 |
+
"contained an option result."
|
| 186 |
+
)
|
| 187 |
+
except Exception as exc:
|
| 188 |
+
return "unavailable", f"{fallback} Diagnostic failed: {type(exc).__name__}."
|
| 189 |
+
|
| 190 |
def _contracts_from_frame(
|
| 191 |
self,
|
| 192 |
symbol: str,
|
tests/test_options_typed.py
CHANGED
|
@@ -134,6 +134,55 @@ def test_yfinance_provider_retries_empty_expiration_response(monkeypatch):
|
|
| 134 |
assert len(snapshot.contracts) == 1
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def runtime(tmp_path: Path, provider: FakeOptionsProvider):
|
| 138 |
session = tmp_path / "session1"
|
| 139 |
session.mkdir()
|
|
|
|
| 134 |
assert len(snapshot.contracts) == 1
|
| 135 |
|
| 136 |
|
| 137 |
+
@pytest.mark.parametrize(
|
| 138 |
+
("status_code", "body", "expected_status", "expected_note"),
|
| 139 |
+
[
|
| 140 |
+
(
|
| 141 |
+
401,
|
| 142 |
+
{"optionChain": {"result": None, "error": {
|
| 143 |
+
"code": "Unauthorized", "description": "Access denied",
|
| 144 |
+
}}},
|
| 145 |
+
"error",
|
| 146 |
+
"Yahoo options HTTP 401: Unauthorized; Access denied",
|
| 147 |
+
),
|
| 148 |
+
(
|
| 149 |
+
200,
|
| 150 |
+
{"optionChain": {"result": [], "error": None}},
|
| 151 |
+
"unavailable",
|
| 152 |
+
"Yahoo options HTTP 200 returned no optionChain.result.",
|
| 153 |
+
),
|
| 154 |
+
],
|
| 155 |
+
)
|
| 156 |
+
def test_yfinance_empty_expirations_preserve_yahoo_diagnostic(
|
| 157 |
+
monkeypatch, status_code, body, expected_status, expected_note,
|
| 158 |
+
):
|
| 159 |
+
calls = []
|
| 160 |
+
|
| 161 |
+
class FakeTicker:
|
| 162 |
+
def __init__(self, symbol):
|
| 163 |
+
calls.append(symbol)
|
| 164 |
+
self._data = SimpleNamespace(get=lambda **_: SimpleNamespace(
|
| 165 |
+
status_code=status_code,
|
| 166 |
+
json=lambda: body,
|
| 167 |
+
))
|
| 168 |
+
|
| 169 |
+
@property
|
| 170 |
+
def options(self):
|
| 171 |
+
return ()
|
| 172 |
+
|
| 173 |
+
monkeypatch.setitem(sys.modules, "yfinance", SimpleNamespace(Ticker=FakeTicker))
|
| 174 |
+
monkeypatch.setitem(
|
| 175 |
+
sys.modules, "yfinance.base", SimpleNamespace(_BASE_URL_="https://query2.finance.yahoo.com"),
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
snapshot = YFinanceOptionsDataProvider().fetch("SPY")
|
| 179 |
+
|
| 180 |
+
assert calls == ["SPY", "SPY"]
|
| 181 |
+
assert snapshot.status == expected_status
|
| 182 |
+
assert snapshot.note == expected_note
|
| 183 |
+
assert snapshot.contracts == []
|
| 184 |
+
|
| 185 |
+
|
| 186 |
def runtime(tmp_path: Path, provider: FakeOptionsProvider):
|
| 187 |
session = tmp_path / "session1"
|
| 188 |
session.mkdir()
|