-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(safeeval): evaluate rewritten AST instead of original string #9063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||
| import ast | ||||||||||||||||||||
| import unittest | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||
| from parameterized import parameterized | ||||||||||||||||||||
|
|
||||||||||||||||||||
| from monai.utils import safe_eval | ||||||||||||||||||||
|
|
@@ -62,6 +63,34 @@ def test_allowed_types(self): | |||||||||||||||||||
| with self.assertRaises(ValueError): | ||||||||||||||||||||
| safe_eval("1*2", allowed_types=allowed) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def test_rewrite_np_produces_numpy_types(self): | ||||||||||||||||||||
| """Test that rewrite_np wraps literals in numpy types.""" | ||||||||||||||||||||
| result = safe_eval("2 + 3", rewrite_np=True) | ||||||||||||||||||||
| self.assertIsInstance(result, np.integer) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| result = safe_eval("2.5 + 1.5", rewrite_np=True) | ||||||||||||||||||||
| self.assertIsInstance(result, np.floating) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def test_rewrite_np_large_exponent(self): | ||||||||||||||||||||
| """Test that rewrite_np prevents slow native-Python exponentiation.""" | ||||||||||||||||||||
| # Under native Python, 9**9**9 produces a ~369-million-digit integer; | ||||||||||||||||||||
| # under np.int32 it overflows and completes almost instantly. | ||||||||||||||||||||
| result = safe_eval("9**9**9", rewrite_np=True) | ||||||||||||||||||||
| self.assertIsInstance(result, np.integer) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def test_rewrite_np_preserves_bool(self): | ||||||||||||||||||||
| """Test that rewrite_np does not wrap bool constants.""" | ||||||||||||||||||||
| result = safe_eval("True", rewrite_np=True) | ||||||||||||||||||||
| self.assertIs(result, True) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| result = safe_eval("False", rewrite_np=True) | ||||||||||||||||||||
| self.assertIs(result, False) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def test_rewrite_np_inf_constant(self): | ||||||||||||||||||||
| """Test that rewrite_np handles inf/nan literals correctly.""" | ||||||||||||||||||||
| result = safe_eval("1e309", rewrite_np=True) | ||||||||||||||||||||
| self.assertIsInstance(result, np.floating) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+89
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Assert that the result is infinite.
Proposed fix def test_rewrite_np_inf_constant(self):
- """Test that rewrite_np handles inf/nan literals correctly."""
+ """Test that rewrite_np handles overflowing infinity literals."""
result = safe_eval("1e309", rewrite_np=True)
self.assertIsInstance(result, np.floating)
+ self.assertTrue(np.isinf(result))📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Path instructions |
||||||||||||||||||||
|
|
||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.