Deal better with some error nodes

This commit is contained in:
Dave Halter
2019-07-08 14:26:11 -07:00
parent ca7658cab7
commit 97bf83aa03
2 changed files with 17 additions and 2 deletions

View File

@@ -185,6 +185,7 @@ class CallDetails(object):
else:
return None
print(args)
is_kwarg = False
for i, (star_count, key_start, had_equal) in enumerate(args):
is_kwarg |= had_equal | (star_count == 2)
@@ -225,6 +226,8 @@ class CallDetails(object):
def _iter_arguments(nodes, position):
def remove_after_pos(name):
if name.type != 'name':
return None
return name.value[:position[1] - name.start_pos[1]]
# Returns Generator[Tuple[star_count, Optional[key_start: str], had_equal]]
@@ -235,6 +238,7 @@ def _iter_arguments(nodes, position):
return
previous_node_yielded = False
print(nodes_before)
for i, node in enumerate(nodes_before):
if node.type == 'argument':
previous_node_yielded = True
@@ -246,7 +250,7 @@ def _iter_arguments(nodes, position):
else:
yield 0, remove_after_pos(first), True
elif first in ('*', '**'):
yield len(first), remove_after_pos(second), ''
yield len(first.value), remove_after_pos(second), False
else:
# Must be a Comprehension
first_leaf = node.get_first_leaf()
@@ -254,6 +258,9 @@ def _iter_arguments(nodes, position):
yield 0, remove_after_pos(first_leaf), False
else:
yield 0, None, False
elif node.type in ('testlist', 'testlist_star_expr'): # testlist is Python 2
for n in node.children[::2]:
yield 0, remove_after_pos(n), False
elif isinstance(node, tree.PythonLeaf) and node.value == ',':
if not previous_node_yielded:
yield 0, '', False
@@ -339,7 +346,7 @@ def get_call_signature_details(module, position):
)
if result is not None:
return result
additional_children.append(n)
additional_children.insert(0, n)
if node.type == 'trailer' and node.children[0] == '(':
leaf = node.get_previous_leaf()

View File

@@ -451,6 +451,8 @@ code4 = 'def i(u, /, v, *args, x=1, y, **kwargs): pass'
(code3, 'h(a,b,args=', None),
(code3, 'h(u,v=', 1),
(code3, 'h(u=', None),
#(code3, 'h(u,*xxx', 1),
#(code3, 'h(u,*[]', 1),
# *args, **kwargs
(code4, 'i(a,b,c,d', 2),
@@ -464,11 +466,17 @@ code4 = 'def i(u, /, v, *args, x=1, y, **kwargs): pass'
(code4, 'i(a,b,c,d=5,y=4,x=3,', 5),
(code4, 'i(a,b,c,d=4,', 3),
(code4, 'i(a,b,c,x=1,d=,', 4),
# Error nodes
(code4, 'i(1, [a,b', 1),
(code4, 'i(1, [a,b=,', 2),
(code4, 'i(1, [a!b,', 2),
]
)
def test_signature_index(skip_pre_python38, Script, code, call, expected_index):
sig, = Script(code + '\n' + call).call_signatures()
index = sig.index
print(call)
assert expected_index == index