Skip to content

Commit bac08f9

Browse files
committed
tools.calculation: replace deprecated ast.Num with ast.Constant
`ast.Num` and friends have been deprecated since Python 3.8, but only started to generate warnings in the prereleases of Python 3.12. Feels like stdlib made a bad trade here, simplifying Python internals in exchange for users writing uglier, more complicated type checks such as this one. But it's been so long, it's not as if they'll un-deprecate the older, more-convenient-for-the-user classes.
1 parent d58d6a7 commit bac08f9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

sopel/tools/calculation.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ def _eval_node(self, node, timeout):
5656
A subclass could overwrite this to handle more nodes, calling it only
5757
for nodes it does not implement itself.
5858
"""
59-
if isinstance(node, ast.Num):
60-
return node.n
59+
if (
60+
isinstance(node, ast.Constant) and
61+
isinstance(node.value, (int, float))
62+
):
63+
return node.value
6164

6265
elif (isinstance(node, ast.BinOp) and
6366
type(node.op) in self.binary_ops):
@@ -77,7 +80,7 @@ def _eval_node(self, node, timeout):
7780
return self.unary_ops[type(node.op)](operand)
7881

7982
raise ExpressionEvaluator.Error(
80-
"Ast.Node '%s' not implemented." % (type(node).__name__,))
83+
"ast.Node '%s' not implemented." % (type(node).__name__,))
8184

8285

8386
def guarded_mul(left, right):

0 commit comments

Comments
 (0)