Skip to content

fix HRPOpt.optimize() crash on scipy >= 1.18 (removed _LINKAGE_METHODS) - #760

Draft
kpj2006 wants to merge 1 commit into
PyPortfolio:mainfrom
kpj2006:scipy
Draft

fix HRPOpt.optimize() crash on scipy >= 1.18 (removed _LINKAGE_METHODS)#760
kpj2006 wants to merge 1 commit into
PyPortfolio:mainfrom
kpj2006:scipy

Conversation

@kpj2006

@kpj2006 kpj2006 commented Aug 31, 2026

Copy link
Copy Markdown

HRPOpt.optimize() calls sch._LINKAGE_METHODS to validate linkage_method before clustering. That private dict was removed from scipy.cluster.hierarchy's public surface in scipy 1.18's module → package refactor, so on a fresh install with scipy >= 1.18, HRPOpt.optimize() raises AttributeError before it can do anything — the feature is unreachable. This has been failing main's CI (nosoftdeps on Python 3.12/3.13/3.14) since 2026-07-07, and it also breaks cookbook/5-Hierarchical-Risk-Parity.ipynb, which uses HRPOpt.

…t; remove unused linkage_method check in HRPOpt

@kpj2006 kpj2006 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robertmartin8 @fkiraly @tschm is this correct way to do so ?

Comment on lines -179 to -181
if linkage_method not in sch._LINKAGE_METHODS:
raise ValueError("linkage_method must be one recognised by scipy")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the precheck entirely. scipy.cluster.hierarchy.linkage() already validates method internally (confirmed by reading scipy's current source: if method not in _LINKAGE_METHODS: raise ValueError(f"Invalid method: {method}"), that dict just isn't exposed as a module attribute anymore. Letting linkage() do its own validation means this doesn't re-break on a future scipy internal refactor.

Comment thread requirements.txt
Comment on lines +2 to +3
numpy>=1.26.0,<3.0.0
pandas>=1.0.0,<4.0.0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also synced requirements.txt's numpy/pandas bounds to match pyproject.toml (numpy>=1.0.0/pandas>=0.19 → numpy>=1.26.0,<3.0.0/pandas>=1.0.0,<4.0.0), found while checking dependency consistency, pip install -r requirements.txt was pulling much older, unsupported versions than the package actually requires.

@kpj2006
kpj2006 marked this pull request as draft August 31, 2026 05:56
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