Handle HTTP protocol errors in factor download
This commit is contained in:
@@ -43,6 +43,7 @@ def _download_kf_zip_bytes() -> bytes:
|
||||
socket.timeout,
|
||||
socket.gaierror,
|
||||
ssl.SSLError,
|
||||
http.client.HTTPException,
|
||||
http.client.IncompleteRead,
|
||||
http.client.RemoteDisconnected,
|
||||
) as exc:
|
||||
|
||||
@@ -138,6 +138,29 @@ class ExternalFactorLoaderTests(unittest.TestCase):
|
||||
self.assertEqual(len(factors), 1)
|
||||
self.assertAlmostEqual(factors.iloc[0]["MKT_RF"], 0.01)
|
||||
|
||||
def test_load_external_us_factors_falls_back_to_cache_when_http_status_line_is_bad(self):
|
||||
cached = pd.DataFrame(
|
||||
{
|
||||
"MKT_RF": [0.01],
|
||||
"SMB": [0.0],
|
||||
"HML": [0.0],
|
||||
"RMW": [0.0],
|
||||
"CMA": [0.0],
|
||||
"RF": [0.0001],
|
||||
},
|
||||
index=pd.to_datetime(["2026-01-02"]),
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
cache_dir = Path(tmpdir)
|
||||
cached.to_csv(cache_dir / "ff5_us_daily.csv")
|
||||
with mock.patch("factor_attribution.urlopen", side_effect=http.client.BadStatusLine("HTTP/1.1 ???")):
|
||||
with self.assertWarnsRegex(UserWarning, "cached data"):
|
||||
factors = load_external_us_factors(cache_dir=cache_dir)
|
||||
|
||||
self.assertEqual(len(factors), 1)
|
||||
self.assertAlmostEqual(factors.iloc[0]["MKT_RF"], 0.01)
|
||||
|
||||
def test_parse_kf_daily_csv_raises_external_factor_format_error_for_missing_header(self):
|
||||
zip_bytes = self._make_zip_bytes(
|
||||
"F-F_Research_Data_5_Factors_2x3_daily.csv",
|
||||
|
||||
Reference in New Issue
Block a user