Skip to content

Commit

Permalink
tools.calculation: replace deprecated ast.Num with ast.Constant
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
dgw committed Aug 9, 2023
1 parent d58d6a7 commit bac08f9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sopel/tools/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit bac08f9

Please sign in to comment.