From c53321a4405d0c45c14b75d93940b79c61029b34 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 24 Jul 2020 00:30:15 +0200 Subject: [PATCH] Comprehensions are not valid as class params, fixes #122 --- parso/python/errors.py | 11 ++++++++++- test/failing_examples.py | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/parso/python/errors.py b/parso/python/errors.py index 36b9d54..54a6641 100644 --- a/parso/python/errors.py +++ b/parso/python/errors.py @@ -209,6 +209,11 @@ def _get_for_stmt_definition_exprs(for_stmt): exprlist = for_stmt.children[1] return list(_iter_definition_exprs_from_lists(exprlist)) + +def _is_argument_comprehension(argument): + return argument.children[1].type in _COMP_FOR_TYPES + + def _any_fstring_error(version, node): if version < (3, 9) or node is None: return False @@ -862,6 +867,9 @@ class _ArgumentRule(SyntaxRule): message = 'expression cannot contain assignment, perhaps you meant "=="?' self.add_issue(first, message=message) + if _is_argument_comprehension(node) and node.parent.type == 'classdef': + self.add_issue(node, message='invalid syntax') + @ErrorFinder.register_rule(type='nonlocal_stmt') class _NonlocalModuleLevelRule(SyntaxRule): @@ -902,9 +910,10 @@ class _ArglistRule(SyntaxRule): if argument.type == 'argument': first = argument.children[0] - if argument.children[1].type in _COMP_FOR_TYPES and len(node.children) >= 2: + if _is_argument_comprehension(argument) and len(node.children) >= 2: # a(a, b for b in c) return True + if first in ('*', '**'): if first == '*': if kw_unpacking_only: diff --git a/test/failing_examples.py b/test/failing_examples.py index c341261..d2078b1 100644 --- a/test/failing_examples.py +++ b/test/failing_examples.py @@ -76,6 +76,7 @@ FAILING_EXAMPLES = [ '(yield) += 1', '(yield from x) += 1', '(x if x else y) += 1', + 'class X(base for base in bases): pass', 'a() += 1', 'a + b += 1', '+a += 1',