Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 395d428

Browse files
authored
gh-98931: Improve error message when the user types 'import x from y' instead of 'from y import x' (#98932)
1 parent 0e15c31 commit 395d428

File tree

5 files changed

+503
-392
lines changed

5 files changed

+503
-392
lines changed

Doc/whatsnew/3.12.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ Important deprecations, removals or restrictions:
7070

7171
* :pep:`623`, Remove wstr from Unicode
7272

73+
Improved Error Messages
74+
=======================
75+
76+
* Improve the :exc:`SyntaxError` error message when the user types ``import x
77+
from y`` instead of ``from y import x``. Contributed by Pablo Galindo in :gh:`98931`.
78+
79+
>>> import a.y.z from b.y.z
80+
Traceback (most recent call last):
81+
File "<stdin>", line 1
82+
import a.y.z from b.y.z
83+
^^^^^^^^^^^^^^^^^^^^^^^
84+
SyntaxError: Did you mean to use 'from ... import ...' instead?
7385

7486
New Features
7587
============

Grammar/python.gram

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
194194

195195
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
196196

197-
import_stmt[stmt_ty]: import_name | import_from
197+
import_stmt[stmt_ty]:
198+
| invalid_import
199+
| import_name
200+
| import_from
198201

199202
# Import statements
200203
# -----------------
@@ -1230,6 +1233,10 @@ invalid_group:
12301233
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
12311234
| '(' a='**' expression ')' {
12321235
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
1236+
invalid_import:
1237+
| a='import' dotted_name 'from' dotted_name {
1238+
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
1239+
12331240
invalid_import_from_targets:
12341241
| import_from_as_names ',' NEWLINE {
12351242
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

Lib/test/test_syntax.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,22 @@
15841584
Traceback (most recent call last):
15851585
SyntaxError: trailing comma not allowed without surrounding parentheses
15861586
1587+
>>> import a from b
1588+
Traceback (most recent call last):
1589+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1590+
1591+
>>> import a.y.z from b.y.z
1592+
Traceback (most recent call last):
1593+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1594+
1595+
>>> import a from b as bar
1596+
Traceback (most recent call last):
1597+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1598+
1599+
>>> import a.y.z from b.y.z as bar
1600+
Traceback (most recent call last):
1601+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1602+
15871603
# Check that we dont raise the "trailing comma" error if there is more
15881604
# input to the left of the valid part that we parsed.
15891605
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the :exc:`SyntaxError` error message when the user types ``import x
2+
from y`` instead of ``from y import x``. Patch by Pablo Galindo

0 commit comments

Comments
 (0)