CEF stage 2b: per-share financial-highlights parser (cef_annual.py)

Joint backtracking placement for CEF tables that drop zero columns;
NAV-identity fallback when the printed total row is short; 4 fund-
family layouts verified (Franklin/classic/abrdn/Korea). 16/50 of the
CEF shortlist now have a verified div/gains/ROC split + FY-end
discount series. 17 new parser unit tests (123/123 green).
This commit is contained in:
gmp
2026-08-28 07:30:16 -04:00
parent d1e85026bf
commit 6f8bde4c35
58 changed files with 2816 additions and 5 deletions
+184
View File
@@ -610,6 +610,186 @@ def test_cef() -> None:
{"aaa", "bbb", "ccc"} <= set(sel), str(sel))
# CEF annual-report per-share highlights parser
# ----------------------------------------------------------------------
_DOC_FRANKLIN = """\
Financial highlights For a common share of capital stock outstanding
throughout each year ended November 30: 2025 1 2024 1 2023 1 2022 1 2021 1
Net
asset value, beginning of year
$55.82
$38.75
$35.97
$26.53
$17.13
Income
(loss) from operations:
Net
investment loss
(0.13
)
(0.51
)
(0.69
)
(0.43
)
(0.36
)
Net
realized and unrealized gain (loss)
(2.12
)
20.58
5.80
11.65
11.01
Total
income (loss) from operations
(2.25)
20.07
5.11
11.22
10.65
Less
distributions to common
shareholders
from:
Dividends
(0.81
)
(3.00
)
(2.37
)
(1.92
)
(0.54
)
Return of capital
(3.42
)
(0.93
)
Total distributions to common shareholders
4.23
3.00
2.37
1.92
1.47
Anti-dilutive impact of repurchase plan 0.04 0.14 0.22
Dilutive impact of rights offering (0.46) (0.27) (0.13) (0.09) (0.13)
Anti-dilutive impact of tender offer plan 0.00 0.00 0.00 0.00 0.00
Net asset value, end of year $48.88 $55.82 $38.75 $35.97 $26.53
Market price, end of year $45.55 $50.49 $34.50 $30.43 $21.65
"""
_DOC_CLASSIC = """\
Selected Per Share Data Net asset value, beginning of year $ 53.78 $ 39.73
$ 29.96 $ 42.83 $ 28.79 Income from Investment Operations: Net investment
income (a) 0.30 0.43 0.41 (b) 0.33 0.04 Net realized and unrealized gain
(loss) on investments and foreign currency transactions 11.62 13.13 9.32 (c)
(10.28) 17.31 Total from investment operations 11.92 13.56 9.73 (9.95) 17.35
Less Distributions to Stockholders from: Net investment income (0.48) (0.38)
Net realized gains (7.40) (0.44) (2.44) (2.93) Total distributions to
stockholders (7.40) (0.44) (2.92) (3.31) Capital Share Transactions:
Accretion (dilution) to net asset value, resulting from share repurchase
program, tender offer or issuance of shares for the reinvestment of
distributions from net investment income and net realized gains 0.83 0.93
0.04 0.00 (d) 0.00 (d) Net asset value, end of year $ 59.13 $ 53.78 $ 39.73
$ 29.96 $ 42.83 Market value, end of year $ 50.25 $ 44.73 $ 31.10 $ 25.20
$ 35.83
"""
_DOC_ABRDN = """\
PER SHARE OPERATING PERFORMANCE: Net asset value per common share, beginning
of year $16.38 $15.00 $15.49 $21.22 $20.25 Net investment loss (c) (0.12)
(0.07) (0.08) (0.12) (0.17) Net realized and unrealized gains/(losses) on
investments, written options and foreign currency transactions 2.91 3.11 0.87
(4.14) 2.83 Total from investment operations applicable to common
shareholders 2.79 3.04 0.79 (4.26) 2.66 Distributions to common shareholders
from: Net investment income (1.72) (0.69) (0.02) (0.03) (0.42) Net realized
gains (0.10) (0.76) (1.26) (1.44) (1.27) Return of capital (0.21) Total
distributions (1.82) (1.66) (1.28) (1.47) (1.69) Net asset value per common
share, end of year $17.35 $16.38 $15.00 $15.49 $21.22 Market price, end of
year $15.51 $15.08 $12.47 $13.66 $20.80
"""
_DOC_KOREA = """\
The Korea Fund, Inc. Financial Highlights For a share of stock outstanding
throughout each year: Year ended June 30, 2026 2025 2024 2023 2022 Net
asset value, beginning of year $30.87 $28.78 $26.52 $28.54 $54.37 Investment
Operations: Net investment income (1) (0.05) 0.02 0.09 0.19 0.32 Net realized
and change in unrealized gain (loss) 58.44 2.39 2.08 1.06 (17.05) Total from
investment operations 58.39 2.41 2.17 1.25 (16.73) Dividends and
Distributions to Stockholders from: Net investment income (1.38) (0.45)
(0.03) (2.05) Net realized gains (3.27) (7.06) Return of capital (0.02)
Total dividends and distributions to stockholders (1.38) (0.45) (3.32) (9.11)
Common Stock Transactions: Accretion to net asset value resulting from share
repurchases and tender offer 0.13 0.09 0.05 0.01 Net asset value, end of
year $87.88 $30.87 $28.78 $26.52 $28.54 Market price, end of year $75.21
$26.93 $24.13 $23.14 $24.35
"""
def test_cef_annual() -> None:
from fundlab import cef_annual as cn
# Franklin style (EMO): td-split labels, ROC in cols 0 and 4,
# positive-printed total row
h = cn.parse_highlights(_DOC_FRANKLIN)
check("franklin parses 5 cols", h is not None and h["ncols"] == 5, str(h))
check("franklin div share ~66.5%",
abs(h["share_div"] - 8.64 / 12.99) < 0.005, str(h["share_div"]))
check("franklin gains zero", h["share_gains"] == 0.0)
check("franklin roc share ~33.5%",
abs(h["share_roc"] - 4.35 / 12.99) < 0.005, str(h["share_roc"]))
check("franklin FY-end discount ~-6.8%",
abs(h["mkt_end"][0] / h["nav_end"][0] - 1 - (-0.0681)) < 0.001)
# Classic CEF (TWN): printed total row is SHORT (a zero year dropped),
# so totals derive from the NAV identity; gains sit in 4 of 5 cols
# and NII-divs in the OTHER two
h = cn.parse_highlights(_DOC_CLASSIC)
check("classic parses 5 cols", h is not None and h["ncols"] == 5, str(h))
check("classic derived total col0 = 7.40",
abs(h["dist_tot"][0] - 7.40) < 0.01, str(h["dist_tot"]))
check("classic zero-year col2", abs(h["dist_tot"][2]) < 0.01,
str(h["dist_tot"]))
check("classic gains ~93.9%",
abs(h["share_gains"] - 13.21 / 14.07) < 0.005, str(h["share_gains"]))
check("classic NII ~6.1%",
abs(h["share_div"] - 0.86 / 14.07) < 0.005, str(h["share_div"]))
# abrdn style (HQL): "per common share" labels, single-value ROC row
h = cn.parse_highlights(_DOC_ABRDN)
check("abrdn parses 5 cols", h is not None and h["ncols"] == 5, str(h))
check("abrdn div ~36.4%",
abs(h["share_div"] - 2.88 / 7.92) < 0.005, str(h["share_div"]))
check("abrdn gains ~61.0%",
abs(h["share_gains"] - 4.83 / 7.92) < 0.005, str(h["share_gains"]))
check("abrdn roc ~2.7%",
abs(h["share_roc"] - 0.21 / 7.92) < 0.005, str(h["share_roc"]))
# Korea style (KF): "Total dividends and distributions" label (the
# divs regex must not grab the total row), accretion row in cols 1-4
h = cn.parse_highlights(_DOC_KOREA)
check("korea parses 5 cols", h is not None and h["ncols"] == 5, str(h))
check("korea div ~27.4%",
abs(h["share_div"] - 3.91 / 14.26) < 0.01, str(h["share_div"]))
check("korea gains ~72.4%",
abs(h["share_gains"] - 10.33 / 14.26) < 0.01,
str(h["share_gains"]))
# unreconcilable table must be rejected, not guessed
bad = _DOC_CLASSIC.replace("(7.40) (0.44) (2.44) (2.93)",
"(7.99) (0.44) (2.44) (2.93)")
check("unreconcilable table rejected",
cn.parse_highlights(bad) is None)
def main() -> int:
test_pool()
test_text_and_objective()
@@ -625,6 +805,7 @@ def main() -> int:
test_taxplan()
test_drawdown()
test_cef()
test_cef_annual()
test_edgar_live()
print(f"\n{PASS} passed, {FAIL} failed")
return 1 if FAIL else 0
@@ -632,3 +813,6 @@ def main() -> int:
if __name__ == "__main__":
sys.exit(main())
# ----------------------------------------------------------------------