Skip to content

Commit

Permalink
Remove use of deprecated ast.Num
Browse files Browse the repository at this point in the history
Since Python 3.8 (our minimum supported version), this has been silently
deprecated in favour of the catch-all `ast.Constant` (which `Num`
subclasses).  In Python 3.12, this deprecation is elevated to a warning,
which will cause a failure in our test suite when it releases.
  • Loading branch information
jakelishman committed Sep 22, 2023
1 parent 113f92a commit e68a080
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions qiskit/pulse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def __call__(self, *args, **kwargs) -> Union[complex, ast.Expression]:
Raises:
PulseError: When parameters are not bound.
"""
if isinstance(self._tree.body, (ast.Constant, ast.Num)):
return self._tree.body.n
if isinstance(self._tree.body, ast.Constant):
return self._tree.body.value

self._locals_dict.clear()
if args:
Expand All @@ -133,12 +133,12 @@ def __call__(self, *args, **kwargs) -> Union[complex, ast.Expression]:

expr = self.visit(self._tree)

if not isinstance(expr.body, (ast.Constant, ast.Num)):
if not isinstance(expr.body, ast.Constant):
if self._partial_binding:
return PulseExpression(expr, self._partial_binding)
else:
raise PulseError("Parameters %s are not all bound." % self.params)
return expr.body.n
return expr.body.value

@staticmethod
def _match_ops(opr: ast.AST, opr_dict: Dict, *args) -> complex:
Expand Down Expand Up @@ -174,19 +174,6 @@ def visit_Expression(self, node: ast.Expression) -> ast.Expression:

return tmp_node

def visit_Num(self, node: ast.Num) -> ast.Num:
"""Return number as it is.
Args:
node: Number.
Returns:
Input node.
"""
# node that Num node is deprecated in Python 3.8.
# Constant node is recommended.
return node

def visit_Constant(self, node: ast.Constant) -> ast.Constant:
"""Return constant value as it is.
Expand All @@ -211,7 +198,7 @@ def visit_Name(self, node: ast.Name) -> Union[ast.Name, ast.Constant]:
PulseError: When parameter value is not a number.
"""
if node.id in self._math_ops:
val = ast.Constant(n=self._math_ops[node.id])
val = ast.Constant(self._math_ops[node.id])
return ast.copy_location(val, node)
elif node.id in self._locals_dict:
_val = self._locals_dict[node.id]
Expand All @@ -226,7 +213,7 @@ def visit_Name(self, node: ast.Name) -> Union[ast.Name, ast.Constant]:
f"Invalid parameter value {node.id} = {self._locals_dict[node.id]} is "
"specified."
) from ex
val = ast.Constant(n=_val)
val = ast.Constant(_val)
return ast.copy_location(val, node)
self._params.add(node.id)
return node
Expand All @@ -242,8 +229,8 @@ def visit_UnaryOp(self, node: ast.UnaryOp) -> Union[ast.UnaryOp, ast.Constant]:
"""
node = copy.copy(node)
node.operand = self.visit(node.operand)
if isinstance(node.operand, (ast.Constant, ast.Num)):
val = ast.Constant(n=self._match_ops(node.op, self._unary_ops, node.operand.n))
if isinstance(node.operand, ast.Constant):
val = ast.Constant(self._match_ops(node.op, self._unary_ops, node.operand.value))
return ast.copy_location(val, node)
return node

Expand All @@ -259,11 +246,9 @@ def visit_BinOp(self, node: ast.BinOp) -> Union[ast.BinOp, ast.Constant]:
node = copy.copy(node)
node.left = self.visit(node.left)
node.right = self.visit(node.right)
if isinstance(node.left, (ast.Constant, ast.Num)) and isinstance(
node.right, (ast.Constant, ast.Num)
):
if isinstance(node.left, ast.Constant) and isinstance(node.right, ast.Constant):
val = ast.Constant(
n=self._match_ops(node.op, self._binary_ops, node.left.n, node.right.n)
self._match_ops(node.op, self._binary_ops, node.left.value, node.right.value)
)
return ast.copy_location(val, node)
return node
Expand All @@ -284,14 +269,14 @@ def visit_Call(self, node: ast.Call) -> Union[ast.Call, ast.Constant]:
raise PulseError("Unsafe expression is detected.")
node = copy.copy(node)
node.args = [self.visit(arg) for arg in node.args]
if all(isinstance(arg, (ast.Constant, ast.Num)) for arg in node.args):
if all(isinstance(arg, ast.Constant) for arg in node.args):
if node.func.id not in self._math_ops.keys():
raise PulseError("Function %s is not supported." % node.func.id)
_args = [arg.n for arg in node.args]
_args = [arg.value for arg in node.args]
_val = self._math_ops[node.func.id](*_args)
if not _val.imag:
_val = _val.real
val = ast.Constant(n=_val)
val = ast.Constant(_val)
return ast.copy_location(val, node)
return node

Expand Down

0 comments on commit e68a080

Please sign in to comment.