Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions rope/base/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,31 @@
# Suppress the mypy complaint: Module "ast" has no attribute "_const_node_type_names"
from ast import _const_node_type_names # type:ignore
except ImportError:
# backported from stdlib `ast`
assert sys.version_info < (3, 8) or sys.version_info >= (3, 14)
_const_node_type_names = {
bool: "NameConstant", # should be before int
type(None): "NameConstant",
int: "Num",
float: "Num",
complex: "Num",
str: "Str",
bytes: "Bytes",
type(...): "Ellipsis",
}
if sys.version_info >= (3, 8):
# Python 3.14+ removed _const_node_type_names; all constant literals
# are represented as ast.Constant nodes.
_const_node_type_names = {
bool: "Constant", # should be before int
type(None): "Constant",
int: "Constant",
float: "Constant",
complex: "Constant",
str: "Constant",
bytes: "Constant",
type(...): "Constant",
}
else:
# Python < 3.8 used separate node types (Num, Str, Bytes, etc.)
_const_node_type_names = {
bool: "NameConstant", # should be before int
type(None): "NameConstant",
int: "Num",
float: "Num",
complex: "Num",
str: "Str",
bytes: "Bytes",
type(...): "Ellipsis",
}


def parse(source, filename="<string>", *args, **kwargs): # type: ignore
Expand Down
22 changes: 22 additions & 0 deletions ropetest/refactor/patchedasttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,28 @@ def test_match_node_with_match_mapping_match_as(self):
])


class ConstNodeTypeNamesTest(unittest.TestCase):
def _name(self, value):
import ast as stdlib_ast
node = stdlib_ast.Constant(value=value)
return ast.get_const_subtype_name(node)

@unittest.skipIf(sys.version_info < (3, 8), "get_const_subtype_name requires 3.8+")
def test_int_name(self):
expected = "Constant" if sys.version_info >= (3, 14) else "Num"
self.assertEqual(self._name(1), expected)

@unittest.skipIf(sys.version_info < (3, 8), "get_const_subtype_name requires 3.8+")
def test_str_name(self):
expected = "Constant" if sys.version_info >= (3, 14) else "Str"
self.assertEqual(self._name("hello"), expected)

@unittest.skipIf(sys.version_info < (3, 8), "get_const_subtype_name requires 3.8+")
def test_none_name(self):
expected = "Constant" if sys.version_info >= (3, 14) else "NameConstant"
self.assertEqual(self._name(None), expected)


class _ResultChecker:
def __init__(self, test_case, ast):
self.test_case = test_case
Expand Down
Loading