python / python/cpython

Confusing syntax error for missing comma after import-as target

Open
#146,492 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 3.15 interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-146493

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two import-as examples in the Python REPL, then inspect the two matching rules in Grammar/python.gram around lines 1435 and 1439. Compare the resulting diagnostic with the intended behavior described in the issue; done means the missing-comma case receives a clearer syntax error. A linked PR is already noted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.