-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow dotted name in
from ... import
statement (#10903)
## Summary Dotted names aren't allowed in `from ... import` statement. They're only allowed in `import` statement. ### Alternative Another solution would be to parse the dotted name, check if there's a `.` in the parsed string and raise an error. I choose not to do this because it didn't make sense to do `contains` for every import name. ## Test Plan Add invalid syntax test cases to verify the logic.
- Loading branch information
1 parent
27bb09c
commit cefa753
Showing
3 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/err/from_import_dotted_names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from x import a. | ||
from x import a.b | ||
from x import a, b.c, d, e.f, g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_dotted_names.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/from_import_dotted_names.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..67, | ||
body: [ | ||
ImportFrom( | ||
StmtImportFrom { | ||
range: 0..16, | ||
module: Some( | ||
Identifier { | ||
id: "x", | ||
range: 5..6, | ||
}, | ||
), | ||
names: [ | ||
Alias { | ||
range: 14..15, | ||
name: Identifier { | ||
id: "a", | ||
range: 14..15, | ||
}, | ||
asname: None, | ||
}, | ||
], | ||
level: Some( | ||
0, | ||
), | ||
}, | ||
), | ||
ImportFrom( | ||
StmtImportFrom { | ||
range: 17..34, | ||
module: Some( | ||
Identifier { | ||
id: "x", | ||
range: 22..23, | ||
}, | ||
), | ||
names: [ | ||
Alias { | ||
range: 31..32, | ||
name: Identifier { | ||
id: "a", | ||
range: 31..32, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 33..34, | ||
name: Identifier { | ||
id: "b", | ||
range: 33..34, | ||
}, | ||
asname: None, | ||
}, | ||
], | ||
level: Some( | ||
0, | ||
), | ||
}, | ||
), | ||
ImportFrom( | ||
StmtImportFrom { | ||
range: 35..66, | ||
module: Some( | ||
Identifier { | ||
id: "x", | ||
range: 40..41, | ||
}, | ||
), | ||
names: [ | ||
Alias { | ||
range: 49..50, | ||
name: Identifier { | ||
id: "a", | ||
range: 49..50, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 52..53, | ||
name: Identifier { | ||
id: "b", | ||
range: 52..53, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 54..55, | ||
name: Identifier { | ||
id: "c", | ||
range: 54..55, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 57..58, | ||
name: Identifier { | ||
id: "d", | ||
range: 57..58, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 60..61, | ||
name: Identifier { | ||
id: "e", | ||
range: 60..61, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 62..63, | ||
name: Identifier { | ||
id: "f", | ||
range: 62..63, | ||
}, | ||
asname: None, | ||
}, | ||
Alias { | ||
range: 65..66, | ||
name: Identifier { | ||
id: "g", | ||
range: 65..66, | ||
}, | ||
asname: None, | ||
}, | ||
], | ||
level: Some( | ||
0, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Errors | ||
|
||
| | ||
1 | from x import a. | ||
| ^ Syntax Error: Expected ',', found '.' | ||
2 | from x import a.b | ||
3 | from x import a, b.c, d, e.f, g | ||
| | ||
|
||
|
||
| | ||
1 | from x import a. | ||
2 | from x import a.b | ||
| ^ Syntax Error: Expected ',', found '.' | ||
3 | from x import a, b.c, d, e.f, g | ||
| | ||
|
||
|
||
| | ||
1 | from x import a. | ||
2 | from x import a.b | ||
3 | from x import a, b.c, d, e.f, g | ||
| ^ Syntax Error: Expected ',', found '.' | ||
| | ||
|
||
|
||
| | ||
1 | from x import a. | ||
2 | from x import a.b | ||
3 | from x import a, b.c, d, e.f, g | ||
| ^ Syntax Error: Expected ',', found '.' | ||
| |