mirror of https://github.com/python/cpython
gh-98931: Improve error message when the user types 'import x from y' instead of 'from y import x' (#98932)
This commit is contained in:
parent
0e15c31c7e
commit
395d4285bf
|
@ -70,6 +70,18 @@ Important deprecations, removals or restrictions:
|
|||
|
||||
* :pep:`623`, Remove wstr from Unicode
|
||||
|
||||
Improved Error Messages
|
||||
=======================
|
||||
|
||||
* Improve the :exc:`SyntaxError` error message when the user types ``import x
|
||||
from y`` instead of ``from y import x``. Contributed by Pablo Galindo in :gh:`98931`.
|
||||
|
||||
>>> import a.y.z from b.y.z
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1
|
||||
import a.y.z from b.y.z
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
SyntaxError: Did you mean to use 'from ... import ...' instead?
|
||||
|
||||
New Features
|
||||
============
|
||||
|
|
|
@ -194,7 +194,10 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
|
|||
|
||||
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
|
||||
|
||||
import_stmt[stmt_ty]: import_name | import_from
|
||||
import_stmt[stmt_ty]:
|
||||
| invalid_import
|
||||
| import_name
|
||||
| import_from
|
||||
|
||||
# Import statements
|
||||
# -----------------
|
||||
|
@ -1230,6 +1233,10 @@ invalid_group:
|
|||
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
|
||||
| '(' a='**' expression ')' {
|
||||
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
|
||||
invalid_import:
|
||||
| a='import' dotted_name 'from' dotted_name {
|
||||
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
|
||||
|
||||
invalid_import_from_targets:
|
||||
| import_from_as_names ',' NEWLINE {
|
||||
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
|
||||
|
|
|
@ -1584,6 +1584,22 @@ SyntaxError: trailing comma not allowed without surrounding parentheses
|
|||
Traceback (most recent call last):
|
||||
SyntaxError: trailing comma not allowed without surrounding parentheses
|
||||
|
||||
>>> import a from b
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: Did you mean to use 'from ... import ...' instead?
|
||||
|
||||
>>> import a.y.z from b.y.z
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: Did you mean to use 'from ... import ...' instead?
|
||||
|
||||
>>> import a from b as bar
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: Did you mean to use 'from ... import ...' instead?
|
||||
|
||||
>>> import a.y.z from b.y.z as bar
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: Did you mean to use 'from ... import ...' instead?
|
||||
|
||||
# Check that we dont raise the "trailing comma" error if there is more
|
||||
# input to the left of the valid part that we parsed.
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Improve the :exc:`SyntaxError` error message when the user types ``import x
|
||||
from y`` instead of ``from y import x``. Patch by Pablo Galindo
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue