mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-18 14:30:04 +08:00
Some minor refactorings for #90
- search_ancestor is now used instead of using node = node.parent - Some lines were too long
This commit is contained in:
+21
-17
@@ -125,7 +125,6 @@ def _get_for_stmt_definition_exprs(for_stmt):
|
|||||||
return list(_iter_definition_exprs_from_lists(exprlist))
|
return list(_iter_definition_exprs_from_lists(exprlist))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class _Context(object):
|
class _Context(object):
|
||||||
def __init__(self, node, add_syntax_error, parent_context=None):
|
def __init__(self, node, add_syntax_error, parent_context=None):
|
||||||
self.node = node
|
self.node = node
|
||||||
@@ -1035,7 +1034,8 @@ class _NamedExprRule(_CheckAssignmentRule):
|
|||||||
# [i+1 for i in (i := range(5))]
|
# [i+1 for i in (i := range(5))]
|
||||||
# [i+1 for i in (j := range(5))]
|
# [i+1 for i in (j := range(5))]
|
||||||
# [i+1 for i in (lambda: (j := range(5)))()]
|
# [i+1 for i in (lambda: (j := range(5)))()]
|
||||||
self.add_issue(namedexpr_test, message='assignment expression cannot be used in a comprehension iterable expression')
|
message = 'assignment expression cannot be used in a comprehension iterable expression'
|
||||||
|
self.add_issue(namedexpr_test, message=message)
|
||||||
|
|
||||||
# defined names
|
# defined names
|
||||||
exprlist = list()
|
exprlist = list()
|
||||||
@@ -1048,24 +1048,25 @@ class _NamedExprRule(_CheckAssignmentRule):
|
|||||||
exprlist.extend(_get_for_stmt_definition_exprs(comp))
|
exprlist.extend(_get_for_stmt_definition_exprs(comp))
|
||||||
|
|
||||||
def search_all_comp_ancestors(node):
|
def search_all_comp_ancestors(node):
|
||||||
ancestors = list()
|
has_ancestors = False
|
||||||
while True:
|
while True:
|
||||||
node = node.parent
|
node = search_ancestor(node, 'testlist_comp', 'dictorsetmaker')
|
||||||
if node is None:
|
if node is None:
|
||||||
return ancestors
|
break
|
||||||
if node.type in ['testlist_comp', 'dictorsetmaker']:
|
for child in node.children:
|
||||||
for child in node.children:
|
if child.type in _COMP_FOR_TYPES:
|
||||||
if child.type in _COMP_FOR_TYPES:
|
process_comp_for(child)
|
||||||
process_comp_for(child)
|
has_ancestors = True
|
||||||
ancestors.append(node)
|
break
|
||||||
break
|
return has_ancestors
|
||||||
|
|
||||||
# check assignment expressions in comprehensions
|
# check assignment expressions in comprehensions
|
||||||
search_all = search_all_comp_ancestors(namedexpr_test)
|
search_all = search_all_comp_ancestors(namedexpr_test)
|
||||||
if search_all:
|
if search_all:
|
||||||
if self._normalizer.context.node.type == 'classdef':
|
if self._normalizer.context.node.type == 'classdef':
|
||||||
self.add_issue(
|
message = 'assignment expression within a comprehension ' \
|
||||||
namedexpr_test, message='assignment expression within a comprehension cannot be used in a class body')
|
'cannot be used in a class body'
|
||||||
|
self.add_issue(namedexpr_test, message=message)
|
||||||
|
|
||||||
namelist = [expr.value for expr in exprlist]
|
namelist = [expr.value for expr in exprlist]
|
||||||
if first.type == 'name' and first.value in namelist:
|
if first.type == 'name' and first.value in namelist:
|
||||||
@@ -1073,8 +1074,9 @@ class _NamedExprRule(_CheckAssignmentRule):
|
|||||||
# [[(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)]
|
||||||
self.add_issue(
|
message = 'assignment expression cannot rebind ' \
|
||||||
namedexpr_test, message='assignment expression cannot rebind comprehension iteration variable %r' % first.value)
|
'comprehension iteration variable %r' % first.value
|
||||||
|
self.add_issue(namedexpr_test, message=message)
|
||||||
|
|
||||||
if first.type == 'lambdef':
|
if first.type == 'lambdef':
|
||||||
# (lambda: x := 1)
|
# (lambda: x := 1)
|
||||||
@@ -1087,9 +1089,11 @@ class _NamedExprRule(_CheckAssignmentRule):
|
|||||||
if first_child.type == 'operator':
|
if first_child.type == 'operator':
|
||||||
if first_child.value == '[':
|
if first_child.value == '[':
|
||||||
# (a[i] := x)
|
# (a[i] := x)
|
||||||
self.add_issue(namedexpr_test, message='cannot use named assignment with subscript')
|
message = 'cannot use named assignment with subscript'
|
||||||
|
self.add_issue(namedexpr_test, message=message)
|
||||||
elif first_child.value == '.':
|
elif first_child.value == '.':
|
||||||
# (a.b := c)
|
# (a.b := c)
|
||||||
self.add_issue(namedexpr_test, message='cannot use named assignment with attribute')
|
message = 'cannot use named assignment with attribute'
|
||||||
|
self.add_issue(namedexpr_test, message=message)
|
||||||
else:
|
else:
|
||||||
self._check_assignment(first, is_namedexpr=True)
|
self._check_assignment(first, is_namedexpr=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user