Fix eval_expr to raise ValueError on division by zero - #1810
Merged
Conversation
eval_expr documents that it raises ValueError for invalid expressions, and it is used to evaluate the pre_dispatch argument of Parallel. But an expression dividing or taking a modulo by zero raised an uncaught ZeroDivisionError. Catch it as well so the documented ValueError is raised, and cover 1/0, 1//0 and 1%0 in the invalid-expression test.
Sreekant13
force-pushed
the
fix/eval-expr-zerodivision
branch
from
July 2, 2026 23:23
390202f to
a51fb60
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1810 +/- ##
==========================================
- Coverage 94.15% 94.12% -0.03%
==========================================
Files 46 46
Lines 7970 7970
==========================================
- Hits 7504 7502 -2
- Misses 466 468 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
Looks good to me. I think we can merge. |
Contributor
Author
|
Thanks for the review, @Nanored4498! Happy to rebase if needed, otherwise this should be good to merge whenever you get a moment. Thanks again! |
Contributor
Author
|
Thank you for merging this, @tomMoral! And thanks @Nanored4498 for the review. Much appreciated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
eval_expris documented to "Raise ValueError if the expression is invalid", and it's used to evaluate thepre_dispatchargument ofParallel(e.g.Parallel(pre_dispatch="2*n_jobs")). It catchesTypeError,SyntaxError,OverflowErrorandKeyError, but notZeroDivisionError, so an expression that divides or takes a modulo by zero leaks the raw error instead of the documentedValueError:This catches
ZeroDivisionErroras well, so 1/0, 1//0 and 1%0 now raise the documentedValueError. Added those cases totest_eval_expr_invalidand aCHANGES.rstentry.