Skip to content
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

Merged
merged 2 commits into from
Aug 13, 2023
Merged

Conversation

dgw
Copy link
Member

@dgw dgw commented Jun 6, 2023

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

  • I have read CONTRIBUTING.md
  • I can and do license this contribution under the EFLv2
  • No issues are reported by make qa (runs make quality and make test)
  • I have tested the functionality of the things this change touches

@dgw dgw added the Housekeeping Code cleanup, removal of deprecated stuff, etc. label Jun 6, 2023
@dgw dgw added this to the 8.0.0 milestone Jun 6, 2023
@dgw dgw requested a review from a team June 6, 2023 22:12
@dgw dgw marked this pull request as draft June 6, 2023 22:16
sopel/tools/calculation.py Outdated Show resolved Hide resolved
sopel/tools/calculation.py Outdated Show resolved Hide resolved
@dgw dgw force-pushed the rm-ast.Num branch 2 times, most recently from bac08f9 to 6095c54 Compare August 9, 2023 08:34
@dgw
Copy link
Member Author

dgw commented Aug 9, 2023

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 test/tools/test_tools_calculation.py suite. But that's out of scope of this PR, and doesn't need to be done for 8.0 specifically, so I will mark this as ready for review and just show up with the test suite later when I'm done with it.

@dgw dgw marked this pull request as ready for review August 9, 2023 08:37
@dgw dgw requested a review from SnoopJ August 10, 2023 02:03
Copy link
Contributor

@SnoopJ SnoopJ left a 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!

dgw and others added 2 commits August 9, 2023 19:27
`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.
Copy link
Contributor

@Exirel Exirel left a 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.

@SnoopJ
Copy link
Contributor

SnoopJ commented Aug 13, 2023

Had a go at writing a basic set of tests for tools.calculation if you'd like to add them to this PR. Also wrote some up for the calc, dice plugins, I've opened #2503 for those (but can add the test listed here to that one if you'd prefer, let me know)

click for diff
diff --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

@dgw
Copy link
Member Author

dgw commented Aug 13, 2023

Had a go at writing a basic set of tests for tools.calculation if you'd like to add them to this PR.

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?).

@dgw dgw merged commit 7b74964 into master Aug 13, 2023
13 checks passed
@dgw dgw deleted the rm-ast.Num branch August 13, 2023 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Housekeeping Code cleanup, removal of deprecated stuff, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants