-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
tools.calculation: replace deprecated ast.Num
with ast.Constant
#2464
Conversation
bac08f9
to
6095c54
Compare
Well, there are no direct tests of this module… Even though CI passes, I don't really trust it that much. I've started work on a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Not having tests is I think no great sin, since that's not a change to the state of the module prior to this PR. Nice to have it covered, but doesn't need to be another thing weighing down 8.0.
Ship it!
`ast.Num` and friends have been deprecated since Python 3.8, but didn't start generating deprecation warnings until prereleases of Python 3.12. Feels like stdlib made a bad trade here, simplifying Python internals in exchange for making users write uglier, more complicated type checks such as this patch implements. But it's been so long, it's not as if they'll un-deprecate the older, more-convenient-for-the-user classes.
Co-authored-by: SnoopJ <snoopjedi@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing to add to the list of stuff to add tests for I guess.
At some point, we might also consider deprecating this tool, and extract the plugin that needs that code.
Had a go at writing a basic set of tests for click for diffdiff --git a/test/tools/test_tools_calculation.py b/test/tools/test_tools_calculation.py
new file mode 100644
index 00000000..9f858285
--- /dev/null
+++ b/test/tools/test_tools_calculation.py
@@ -0,0 +1,35 @@
+from __future__ import annotations
+
+import ast
+import operator
+
+import pytest
+
+from sopel.tools.calculation import EquationEvaluator, ExpressionEvaluator
+
+
+def test_expression_eval():
+ OPS = {
+ ast.Add: operator.add,
+ ast.Sub: operator.sub,
+ }
+ evaluator = ExpressionEvaluator(bin_ops=OPS)
+
+ assert evaluator("1 + 1") == 2
+ assert evaluator("43 - 1") == 42
+ assert evaluator("1 + 1 - 2") == 0
+
+ with pytest.raises(ExpressionEvaluator.Error):
+ evaluator("2 * 2")
+
+
+def test_equation_eval():
+ evaluator = EquationEvaluator()
+
+ assert evaluator("1 + 1") == 2
+ assert evaluator("43 - 1") == 42
+ assert evaluator("(((1 + 1 + 2) * 3 / 5) ** 8 - 13) // 21 % 35") == 16.0
+ assert evaluator("-42") == -42
+ assert evaluator("-(-42)") == 42
+ assert evaluator("+42") == 42
+ assert evaluator("3 ^ 2") == 9 |
I've adopted them into my WIP branch for adding tests to this module. This PR is already double-approved, and I'd rather not kibosh that status. 😁 But I appreciate that your quick patch gets the module to 81% coverage already! I'll have a look at the helpers and error cases at my leisure, probably for submission to the 8.1 cycle (coincidence?). |
ast.Num
and friends have been deprecated since Python 3.8, but only started to generate warnings in Python 3.12 prereleases.Feels like stdlib made a bad trade here, simplifying Python internals in exchange for making users write uglier, more complicated type checks such as this. But it's been so long, it's not as if they'll un-deprecate the older, more-convenient-for-the-user classes.
Compatibility
I was under the impression that
ast.Constant
would work as far back as Python 3.6, but apparently not. This change has to wait until we're ready to drop Python 3.7.Checklist
make qa
(runsmake quality
andmake test
)