Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit f99a03e

Browse files
author
Juanjo Alvarez
committed
Handle NoneTypes that can be found by the main visit() method
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 08a58f2 commit f99a03e

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

native/python_package/python_driver/astimprove.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ def _create_astmissing_lines(self):
189189
self._current_line = len(lines)
190190
return lines
191191

192-
def add_noops(self, node, visit_dict, root):
193-
if not isinstance(visit_dict, dict):
194-
return visit_dict
192+
def add_noops(self, node, root):
193+
if not isinstance(node, dict):
194+
return node
195195

196196
def _create_nooplines_list(startline, noops_previous):
197197
nooplines = []
@@ -208,10 +208,9 @@ def _create_nooplines_list(startline, noops_previous):
208208

209209
# Add all the noop (whitespace and comments) lines between the
210210
# last node and this one
211-
noops_previous, startline, endline, endcol =\
212-
self.previous_nooplines(node)
211+
noops_previous, startline, endline, endcol = self.previous_nooplines(node)
213212
if noops_previous:
214-
visit_dict['noops_previous'] = {
213+
node['noops_previous'] = {
215214
"ast_type": "PreviousNoops",
216215
"lineno": startline,
217216
"col_offset": 1,
@@ -225,7 +224,7 @@ def _create_nooplines_list(startline, noops_previous):
225224
noops_sameline = self.sameline_remainder_noops(node)
226225
joined_sameline = ''.join([x['value'] for x in noops_sameline])
227226
if noops_sameline:
228-
visit_dict['noops_sameline'] = {
227+
node['noops_sameline'] = {
229228
"ast_type": "SameLineNoops",
230229
"lineno": node.get("lineno", 0),
231230
"col_offset": noops_sameline[0]["colstart"],
@@ -239,7 +238,7 @@ def _create_nooplines_list(startline, noops_previous):
239238
noops_remainder, startline, endline, endcol =\
240239
self.remainder_noops()
241240
if noops_remainder:
242-
visit_dict['noops_remainder'] = {
241+
node['noops_remainder'] = {
243242
"ast_type": "RemainderNoops",
244243
"lineno": startline,
245244
"col_offset": 1,
@@ -423,14 +422,16 @@ def visit(self, node, root=False):
423422
# the ctx property always has a "Load"/"Store"/etc dictionary that
424423
# can be perfectly converted to a string value since they don't
425424
# hold anything more than the name
426-
node_type = node["ast_type"] if isinstance(node, dict) else node.__class__.__name__
427-
ctx = node.get("ctx")
428-
if ctx:
429-
node["ctx"] = ctx["ast_type"]
425+
if isinstance(node, dict):
426+
node_type = node["ast_type"]
427+
if "ctx" in node:
428+
node["ctx"] = node["ctx"]["ast_type"]
429+
else:
430+
node_type = node.__class__.__name__
430431

431432
meth = getattr(self, "visit_" + node_type, self.visit_other)
432433
visit_result = meth(node)
433-
self.noops_sync.add_noops(node, visit_result, root)
434+
self.noops_sync.add_noops(node, root)
434435
self.pos_sync.sync_node_pos(visit_result)
435436

436437
if not self.codestr:
@@ -489,8 +490,7 @@ def visit_NameConstant(self, node):
489490
node.update({"LiteralValue": "True" if node["value"] else "False",
490491
"ast_type": "BoolLiteral"})
491492
elif repr_val == "None":
492-
node.update({"LiteralValue": "None",
493-
"ast_type": "NoneLiteral"})
493+
node = self.visit_NoneType(node)
494494
else:
495495
node["ast_type"] = "NameConstant"
496496
return node
@@ -502,6 +502,14 @@ def visit_Num(self, node):
502502
"imag": node["n"].imag}})
503503
return node
504504

505+
def visit_NoneType(self, node):
506+
if node:
507+
ret = node
508+
509+
ret.update({"LiteralValue": "None",
510+
"ast_type": "NoneLiteral"})
511+
return ret
512+
505513
def visit_other(self, node):
506514
for field in node.get("_fields", []):
507515
meth = getattr(self, "visit_" + node["ast_type"], self.visit_other_field)

0 commit comments

Comments
 (0)