mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Implement F822 (#94)
This commit is contained in:
parent
e9412c9452
commit
6bb6cb1783
8 changed files with 101 additions and 36 deletions
|
@ -118,6 +118,7 @@ OPTIONS:
|
||||||
| F704 | YieldOutsideFunction | a `yield` or `yield from` statement outside of a function/method |
|
| F704 | YieldOutsideFunction | a `yield` or `yield from` statement outside of a function/method |
|
||||||
| F706 | ReturnOutsideFunction | a `return` statement outside of a function/method |
|
| F706 | ReturnOutsideFunction | a `return` statement outside of a function/method |
|
||||||
| F821 | UndefinedName | Undefined name `...` |
|
| F821 | UndefinedName | Undefined name `...` |
|
||||||
|
| F822 | UndefinedExport | Undefined name `...` in __all__ |
|
||||||
| F823 | UndefinedLocal | Local variable `...` referenced before assignment |
|
| F823 | UndefinedLocal | Local variable `...` referenced before assignment |
|
||||||
| F831 | DuplicateArgumentName | Duplicate argument name in function definition |
|
| F831 | DuplicateArgumentName | Duplicate argument name in function definition |
|
||||||
| F841 | UnusedVariable | Local variable `...` is assigned to but never used |
|
| F841 | UnusedVariable | Local variable `...` is assigned to but never used |
|
||||||
|
|
|
@ -12,6 +12,7 @@ fn main() {
|
||||||
CheckKind::ReturnOutsideFunction,
|
CheckKind::ReturnOutsideFunction,
|
||||||
CheckKind::UndefinedLocal("...".to_string()),
|
CheckKind::UndefinedLocal("...".to_string()),
|
||||||
CheckKind::UndefinedName("...".to_string()),
|
CheckKind::UndefinedName("...".to_string()),
|
||||||
|
CheckKind::UndefinedExport("...".to_string()),
|
||||||
CheckKind::UnusedImport("...".to_string()),
|
CheckKind::UnusedImport("...".to_string()),
|
||||||
CheckKind::UnusedVariable("...".to_string()),
|
CheckKind::UnusedVariable("...".to_string()),
|
||||||
CheckKind::UselessObjectInheritance("...".to_string()),
|
CheckKind::UselessObjectInheritance("...".to_string()),
|
||||||
|
|
3
resources/test/fixtures/F822.py
vendored
Normal file
3
resources/test/fixtures/F822.py
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
a = 1
|
||||||
|
|
||||||
|
__all__ = ["a", "b"]
|
1
resources/test/fixtures/pyproject.toml
vendored
1
resources/test/fixtures/pyproject.toml
vendored
|
@ -10,6 +10,7 @@ select = [
|
||||||
"F704",
|
"F704",
|
||||||
"F706",
|
"F706",
|
||||||
"F821",
|
"F821",
|
||||||
|
"F822",
|
||||||
"F823",
|
"F823",
|
||||||
"F831",
|
"F831",
|
||||||
"F841",
|
"F841",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Stmt,
|
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Stmt,
|
||||||
|
@ -15,6 +16,7 @@ use crate::visitor::{walk_excepthandler, Visitor};
|
||||||
|
|
||||||
struct Checker<'a> {
|
struct Checker<'a> {
|
||||||
settings: &'a Settings,
|
settings: &'a Settings,
|
||||||
|
path: &'a str,
|
||||||
checks: Vec<Check>,
|
checks: Vec<Check>,
|
||||||
scopes: Vec<Scope>,
|
scopes: Vec<Scope>,
|
||||||
dead_scopes: Vec<Scope>,
|
dead_scopes: Vec<Scope>,
|
||||||
|
@ -24,9 +26,10 @@ struct Checker<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Checker<'_> {
|
impl Checker<'_> {
|
||||||
pub fn new(settings: &Settings) -> Checker {
|
pub fn new<'a>(settings: &'a Settings, path: &'a str) -> Checker<'a> {
|
||||||
Checker {
|
Checker {
|
||||||
settings,
|
settings,
|
||||||
|
path,
|
||||||
checks: vec![],
|
checks: vec![],
|
||||||
scopes: vec![],
|
scopes: vec![],
|
||||||
dead_scopes: vec![],
|
dead_scopes: vec![],
|
||||||
|
@ -673,19 +676,40 @@ impl Checker<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_dead_scopes(&mut self) {
|
fn check_dead_scopes(&mut self) {
|
||||||
if self.settings.select.contains(&CheckCode::F401) {
|
if !self.settings.select.contains(&CheckCode::F822)
|
||||||
|
&& !self.settings.select.contains(&CheckCode::F401)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for scope in &self.dead_scopes {
|
for scope in &self.dead_scopes {
|
||||||
let all_binding = match scope.values.get("__all__") {
|
let all_binding = scope.values.get("__all__");
|
||||||
Some(binding) => match &binding.kind {
|
let all_names = all_binding.and_then(|binding| match &binding.kind {
|
||||||
BindingKind::Export(names) => Some(names),
|
BindingKind::Export(names) => Some(names),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
});
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
if self.settings.select.contains(&CheckCode::F822)
|
||||||
|
&& !Path::new(self.path).ends_with("__init__.py")
|
||||||
|
{
|
||||||
|
if let Some(binding) = all_binding {
|
||||||
|
if let Some(names) = all_names {
|
||||||
|
for name in names {
|
||||||
|
if !scope.values.contains_key(name) {
|
||||||
|
self.checks.push(Check {
|
||||||
|
kind: CheckKind::UndefinedExport(name.to_string()),
|
||||||
|
location: binding.location,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.settings.select.contains(&CheckCode::F401) {
|
||||||
for (name, binding) in scope.values.iter().rev() {
|
for (name, binding) in scope.values.iter().rev() {
|
||||||
let used = binding.used.is_some()
|
let used = binding.used.is_some()
|
||||||
|| all_binding
|
|| all_names
|
||||||
.map(|names| names.contains(name))
|
.map(|names| names.contains(name))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
@ -708,7 +732,7 @@ impl Checker<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_ast(python_ast: &Suite, settings: &Settings, path: &str) -> Vec<Check> {
|
pub fn check_ast(python_ast: &Suite, settings: &Settings, path: &str) -> Vec<Check> {
|
||||||
let mut checker = Checker::new(settings);
|
let mut checker = Checker::new(settings, path);
|
||||||
checker.push_scope(Scope::new(ScopeKind::Module));
|
checker.push_scope(Scope::new(ScopeKind::Module));
|
||||||
checker.bind_builtins();
|
checker.bind_builtins();
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub enum CheckCode {
|
||||||
F704,
|
F704,
|
||||||
F706,
|
F706,
|
||||||
F821,
|
F821,
|
||||||
|
F822,
|
||||||
F823,
|
F823,
|
||||||
F831,
|
F831,
|
||||||
F841,
|
F841,
|
||||||
|
@ -36,6 +37,7 @@ impl FromStr for CheckCode {
|
||||||
"F704" => Ok(CheckCode::F704),
|
"F704" => Ok(CheckCode::F704),
|
||||||
"F706" => Ok(CheckCode::F706),
|
"F706" => Ok(CheckCode::F706),
|
||||||
"F821" => Ok(CheckCode::F821),
|
"F821" => Ok(CheckCode::F821),
|
||||||
|
"F822" => Ok(CheckCode::F822),
|
||||||
"F823" => Ok(CheckCode::F823),
|
"F823" => Ok(CheckCode::F823),
|
||||||
"F831" => Ok(CheckCode::F831),
|
"F831" => Ok(CheckCode::F831),
|
||||||
"F841" => Ok(CheckCode::F841),
|
"F841" => Ok(CheckCode::F841),
|
||||||
|
@ -58,6 +60,7 @@ impl CheckCode {
|
||||||
CheckCode::F706 => "F706",
|
CheckCode::F706 => "F706",
|
||||||
CheckCode::F821 => "F821",
|
CheckCode::F821 => "F821",
|
||||||
CheckCode::F823 => "F823",
|
CheckCode::F823 => "F823",
|
||||||
|
CheckCode::F822 => "F822",
|
||||||
CheckCode::F831 => "F831",
|
CheckCode::F831 => "F831",
|
||||||
CheckCode::F841 => "F841",
|
CheckCode::F841 => "F841",
|
||||||
CheckCode::F901 => "F901",
|
CheckCode::F901 => "F901",
|
||||||
|
@ -76,6 +79,7 @@ impl CheckCode {
|
||||||
CheckCode::F704 => &LintSource::AST,
|
CheckCode::F704 => &LintSource::AST,
|
||||||
CheckCode::F706 => &LintSource::AST,
|
CheckCode::F706 => &LintSource::AST,
|
||||||
CheckCode::F821 => &LintSource::AST,
|
CheckCode::F821 => &LintSource::AST,
|
||||||
|
CheckCode::F822 => &LintSource::AST,
|
||||||
CheckCode::F823 => &LintSource::AST,
|
CheckCode::F823 => &LintSource::AST,
|
||||||
CheckCode::F831 => &LintSource::AST,
|
CheckCode::F831 => &LintSource::AST,
|
||||||
CheckCode::F841 => &LintSource::AST,
|
CheckCode::F841 => &LintSource::AST,
|
||||||
|
@ -101,6 +105,7 @@ pub enum CheckKind {
|
||||||
RaiseNotImplemented,
|
RaiseNotImplemented,
|
||||||
ReturnOutsideFunction,
|
ReturnOutsideFunction,
|
||||||
UndefinedLocal(String),
|
UndefinedLocal(String),
|
||||||
|
UndefinedExport(String),
|
||||||
UndefinedName(String),
|
UndefinedName(String),
|
||||||
UnusedImport(String),
|
UnusedImport(String),
|
||||||
UnusedVariable(String),
|
UnusedVariable(String),
|
||||||
|
@ -120,6 +125,7 @@ impl CheckKind {
|
||||||
CheckKind::RaiseNotImplemented => "RaiseNotImplemented",
|
CheckKind::RaiseNotImplemented => "RaiseNotImplemented",
|
||||||
CheckKind::ReturnOutsideFunction => "ReturnOutsideFunction",
|
CheckKind::ReturnOutsideFunction => "ReturnOutsideFunction",
|
||||||
CheckKind::UndefinedLocal(_) => "UndefinedLocal",
|
CheckKind::UndefinedLocal(_) => "UndefinedLocal",
|
||||||
|
CheckKind::UndefinedExport(_) => "UndefinedExport",
|
||||||
CheckKind::UndefinedName(_) => "UndefinedName",
|
CheckKind::UndefinedName(_) => "UndefinedName",
|
||||||
CheckKind::UnusedImport(_) => "UnusedImport",
|
CheckKind::UnusedImport(_) => "UnusedImport",
|
||||||
CheckKind::UnusedVariable(_) => "UnusedVariable",
|
CheckKind::UnusedVariable(_) => "UnusedVariable",
|
||||||
|
@ -138,6 +144,7 @@ impl CheckKind {
|
||||||
CheckKind::LineTooLong => &CheckCode::E501,
|
CheckKind::LineTooLong => &CheckCode::E501,
|
||||||
CheckKind::RaiseNotImplemented => &CheckCode::F901,
|
CheckKind::RaiseNotImplemented => &CheckCode::F901,
|
||||||
CheckKind::ReturnOutsideFunction => &CheckCode::F706,
|
CheckKind::ReturnOutsideFunction => &CheckCode::F706,
|
||||||
|
CheckKind::UndefinedExport(_) => &CheckCode::F822,
|
||||||
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
||||||
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
||||||
CheckKind::UnusedImport(_) => &CheckCode::F401,
|
CheckKind::UnusedImport(_) => &CheckCode::F401,
|
||||||
|
@ -165,6 +172,9 @@ impl CheckKind {
|
||||||
CheckKind::ReturnOutsideFunction => {
|
CheckKind::ReturnOutsideFunction => {
|
||||||
"a `return` statement outside of a function/method".to_string()
|
"a `return` statement outside of a function/method".to_string()
|
||||||
}
|
}
|
||||||
|
CheckKind::UndefinedExport(name) => {
|
||||||
|
format!("Undefined name `{name}` in __all__")
|
||||||
|
}
|
||||||
CheckKind::UndefinedName(name) => {
|
CheckKind::UndefinedName(name) => {
|
||||||
format!("Undefined name `{name}`")
|
format!("Undefined name `{name}`")
|
||||||
}
|
}
|
||||||
|
|
|
@ -330,6 +330,54 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn f822() -> Result<()> {
|
||||||
|
let actual = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/F822.py"),
|
||||||
|
&settings::Settings {
|
||||||
|
line_length: 88,
|
||||||
|
exclude: vec![],
|
||||||
|
select: BTreeSet::from([CheckCode::F822]),
|
||||||
|
},
|
||||||
|
&cache::Mode::None,
|
||||||
|
)?;
|
||||||
|
let expected = vec![Message {
|
||||||
|
kind: CheckKind::UndefinedExport("b".to_string()),
|
||||||
|
location: Location::new(3, 1),
|
||||||
|
filename: "./resources/test/fixtures/F822.py".to_string(),
|
||||||
|
}];
|
||||||
|
assert_eq!(actual.len(), expected.len());
|
||||||
|
for i in 0..actual.len() {
|
||||||
|
assert_eq!(actual[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn f823() -> Result<()> {
|
||||||
|
let actual = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/F823.py"),
|
||||||
|
&settings::Settings {
|
||||||
|
line_length: 88,
|
||||||
|
exclude: vec![],
|
||||||
|
select: BTreeSet::from([CheckCode::F823]),
|
||||||
|
},
|
||||||
|
&cache::Mode::None,
|
||||||
|
)?;
|
||||||
|
let expected = vec![Message {
|
||||||
|
kind: CheckKind::UndefinedLocal("my_var".to_string()),
|
||||||
|
location: Location::new(6, 5),
|
||||||
|
filename: "./resources/test/fixtures/F823.py".to_string(),
|
||||||
|
}];
|
||||||
|
assert_eq!(actual.len(), expected.len());
|
||||||
|
for i in 0..actual.len() {
|
||||||
|
assert_eq!(actual[i], expected[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn f831() -> Result<()> {
|
fn f831() -> Result<()> {
|
||||||
let actual = check_path(
|
let actual = check_path(
|
||||||
|
@ -366,30 +414,6 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn f823() -> Result<()> {
|
|
||||||
let actual = check_path(
|
|
||||||
Path::new("./resources/test/fixtures/F823.py"),
|
|
||||||
&settings::Settings {
|
|
||||||
line_length: 88,
|
|
||||||
exclude: vec![],
|
|
||||||
select: BTreeSet::from([CheckCode::F823]),
|
|
||||||
},
|
|
||||||
&cache::Mode::None,
|
|
||||||
)?;
|
|
||||||
let expected = vec![Message {
|
|
||||||
kind: CheckKind::UndefinedLocal("my_var".to_string()),
|
|
||||||
location: Location::new(6, 5),
|
|
||||||
filename: "./resources/test/fixtures/F823.py".to_string(),
|
|
||||||
}];
|
|
||||||
assert_eq!(actual.len(), expected.len());
|
|
||||||
for i in 0..actual.len() {
|
|
||||||
assert_eq!(actual[i], expected[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn f841() -> Result<()> {
|
fn f841() -> Result<()> {
|
||||||
let actual = check_path(
|
let actual = check_path(
|
||||||
|
|
|
@ -245,6 +245,7 @@ other-attribute = 1
|
||||||
CheckCode::F704,
|
CheckCode::F704,
|
||||||
CheckCode::F706,
|
CheckCode::F706,
|
||||||
CheckCode::F821,
|
CheckCode::F821,
|
||||||
|
CheckCode::F822,
|
||||||
CheckCode::F823,
|
CheckCode::F823,
|
||||||
CheckCode::F831,
|
CheckCode::F831,
|
||||||
CheckCode::F841,
|
CheckCode::F841,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue