Skip to content

Commit afc2751

Browse files
Reflect AST changes in Python 3.8.
* Num, Str, Bytes, Ellipsis and NameConstant are replaced with Constant. (https://bugs.python.org/issue32892) * Index is replaced with its value, ExtSlice is replaced with Tuple. (https://bugs.python.org/issue34822)
1 parent 5a85ae6 commit afc2751

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

astroid/as_string.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def visit_comprehension(self, node):
188188

189189
def visit_const(self, node):
190190
"""return an astroid.Const node as string"""
191+
if node.value is Ellipsis:
192+
return '...'
191193
return repr(node.value)
192194

193195
def visit_continue(self, node):
@@ -273,7 +275,7 @@ def visit_exec(self, node):
273275

274276
def visit_extslice(self, node):
275277
"""return an astroid.ExtSlice node as string"""
276-
return ','.join([dim.accept(self) for dim in node.dims])
278+
return ', '.join([dim.accept(self) for dim in node.dims])
277279

278280
def visit_for(self, node):
279281
"""return an astroid.For node as string"""
@@ -430,8 +432,12 @@ def visit_slice(self, node):
430432

431433
def visit_subscript(self, node):
432434
"""return an astroid.Subscript node as string"""
433-
return '%s[%s]' % (self._precedence_parens(node, node.value),
434-
node.slice.accept(self))
435+
idx = node.slice.accept(self)
436+
if node.slice.__class__.__name__.lower() == 'tuple' and node.slice.elts:
437+
# Remove parenthesis in tuple and extended slice.
438+
# a[(::1, 1:)] is not valid syntax.
439+
idx = idx[1:-1]
440+
return '%s[%s]' % (self._precedence_parens(node, node.value), idx)
435441

436442
def visit_tryexcept(self, node):
437443
"""return an astroid.TryExcept node as string"""

astroid/rebuilder.py

+11
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def visit_expr(self, node, parent):
479479
newnode.postinit(self.visit(node.value, newnode))
480480
return newnode
481481

482+
# Not used in Python 3.8+.
482483
def visit_ellipsis(self, node, parent):
483484
"""visit an Ellipsis node by returning a fresh instance of it"""
484485
return nodes.Ellipsis(getattr(node, 'lineno', None),
@@ -509,6 +510,7 @@ def visit_exec(self, node, parent):
509510
_visit_or_none(node, 'locals', self, newnode))
510511
return newnode
511512

513+
# Not used in Python 3.8+.
512514
def visit_extslice(self, node, parent):
513515
"""visit an ExtSlice node by returning a fresh instance of it"""
514516
newnode = nodes.ExtSlice(parent=parent)
@@ -641,6 +643,7 @@ def visit_import(self, node, parent):
641643
parent.set_local(name.split('.')[0], newnode)
642644
return newnode
643645

646+
# Not used in Python 3.8+.
644647
def visit_index(self, node, parent):
645648
"""visit a Index node by returning a fresh instance of it"""
646649
newnode = nodes.Index(parent=parent)
@@ -702,12 +705,19 @@ def visit_name(self, node, parent):
702705
self._save_assignment(newnode)
703706
return newnode
704707

708+
def visit_constant(self, node, parent):
709+
"""visit a Constant node by returning a fresh instance of Const"""
710+
return nodes.Const(node.value, getattr(node, 'lineno', None),
711+
getattr(node, 'col_offset', None), parent)
712+
713+
# Not used in Python 3.8+.
705714
def visit_str(self, node, parent):
706715
"""visit a String/Bytes node by returning a fresh instance of Const"""
707716
return nodes.Const(node.s, getattr(node, 'lineno', None),
708717
getattr(node, 'col_offset', None), parent)
709718
visit_bytes = visit_str
710719

720+
# Not used in Python 3.8+.
711721
def visit_num(self, node, parent):
712722
"""visit a Num node by returning a fresh instance of Const"""
713723
return nodes.Const(node.n, getattr(node, 'lineno', None),
@@ -854,6 +864,7 @@ def visit_arg(self, node, parent):
854864
"""visit an arg node by returning a fresh AssName instance"""
855865
return self.visit_assignname(node, parent, node.arg)
856866

867+
# Not used in Python 3.8+.
857868
def visit_nameconstant(self, node, parent):
858869
# in Python 3.4 we have NameConstant for True / False / None
859870
return nodes.Const(node.value, getattr(node, 'lineno', None),

astroid/tests/unittest_nodes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def test_ellipsis(self):
158158
self.assertEqual(ast.as_string(), 'a[...]')
159159

160160
def test_slices(self):
161-
for code in ('a[0]', 'a[1:3]', 'a[:-1:step]', 'a[:,newaxis]',
162-
'a[newaxis,:]', 'del L[::2]', 'del A[1]', 'del Br[:]'):
161+
for code in ('a[0]', 'a[1:3]', 'a[:-1:step]', 'a[:, newaxis]',
162+
'a[newaxis, :]', 'del L[::2]', 'del A[1]', 'del Br[:]'):
163163
ast = abuilder.string_build(code).body[0]
164164
self.assertEqual(ast.as_string(), code)
165165

@@ -170,7 +170,7 @@ def test_slice_and_subscripts(self):
170170
bord[2:]
171171
del av[d::f], a[df:]
172172
a[:1] = bord[2:]
173-
del SRC[::1,newaxis,1:]
173+
del SRC[::1, newaxis, 1:]
174174
tous[vals] = 1010
175175
del thousand[key]
176176
del a[::2], a[:-1:step]
@@ -546,7 +546,7 @@ def test_as_string(self):
546546
print()
547547
test: int = 5
548548
test2: str
549-
test3: List[Dict[(str, str)]] = []
549+
test3: List[Dict[str, str]] = []
550550
""")
551551
ast = abuilder.string_build(code)
552552
self.assertEqual(ast.as_string().strip(), code.strip())

0 commit comments

Comments
 (0)