Fix: Catch some additional cases named expr errors, see #89, #90

This commit is contained in:
Dave Halter
2019-12-14 23:31:43 +01:00
parent dbba1959f7
commit 5f04dad9ab
2 changed files with 10 additions and 23 deletions

View File

@@ -939,6 +939,10 @@ class _CheckAssignmentRule(SyntaxRule):
assert trailer.type == 'trailer' assert trailer.type == 'trailer'
if trailer.children[0] == '(': if trailer.children[0] == '(':
error = 'function call' error = 'function call'
elif is_namedexpr and trailer.children[0] == '[':
error = 'subscript'
elif is_namedexpr and trailer.children[0] == '.':
error = 'attribute'
elif type_ in ('testlist_star_expr', 'exprlist', 'testlist'): elif type_ in ('testlist_star_expr', 'exprlist', 'testlist'):
for child in node.children[::2]: for child in node.children[::2]:
self._check_assignment(child, is_deletion, is_namedexpr) self._check_assignment(child, is_deletion, is_namedexpr)
@@ -1078,26 +1082,4 @@ class _NamedExprRule(_CheckAssignmentRule):
'comprehension iteration variable %r' % first.value 'comprehension iteration variable %r' % first.value
self.add_issue(namedexpr_test, message=message) self.add_issue(namedexpr_test, message=message)
if first.type == 'lambdef': self._check_assignment(first, is_namedexpr=True)
# (lambda: x := 1)
self.add_issue(namedexpr_test, message='cannot use named assignment with lambda')
elif first.type == 'atom_expr':
for child in first.children:
if child.type != 'trailer':
continue
first_child = child.children[0]
if first_child.type == 'operator':
if first_child.value == '[':
# (a[i] := x)
message = 'cannot use named assignment with subscript'
self.add_issue(namedexpr_test, message=message)
elif first_child.value == '.':
# (a.b := c)
message = 'cannot use named assignment with attribute'
self.add_issue(namedexpr_test, message=message)
elif first_child.value == '(':
# (a[i] := x)
message = 'cannot use named assignment with function call'
self.add_issue(namedexpr_test, message=message)
else:
self._check_assignment(first, is_namedexpr=True)

View File

@@ -325,8 +325,10 @@ if sys.version_info[:2] >= (3, 8):
FAILING_EXAMPLES += [ FAILING_EXAMPLES += [
# Case 2 # Case 2
'(lambda: x := 1)', '(lambda: x := 1)',
'((lambda: x) := 1)',
# Case 3 # Case 3
'(a[i] := x)', '(a[i] := x)',
'((a[i]) := x)',
'(a(i) := x)', '(a(i) := x)',
# Case 4 # Case 4
'(a.b := c)', '(a.b := c)',
@@ -341,4 +343,7 @@ if sys.version_info[:2] >= (3, 8):
'[i+1 for i in (lambda: (j:= range(5)))()]', '[i+1 for i in (lambda: (j:= range(5)))()]',
# Case 7 # Case 7
'class Example:\n [(j := i) for i in range(5)]', 'class Example:\n [(j := i) for i in range(5)]',
# Not in that issue
'(await a := x)',
'((await a) := x)',
] ]