Confusing syntax error for missing comma after import-as target
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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