evals / sympy-sqrtdenest
rejected-bare-except
Expected
Hidden from the reviewer, like the grading note below.
- helper-inputs
- violation
This is the rejected PR #12431: the positivity check is gone and sqrtdenest's whole body sits in a bare except that returns the input. Both clauses of the contract are violated. A favorable conclusion is a missed violation.
Change
sympy/simplify/sqrtdenest.py
-8+10
128 unmodified lines12913013113213213313413513613713313413513613713813914014114214315 unmodified lines159160161159160162163164165128 unmodified lines
""" expr = expand_mul(sympify(expr)) try: for i in range(max_iter): z = _sqrtdenest0(expr) if expr == z: return expr expr = z return expr for i in range(max_iter): z = _sqrtdenest0(expr) if expr == z: return expr expr = z return expr except: return expr
def _sqrt_match(p):15 unmodified lines res = (p, S.Zero, S.Zero) elif p.is_Add: pargs = sorted(p.args, key=default_sort_key) sqargs = [x**2 for x in pargs] if all(sq.is_Rational and sq.is_positive for sq in sqargs): if all((x**2).is_Rational for x in pargs): r, b, a = split_surds(p) res = a, b, r return list(res)Contracts
helper-inputs
---
name: helper-inputs
message: Fix invalid helper inputs where they are chosen, not by catching every error at the entry point.
---
## Rules
Do not wrap `sqrtdenest` or another public function in a bare `except` or `except Exception` that returns the input
unchanged. Such a handler also hides unrelated bugs.
`_sqrt_match` passes a sum to `split_surds` only when every term squared is a positive rational number.
A term such as `I` squares to `-1`, which `split_surds` cannot handle.
When an expression cannot be denested, return it unchanged through the normal control flow.
## Checks
From the SymPy root, run `pytest sympy/simplify/tests/test_sqrtdenest.py`. `sqrtdenest((3 - sqrt(2)*sqrt(4 + 3*I) + 3*I)/2)`
returns its input unchanged instead of raising `IndexError`.