8000 gh-118082: Improve `import` without names syntax error message (#118083) · python/cpython@de1f686 · GitHub
[go: up one dir, main page]

Skip to content

Commit de1f686

Browse files
authored
gh-118082: Improve import without names syntax error message (#118083)
1 parent eb927e9 commit de1f686

File tree

4 files changed

+219
-149
lines changed

4 files changed

+219
-149
lines changed

Grammar/python.gram

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,10 +1299,14 @@ invalid_group:
12991299
invalid_import:
13001300
| a='import' ','.dotted_name+ 'from' dotted_name {
13011301
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
1302+
| 'import' token=NEWLINE {
1303+
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
13021304

13031305
invalid_import_from_targets:
13041306
| import_from_as_names ',' NEWLINE {
13051307
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
1308+
| token=NEWLINE {
1309+
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
13061310

13071311
invalid_compound_stmt:
13081312
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }

Lib/test/test_syntax.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,18 @@
16991699
Traceback (most recent call last):
17001700
SyntaxError: invalid syntax
17011701
1702+
>>> from i import
1703+
Traceback (most recent call last):
1704+
SyntaxError: Expected one or more names after 'import'
1705+
1706+
>>> from .. import
1707+
Traceback (most recent call last):
1708+
SyntaxError: Expected one or more names after 'import'
1709+
1710+
>>> import
1711+
Traceback (most recent call last):
1712+
SyntaxError: Expected one or more names after 'import'
1713+
17021714
>>> (): int
17031715
Traceback (most recent call last):
17041716
SyntaxError: only single target (not tuple) can be annotated
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve :exc:`SyntaxError` message for imports without names, like in
2+
``from x import`` and ``import`` cases. It now points
3+
out to users that :keyword:`import` expects at least one name after it.

0 commit comments

Comments
 (0)
0