Skip to content

[BUG] fix HRPOpt.optimize raising AttributeError under scipy >= 1.18 - #751

Open
lenamonj wants to merge 1 commit into
PyPortfolio:mainfrom
lenamonj:fix/hrp-linkage-scipy-118
Open

[BUG] fix HRPOpt.optimize raising AttributeError under scipy >= 1.18#751
lenamonj wants to merge 1 commit into
PyPortfolio:mainfrom
lenamonj:fix/hrp-linkage-scipy-118

Conversation

@lenamonj

Copy link
Copy Markdown

Fixes item 3 of #750: HRPOpt.optimize() validates linkage_method against
scipy.cluster.hierarchy._LINKAGE_METHODS, a private attribute that scipy
1.18.0 removed, so every call raises AttributeError - including the default
linkage_method="single" - and CI is red on main (run 28899115146 at
a6638d2). pyproject.toml allows scipy>=1.3.0 unbounded, so a fresh
install resolves the broken combination.

Fix: delete the pre-check. scipy.cluster.hierarchy.linkage validates the
method itself and raises ValueError: Invalid method: <name> before touching
data, so the documented ValueError contract holds - the existing
test_hrp_errors assertion passes unchanged - and no private-API dependency
remains.

Test: test_hrp_linkage_methods runs optimize() over the seven linkage
methods documented by scipy.cluster.hierarchy.linkage and checks each
returns a full set of weights summing to 1. scipy exposes no public list of
valid methods, so the test pins the documented seven.

Measured on Python 3.13.8 / scipy 1.18.0 / numpy 2.5.1 / pandas 3.0.5 with
pytest ./tests -q:

  • main: 7 failed, 295 passed, 15 skipped - all seven failures are this
    AttributeError (five in test_hrp.py, two in test_plotting.py, both of
    which construct an HRPOpt).
  • The new test alone on main fails with the same AttributeError.
  • This branch: 303 passed, 15 skipped, 0 failed. ruff check and
    ruff format clean on both changed files.

If you would like an upper bound (scipy>=1.3.0,<2.0.0) so a future scipy
2.0 cannot repeat this failure mode, happy to add it here or as a separate
PR, whichever fits your dependency policy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant