From bac08f9cb0d84eaff8c946a8ba86d752e30369ec Mon Sep 17 00:00:00 2001 From: dgw Date: Sat, 3 Jun 2023 03:07:47 -0500 Subject: [PATCH] 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. --- sopel/tools/calculation.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sopel/tools/calculation.py b/sopel/tools/calculation.py index bc162899c..c6a208d49 100644 --- a/sopel/tools/calculation.py +++ b/sopel/tools/calculation.py @@ -56,8 +56,11 @@ def _eval_node(self, node, timeout): A subclass could overwrite this to handle more nodes, calling it only for nodes it does not implement itself. """ - if isinstance(node, ast.Num): - return node.n + if ( + isinstance(node, ast.Constant) and + isinstance(node.value, (int, float)) + ): + return node.value elif (isinstance(node, ast.BinOp) and type(node.op) in self.binary_ops): @@ -77,7 +80,7 @@ def _eval_node(self, node, timeout): return self.unary_ops[type(node.op)](operand) raise ExpressionEvaluator.Error( - "Ast.Node '%s' not implemented." % (type(node).__name__,)) + "ast.Node '%s' not implemented." % (type(node).__name__,)) def guarded_mul(left, right):