Skip to content

Confusing syntax error for missing comma after import-as target #146492

@brianschubert

Description

@brianschubert

Bug report

Bug description:

Right now we have:

>>> import a as a  b
  File "<python-input-0>", line 1
    import a as a  b
                ^
SyntaxError: cannot use name as import target

>>> from x import a as a  b
  File "<python-input-1>", line 1
    from x import a as a  b
                       ^
SyntaxError: cannot use name as import target

This happens since these cases match these two rules.

One possible fix would be to add NAME to the list of not-matched tokens so that the current rules don't match this case.

However, I have an idea for a slightly nicer error. PR forthcoming.

cc @pablogsal @lysnikolaou @sobolevn

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions