mirror of
https://github.com/davidhalter/parso.git
synced 2026-03-05 22:54:35 +08:00
Deal with nested comprehension
e.g. `[i for i, j in range(5) for k in range (10) if True or (i := 1)]`
This commit is contained in:
@@ -1070,14 +1070,26 @@ class _ComprehensionRule(SyntaxRule):
|
|||||||
# (comp_for | (',' (test | star_expr)) * [','])))
|
# (comp_for | (',' (test | star_expr)) * [','])))
|
||||||
|
|
||||||
def is_issue(self, node):
|
def is_issue(self, node):
|
||||||
exprlist = None
|
exprlist = list()
|
||||||
|
namedexpr_list = list()
|
||||||
|
|
||||||
|
def process_comp(comp_for):
|
||||||
|
if comp_for.type in _COMP_FOR_TYPES:
|
||||||
|
if comp_for.type == 'sync_comp_for':
|
||||||
|
comp = comp_for
|
||||||
|
elif comp_for.type == 'comp_for':
|
||||||
|
comp = comp_for.children[1]
|
||||||
|
exprlist.extend(_get_for_stmt_definition_exprs(comp))
|
||||||
|
|
||||||
|
if len(comp.children) > 4:
|
||||||
|
comp_iter = comp.children[4]
|
||||||
|
process_comp(comp_iter)
|
||||||
|
else:
|
||||||
|
# skip assignment expressions in comp_for
|
||||||
|
namedexpr_list.extend(_get_namedexpr(comp_for))
|
||||||
|
|
||||||
for child in node.children:
|
for child in node.children:
|
||||||
if child.type in _COMP_FOR_TYPES:
|
process_comp(child)
|
||||||
if child.type == 'sync_comp_for':
|
|
||||||
exprlist = _get_for_stmt_definition_exprs(child)
|
|
||||||
elif child.type == 'comp_for':
|
|
||||||
exprlist = _get_for_stmt_definition_exprs(child.children[1])
|
|
||||||
break
|
|
||||||
|
|
||||||
# not a comprehension
|
# not a comprehension
|
||||||
if exprlist is None:
|
if exprlist is None:
|
||||||
@@ -1087,7 +1099,6 @@ class _ComprehensionRule(SyntaxRule):
|
|||||||
in_class = self._normalizer.context.node.type == 'classdef'
|
in_class = self._normalizer.context.node.type == 'classdef'
|
||||||
|
|
||||||
namelist = [expr.value for expr in exprlist]
|
namelist = [expr.value for expr in exprlist]
|
||||||
namedexpr_list = _get_namedexpr(node)
|
|
||||||
for expr in namedexpr_list:
|
for expr in namedexpr_list:
|
||||||
if in_class:
|
if in_class:
|
||||||
# class Example:
|
# class Example:
|
||||||
|
|||||||
Reference in New Issue
Block a user