Fix: Add more cases for named expression errors, see #89, #90

This commit is contained in:
Dave Halter
2019-12-15 00:04:38 +01:00
parent 3c475b1e63
commit 753e1999fe
2 changed files with 27 additions and 12 deletions
+23 -12
View File
@@ -94,19 +94,32 @@ def _is_future_import_first(import_from):
def _iter_definition_exprs_from_lists(exprlist): def _iter_definition_exprs_from_lists(exprlist):
for child in exprlist.children[::2]: def check_expr(child):
if child.type == 'atom' and child.children[0] in ('(', '['): if child.type == 'atom':
testlist_comp = child.children[0] if child.children[0] == '(':
if testlist_comp.type == 'testlist_comp': testlist_comp = child.children[1]
for expr in _iter_definition_exprs_from_lists(testlist_comp): if testlist_comp.type == 'testlist_comp':
yield expr for expr in _iter_definition_exprs_from_lists(testlist_comp):
continue yield expr
return
else:
# It's a paren that doesn't do anything, like 1 + (1)
for c in check_expr(testlist_comp):
yield c
return
elif child.children[0] == '[': elif child.children[0] == '[':
yield testlist_comp yield testlist_comp
continue return
yield child yield child
if exprlist.type in _STAR_EXPR_PARENTS:
for child in exprlist.children[::2]:
for c in check_expr(child): # Python 2 sucks
yield c
else:
for c in check_expr(exprlist): # Python 2 sucks
yield c
def _get_expr_stmt_definition_exprs(expr_stmt): def _get_expr_stmt_definition_exprs(expr_stmt):
exprs = [] exprs = []
@@ -120,8 +133,6 @@ def _get_expr_stmt_definition_exprs(expr_stmt):
def _get_for_stmt_definition_exprs(for_stmt): def _get_for_stmt_definition_exprs(for_stmt):
exprlist = for_stmt.children[1] exprlist = for_stmt.children[1]
if exprlist.type != 'exprlist':
return [exprlist]
return list(_iter_definition_exprs_from_lists(exprlist)) return list(_iter_definition_exprs_from_lists(exprlist))
@@ -1072,7 +1083,7 @@ class _NamedExprRule(_CheckAssignmentRule):
'cannot be used in a class body' 'cannot be used in a class body'
self.add_issue(namedexpr_test, message=message) self.add_issue(namedexpr_test, message=message)
namelist = [expr.value for expr in exprlist] namelist = [expr.value for expr in exprlist if expr.type == 'name']
if first.type == 'name' and first.value in namelist: if first.type == 'name' and first.value in namelist:
# [i := 0 for i, j in range(5)] # [i := 0 for i, j in range(5)]
# [[(i := i) for j in range(5)] for i in range(5)] # [[(i := i) for j in range(5)] for i in range(5)]
+4
View File
@@ -332,8 +332,12 @@ if sys.version_info[:2] >= (3, 8):
'(a(i) := x)', '(a(i) := x)',
# Case 4 # Case 4
'(a.b := c)', '(a.b := c)',
'[(i.i:= 0) for ((i), j) in range(5)]',
# Case 5 # Case 5
'[i:= 0 for i, j in range(5)]', '[i:= 0 for i, j in range(5)]',
'[(i:= 0) for ((i), j) in range(5)]',
'[(i:= 0) for ((i), j), in range(5)]',
'[(i:= 0) for ((i), j.i), in range(5)]',
'[[(i:= i) for j in range(5)] for i in range(5)]', '[[(i:= i) for j in range(5)] for i in range(5)]',
'[i for i, j in range(5) if True or (i:= 1)]', '[i for i, j in range(5) if True or (i:= 1)]',
'[False and (i:= 0) for i, j in range(5)]', '[False and (i:= 0) for i, j in range(5)]',