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

Skip to content

Commit 13237a2

Browse files
gh-98931: Add custom error messages to invalid import/from with multiple targets (#105985)
Co-authored-by: Alex Waygood <[email protected]>
1 parent a800670 commit 13237a2

File tree

4 files changed

+849
-708
lines changed

4 files changed

+849
-708
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ invalid_group:
12931293
| '(' a='**' expression ')' {
12941294
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
12951295
invalid_import:
1296-
| a='import' dotted_name 'from' dotted_name {
1296+
| a='import' ','.dotted_name+ 'from' dotted_name {
12971297
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
12981298

12991299
invalid_import_from_targets:

Lib/test/test_syntax.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,22 @@
16211621
Traceback (most recent call last):
16221622
SyntaxError: Did you mean to use 'from ... import ...' instead?
16231623
1624+
>>> import a, b,c from b
1625+
Traceback (most recent call last):
1626+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1627+
1628+
>>> import a.y.z, b.y.z, c.y.z from b.y.z
1629+
Traceback (most recent call last):
1630+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1631+
1632+
>>> import a,b,c from b as bar
1633+
Traceback (most recent call last):
1634+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1635+
1636+
>>> import a.y.z, b.y.z, c.y.z from b.y.z as bar
1637+
Traceback (most recent call last):
1638+
SyntaxError: Did you mean to use 'from ... import ...' instead?
1639+
16241640
# Check that we dont raise the "trailing comma" error if there is more
16251641
# input to the left of the valid part that we parsed.
16261642
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure custom :exc:`SyntaxError` error messages are raised for invalid
2+
imports with multiple targets. Patch by Pablo Galindo

0 commit comments

Comments
 (0)