Skip to content

Missing syntax error for unparenthesized generator expression in single-argument call #17867

@ntBre

Description

@ntBre

generator expression must be parenthesized

Regardless of whether there is a trailing comma or not, the exact same AST is generated by ruff for the code (only the ranges are different).
Given this, how do we detect the trailing comma during the semantic syntax error phase?
Should this rather be detected during parsing?
For example, the ast module does not give an AST at all for this:

>> import ast; ast.parse("max(i for i in range(3),)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    max(i for i in range(3),)
        ^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized

Originally posted by @abhijeetbodas2001 in #17412

This is closely related to #12445, which handled the multiple-argument case, but CPython also raises an error for a single argument.

Metadata

Metadata

Labels

bugSomething isn't workingparserRelated to the parser

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions