Narrow factor loader format fallback handling

This commit is contained in:
2026-04-07 15:51:57 +08:00
parent 9e6da727a3
commit 0e94688066
2 changed files with 148 additions and 23 deletions

View File

@@ -9,8 +9,10 @@ from unittest import mock
import pandas as pd
from factor_attribution import (
ExternalFactorFormatError,
KEN_FRENCH_DAILY_FF5_ZIP_URL,
_download_kf_zip_bytes,
_parse_kf_daily_csv,
load_external_us_factors,
)
@@ -78,12 +80,52 @@ class ExternalFactorLoaderTests(unittest.TestCase):
"factor_attribution._download_kf_zip_bytes",
side_effect=URLError("boom"),
):
factors = load_external_us_factors(cache_dir=cache_dir)
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_load_external_us_factors_falls_back_to_cache_when_parse_fails(self):
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",
"not the expected file format\n20260102,1.00\n",
)
with self.assertRaises(ExternalFactorFormatError):
_parse_kf_daily_csv(zip_bytes)
def test_load_external_us_factors_warns_and_falls_back_to_cache_when_source_format_is_invalid(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")
malformed_zip_bytes = self._make_zip_bytes(
"F-F_Research_Data_5_Factors_2x3_daily.csv",
"not the expected file format\n20260102,1.00\n",
)
with mock.patch(
"factor_attribution._download_kf_zip_bytes",
return_value=malformed_zip_bytes,
):
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_load_external_us_factors_warns_and_falls_back_to_cache_when_zip_is_invalid(self):
cached = pd.DataFrame(
{
"MKT_RF": [0.01],
@@ -103,7 +145,8 @@ class ExternalFactorLoaderTests(unittest.TestCase):
"factor_attribution._download_kf_zip_bytes",
return_value=b"not-a-zip-file",
):
factors = load_external_us_factors(cache_dir=cache_dir)
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)
@@ -129,6 +172,43 @@ class ExternalFactorLoaderTests(unittest.TestCase):
with self.assertRaises(OSError):
load_external_us_factors(cache_dir=Path(tmpdir))
def test_load_external_us_factors_does_not_swallow_unrelated_local_failures(self):
csv_text = (
"This line is ignored\n"
",Mkt-RF,SMB,HML,RMW,CMA,RF\n"
"20260102,1.00,0.50,-0.25,0.10,-0.05,0.02\n"
"\n"
)
zip_bytes = self._make_zip_bytes(
"F-F_Research_Data_5_Factors_2x3_daily.csv",
csv_text,
)
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._download_kf_zip_bytes",
return_value=zip_bytes,
):
with mock.patch(
"factor_attribution._parse_kf_daily_csv",
side_effect=RuntimeError("unexpected local bug"),
):
with self.assertRaises(RuntimeError):
load_external_us_factors(cache_dir=cache_dir)
def _make_zip_bytes(self, filename: str, contents: str) -> bytes:
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, mode="w") as archive: