Merge pull request #119 from isidentical/check-all-args

Check all arguments for unparenthesized generator expressions
This commit is contained in:
Dave Halter
2020-05-23 23:18:00 +02:00
committed by GitHub
2 changed files with 66 additions and 53 deletions
+3 -6
View File
@@ -779,12 +779,6 @@ class _ArglistRule(SyntaxRule):
return "Generator expression must be parenthesized" return "Generator expression must be parenthesized"
def is_issue(self, node): def is_issue(self, node):
first_arg = node.children[0]
if first_arg.type == 'argument' \
and first_arg.children[1].type in _COMP_FOR_TYPES:
# e.g. foo(x for x in [], b)
return len(node.children) >= 2
else:
arg_set = set() arg_set = set()
kw_only = False kw_only = False
kw_unpacking_only = False kw_unpacking_only = False
@@ -806,6 +800,9 @@ class _ArglistRule(SyntaxRule):
if argument.type == 'argument': if argument.type == 'argument':
first = argument.children[0] first = argument.children[0]
if argument.children[1].type in _COMP_FOR_TYPES and len(node.children) >= 2:
# a(a, b for b in c)
return True
if first in ('*', '**'): if first in ('*', '**'):
if first == '*': if first == '*':
if kw_unpacking_only: if kw_unpacking_only:
+16
View File
@@ -375,3 +375,19 @@ def test_repeated_kwarg():
_get_error_list("f(q=1, q=2)", version="3.9")[0].message _get_error_list("f(q=1, q=2)", version="3.9")[0].message
== "SyntaxError: keyword argument repeated: q" == "SyntaxError: keyword argument repeated: q"
) )
@pytest.mark.parametrize(
('source', 'no_errors'), [
('a(a for a in b,)', False),
('a(a for a in b, a)', False),
('a(a, a for a in b)', False),
('a(a, b, a for a in b, c, d)', False),
('a(a for a in b)', True),
('a((a for a in b), c)', True),
('a(c, (a for a in b))', True),
('a(a, b, (a for a in b), c, d)', True),
]
)
def test_unparenthesized_genexp(source, no_errors):
assert bool(_get_error_list(source)) ^ no_errors