From 6844ef044bbaea6c3489e2177b5aa7f865336f4f Mon Sep 17 00:00:00 2001 From: saket3395 Date: Fri, 31 Jul 2026 12:33:21 +0530 Subject: [PATCH] fix: guard None lambda in CLA._solve case-b comparison Fixes #738. CLA.max_sharpe() raised TypeError: '>' not supported between instances of 'NoneType' and 'float' whenever all expected returns were equal. _compute_lambda returns (None, None) when its denominator c is exactly zero, which is guaranteed when all expected returns are identical. The case-a) branch of _solve already guards against this by comparing CLA._infnone(lam) instead of the raw lam, but the case-b) branch compared the raw lam directly, causing the crash. Wraps lam with CLA._infnone(...) in both comparisons of the case-b) branch, mirroring the existing case-a) pattern. When lam is None this now resolves to -inf, so the candidate is simply never selected as i_out/l_out, and the algorithm falls through to the minimum-variance solution instead of crashing. --- pypfopt/cla.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pypfopt/cla.py b/pypfopt/cla.py index bde53b19..b21aff6d 100644 --- a/pypfopt/cla.py +++ b/pypfopt/cla.py @@ -364,8 +364,8 @@ def _solve(self): self.w[-1][i], ) if ( - self.ls[-1] is None or lam < self.ls[-1] - ) and lam > CLA._infnone(l_out): + self.ls[-1] is None or CLA._infnone(lam) < self.ls[-1] + ) and CLA._infnone(lam) > CLA._infnone(l_out): l_out, i_out = lam, i if (l_in is None or l_in < 0) and (l_out is None or l_out < 0): # 3) compute minimum variance solution