mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:14:52 +00:00
[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
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:
parent
8a027b0d74
commit
c3f2187fda
9 changed files with 373 additions and 18 deletions
|
@ -0,0 +1,8 @@
|
|||
def f1():
|
||||
from module import *
|
||||
class C:
|
||||
from module import *
|
||||
def f2():
|
||||
from ..module import *
|
||||
def f3():
|
||||
from module import *, *
|
|
@ -0,0 +1 @@
|
|||
from module import *
|
|
@ -3,16 +3,16 @@
|
|||
//! This checker is not responsible for traversing the AST itself. Instead, its
|
||||
//! [`SemanticSyntaxChecker::visit_stmt`] and [`SemanticSyntaxChecker::visit_expr`] methods should
|
||||
//! be called in a parent `Visitor`'s `visit_stmt` and `visit_expr` methods, respectively.
|
||||
use std::fmt::Display;
|
||||
|
||||
use ruff_python_ast::{
|
||||
self as ast, Expr, ExprContext, IrrefutablePatternKind, Pattern, PythonVersion, Stmt, StmtExpr,
|
||||
StmtImportFrom,
|
||||
comparable::ComparableExpr,
|
||||
helpers,
|
||||
visitor::{Visitor, walk_expr},
|
||||
};
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
use rustc_hash::{FxBuildHasher, FxHashSet};
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SemanticSyntaxChecker {
|
||||
|
@ -58,10 +58,40 @@ impl SemanticSyntaxChecker {
|
|||
|
||||
fn check_stmt<Ctx: SemanticSyntaxContext>(&mut self, stmt: &ast::Stmt, ctx: &Ctx) {
|
||||
match stmt {
|
||||
Stmt::ImportFrom(StmtImportFrom { range, module, .. }) => {
|
||||
Stmt::ImportFrom(StmtImportFrom {
|
||||
range,
|
||||
module,
|
||||
level,
|
||||
names,
|
||||
..
|
||||
}) => {
|
||||
if self.seen_futures_boundary && matches!(module.as_deref(), Some("__future__")) {
|
||||
Self::add_error(ctx, SemanticSyntaxErrorKind::LateFutureImport, *range);
|
||||
}
|
||||
for alias in names {
|
||||
if alias.name.as_str() == "*" && !ctx.in_module_scope() {
|
||||
// test_err import_from_star
|
||||
// def f1():
|
||||
// from module import *
|
||||
// class C:
|
||||
// from module import *
|
||||
// def f2():
|
||||
// from ..module import *
|
||||
// def f3():
|
||||
// from module import *, *
|
||||
|
||||
// test_ok import_from_star
|
||||
// from module import *
|
||||
Self::add_error(
|
||||
ctx,
|
||||
SemanticSyntaxErrorKind::NonModuleImportStar(
|
||||
helpers::format_import_from(*level, module.as_deref()).to_string(),
|
||||
),
|
||||
*range,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Match(match_stmt) => {
|
||||
Self::irrefutable_match_case(match_stmt, ctx);
|
||||
|
@ -1002,6 +1032,9 @@ impl Display for SemanticSyntaxError {
|
|||
SemanticSyntaxErrorKind::YieldFromInAsyncFunction => {
|
||||
f.write_str("`yield from` statement in async function; use `async for` instead")
|
||||
}
|
||||
SemanticSyntaxErrorKind::NonModuleImportStar(name) => {
|
||||
write!(f, "`from {name} import *` only allowed at module level")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1362,6 +1395,9 @@ pub enum SemanticSyntaxErrorKind {
|
|||
|
||||
/// Represents the use of `yield from` inside an asynchronous function.
|
||||
YieldFromInAsyncFunction,
|
||||
|
||||
/// Represents the use of `from <module> import *` outside module scope.
|
||||
NonModuleImportStar(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, get_size2::GetSize)]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
)
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue