Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix eval_expr to raise ValueError on division by zero - #1810

Merged
tomMoral merged 1 commit into
joblib:mainfrom
Sreekant13:fix/eval-expr-zerodivision
Jul 15, 2026
Merged

Fix eval_expr to raise ValueError on division by zero#1810
tomMoral merged 1 commit into
joblib:mainfrom
Sreekant13:fix/eval-expr-zerodivision

Conversation

@Sreekant13

Copy link
Copy Markdown
Contributor

eval_expr is documented to "Raise ValueError if the expression is invalid", and it's used to evaluate the pre_dispatch argument of Parallel (e.g. Parallel(pre_dispatch="2*n_jobs")). It catches TypeError, SyntaxError, OverflowError and KeyError, but not ZeroDivisionError, so an expression that divides or takes a modulo by zero leaks the raw error instead of the documented ValueError:

from joblib._utils import eval_expr

eval_expr("1/0")   # ZeroDivisionError: division by zero

This catches ZeroDivisionError as well, so 1/0, 1//0 and 1%0 now raise the documented ValueError. Added those cases to test_eval_expr_invalid and a CHANGES.rst entry.

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
Sreekant13 force-pushed the fix/eval-expr-zerodivision branch from 390202f to a51fb60 Compare July 2, 2026 23:23
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.12%. Comparing base (b030e4e) to head (a51fb60).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Nanored4498

Copy link
Copy Markdown
Contributor

Looks good to me. I think we can merge.

@Sreekant13

Copy link
Copy Markdown
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!

@tomMoral tomMoral left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@tomMoral
tomMoral merged commit 86671ba into joblib:main Jul 15, 2026
37 checks passed
@Sreekant13

Copy link
Copy Markdown
Contributor Author

Thank you for merging this, @tomMoral! And thanks @Nanored4498 for the review. Much appreciated.

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.

3 participants