[syntax-errors]: import from * only allowed at module scope (F406) (#20166)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR implements F406
https://docs.astral.sh/ruff/rules/undefined-local-with-nested-import-star-usage/
as a semantic syntax error

## Test Plan

I have written inline tests as directed in #17412

---------

Signed-off-by: 11happy <soni5happy@gmail.com>
This commit is contained in:
Bhuminjay Soni 2025-09-17 01:23:28 +05:30 committed by GitHub
parent 8a027b0d74
commit c3f2187fda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 373 additions and 18 deletions

View file

@ -0,0 +1,271 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/import_from_star.py
---
## AST
```
Module(
ModModule {
node_index: NodeIndex(None),
range: 0..144,
body: [
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 0..34,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f1"),
range: 4..6,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 6..8,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
ImportFrom(
StmtImportFrom {
node_index: NodeIndex(None),
range: 14..34,
module: Some(
Identifier {
id: Name("module"),
range: 19..25,
node_index: NodeIndex(None),
},
),
names: [
Alias {
range: 33..34,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 33..34,
node_index: NodeIndex(None),
},
asname: None,
},
],
level: 0,
},
),
],
},
),
ClassDef(
StmtClassDef {
node_index: NodeIndex(None),
range: 35..68,
decorator_list: [],
name: Identifier {
id: Name("C"),
range: 41..42,
node_index: NodeIndex(None),
},
type_params: None,
arguments: None,
body: [
ImportFrom(
StmtImportFrom {
node_index: NodeIndex(None),
range: 48..68,
module: Some(
Identifier {
id: Name("module"),
range: 53..59,
node_index: NodeIndex(None),
},
),
names: [
Alias {
range: 67..68,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 67..68,
node_index: NodeIndex(None),
},
asname: None,
},
],
level: 0,
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 69..105,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f2"),
range: 73..75,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 75..77,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
ImportFrom(
StmtImportFrom {
node_index: NodeIndex(None),
range: 83..105,
module: Some(
Identifier {
id: Name("module"),
range: 90..96,
node_index: NodeIndex(None),
},
),
names: [
Alias {
range: 104..105,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 104..105,
node_index: NodeIndex(None),
},
asname: None,
},
],
level: 2,
},
),
],
},
),
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 106..143,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("f3"),
range: 110..112,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 112..114,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
ImportFrom(
StmtImportFrom {
node_index: NodeIndex(None),
range: 120..143,
module: Some(
Identifier {
id: Name("module"),
range: 125..131,
node_index: NodeIndex(None),
},
),
names: [
Alias {
range: 139..140,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 139..140,
node_index: NodeIndex(None),
},
asname: None,
},
Alias {
range: 142..143,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 142..143,
node_index: NodeIndex(None),
},
asname: None,
},
],
level: 0,
},
),
],
},
),
],
},
)
```
## Errors
|
6 | from ..module import *
7 | def f3():
8 | from module import *, *
| ^^^^ Syntax Error: Star import must be the only import
|
## Semantic Syntax Errors
|
1 | def f1():
2 | from module import *
| ^^^^^^^^^^^^^^^^^^^^ Syntax Error: `from module import *` only allowed at module level
3 | class C:
4 | from module import *
|
|
2 | from module import *
3 | class C:
4 | from module import *
| ^^^^^^^^^^^^^^^^^^^^ Syntax Error: `from module import *` only allowed at module level
5 | def f2():
6 | from ..module import *
|
|
4 | from module import *
5 | def f2():
6 | from ..module import *
| ^^^^^^^^^^^^^^^^^^^^^^ Syntax Error: `from ..module import *` only allowed at module level
7 | def f3():
8 | from module import *, *
|
|
6 | from ..module import *
7 | def f3():
8 | from module import *, *
| ^^^^^^^^^^^^^^^^^^^^^^^ Syntax Error: `from module import *` only allowed at module level
|

View file

@ -0,0 +1,42 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/import_from_star.py
---
## AST
```
Module(
ModModule {
node_index: NodeIndex(None),
range: 0..21,
body: [
ImportFrom(
StmtImportFrom {
node_index: NodeIndex(None),
range: 0..20,
module: Some(
Identifier {
id: Name("module"),
range: 5..11,
node_index: NodeIndex(None),
},
),
names: [
Alias {
range: 19..20,
node_index: NodeIndex(None),
name: Identifier {
id: Name("*"),
range: 19..20,
node_index: NodeIndex(None),
},
asname: None,
},
],
level: 0,
},
),
],
},
)
```