Skip to content

Commit bb6ecb3

Browse files
committed
Add ast.Constant support for Python 3.8+ in _ast_eval
Updated the _ast_eval function to handle ast.Constant nodes for compatibility with Python 3.8 and newer, where ast.Num is deprecated. This ensures correct evaluation of constants across Python versions.
1 parent a230105 commit bb6ecb3

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

hyperpyyaml/core.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import re
1010
import ast
11+
import sys
1112
import yaml
1213
import copy
1314
import pydoc
@@ -715,9 +716,18 @@ def _ast_eval(node):
715716
ast.Pow: op.pow,
716717
ast.Mod: op.mod,
717718
}
718-
if isinstance(node, ast.Num): # <number>
719-
return node.n
720-
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
719+
720+
# Compatibility check for Python versions
721+
# In Python 3.8, ast.Num was deprecated and replaced by ast.Constant.
722+
if sys.version_info >= (3, 8):
723+
if isinstance(node, ast.Constant): # <number>, <string>, <bool>, <None>
724+
return node.value
725+
else:
726+
# For Python versions older than 3.8
727+
if isinstance(node, ast.Num): # <number>
728+
return node.n
729+
730+
if isinstance(node, ast.BinOp): # <left> <operator> <right>
721731
return ops[type(node.op)](_ast_eval(node.left), _ast_eval(node.right))
722732
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
723733
return ops[type(node.op)](_ast_eval(node.operand))

0 commit comments

Comments
 (0)