mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:43 +00:00
Flag unimplemented error codes in M001 (#388)
This commit is contained in:
parent
9cc902b802
commit
46750a3e17
5 changed files with 98 additions and 59 deletions
3
resources/test/fixtures/M001.py
vendored
3
resources/test/fixtures/M001.py
vendored
|
@ -15,6 +15,9 @@ def f() -> None:
|
||||||
# Invalid
|
# Invalid
|
||||||
d = 1 # noqa: F841, E501
|
d = 1 # noqa: F841, E501
|
||||||
|
|
||||||
|
# Invalid (and unimplemented)
|
||||||
|
d = 1 # noqa: F841, W191
|
||||||
|
|
||||||
|
|
||||||
# Valid
|
# Valid
|
||||||
_ = """Lorem ipsum dolor sit amet.
|
_ = """Lorem ipsum dolor sit amet.
|
||||||
|
|
|
@ -184,15 +184,15 @@ pub fn check_lines(
|
||||||
let mut valid_codes = vec![];
|
let mut valid_codes = vec![];
|
||||||
for code in codes {
|
for code in codes {
|
||||||
if !matches.contains(&code) {
|
if !matches.contains(&code) {
|
||||||
invalid_codes.push(code);
|
invalid_codes.push(code.to_string());
|
||||||
} else {
|
} else {
|
||||||
valid_codes.push(code);
|
valid_codes.push(code.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !invalid_codes.is_empty() {
|
if !invalid_codes.is_empty() {
|
||||||
let mut check = Check::new(
|
let mut check = Check::new(
|
||||||
CheckKind::UnusedNOQA(Some(invalid_codes.join(", "))),
|
CheckKind::UnusedNOQA(Some(invalid_codes)),
|
||||||
Range {
|
Range {
|
||||||
location: Location::new(row + 1, start + 1),
|
location: Location::new(row + 1, start + 1),
|
||||||
end_location: Location::new(row + 1, end + 1),
|
end_location: Location::new(row + 1, end + 1),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::str::FromStr;
|
||||||
use strum_macros::{AsRefStr, EnumIter, EnumString};
|
use strum_macros::{AsRefStr, EnumIter, EnumString};
|
||||||
|
|
||||||
use crate::ast::checks::Primitive;
|
use crate::ast::checks::Primitive;
|
||||||
|
@ -237,7 +238,7 @@ pub enum CheckKind {
|
||||||
UsePEP604Annotation,
|
UsePEP604Annotation,
|
||||||
SuperCallWithParameters,
|
SuperCallWithParameters,
|
||||||
// Meta
|
// Meta
|
||||||
UnusedNOQA(Option<String>),
|
UnusedNOQA(Option<Vec<String>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CheckCode {
|
impl CheckCode {
|
||||||
|
@ -650,9 +651,21 @@ impl CheckKind {
|
||||||
"Use `super()` instead of `super(__class__, self)`".to_string()
|
"Use `super()` instead of `super(__class__, self)`".to_string()
|
||||||
}
|
}
|
||||||
// Meta
|
// Meta
|
||||||
CheckKind::UnusedNOQA(code) => match code {
|
CheckKind::UnusedNOQA(codes) => match codes {
|
||||||
None => "Unused `noqa` directive".to_string(),
|
None => "Unused `noqa` directive".to_string(),
|
||||||
Some(code) => format!("Unused `noqa` directive for: {code}"),
|
Some(codes) => {
|
||||||
|
let codes = codes
|
||||||
|
.iter()
|
||||||
|
.map(|code| {
|
||||||
|
if CheckCode::from_str(code).is_ok() {
|
||||||
|
code.to_string()
|
||||||
|
} else {
|
||||||
|
format!("{code} (not implemented)")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.join(", ");
|
||||||
|
format!("Unused `noqa` directive for: {codes}")
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -762,42 +762,6 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn m001() -> Result<()> {
|
|
||||||
let mut checks = check_path(
|
|
||||||
Path::new("./resources/test/fixtures/M001.py"),
|
|
||||||
&settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]),
|
|
||||||
&fixer::Mode::Generate,
|
|
||||||
)?;
|
|
||||||
checks.sort_by_key(|check| check.location);
|
|
||||||
insta::assert_yaml_snapshot!(checks);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn init() -> Result<()> {
|
|
||||||
let mut checks = check_path(
|
|
||||||
Path::new("./resources/test/fixtures/__init__.py"),
|
|
||||||
&settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]),
|
|
||||||
&fixer::Mode::Generate,
|
|
||||||
)?;
|
|
||||||
checks.sort_by_key(|check| check.location);
|
|
||||||
insta::assert_yaml_snapshot!(checks);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn future_annotations() -> Result<()> {
|
|
||||||
let mut checks = check_path(
|
|
||||||
Path::new("./resources/test/fixtures/future_annotations.py"),
|
|
||||||
&settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]),
|
|
||||||
&fixer::Mode::Generate,
|
|
||||||
)?;
|
|
||||||
checks.sort_by_key(|check| check.location);
|
|
||||||
insta::assert_yaml_snapshot!(checks);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn e999() -> Result<()> {
|
fn e999() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = check_path(
|
||||||
|
@ -1097,4 +1061,40 @@ mod tests {
|
||||||
insta::assert_yaml_snapshot!(checks);
|
insta::assert_yaml_snapshot!(checks);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn m001() -> Result<()> {
|
||||||
|
let mut checks = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/M001.py"),
|
||||||
|
&settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]),
|
||||||
|
&fixer::Mode::Generate,
|
||||||
|
)?;
|
||||||
|
checks.sort_by_key(|check| check.location);
|
||||||
|
insta::assert_yaml_snapshot!(checks);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn init() -> Result<()> {
|
||||||
|
let mut checks = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/__init__.py"),
|
||||||
|
&settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]),
|
||||||
|
&fixer::Mode::Generate,
|
||||||
|
)?;
|
||||||
|
checks.sort_by_key(|check| check.location);
|
||||||
|
insta::assert_yaml_snapshot!(checks);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn future_annotations() -> Result<()> {
|
||||||
|
let mut checks = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/future_annotations.py"),
|
||||||
|
&settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]),
|
||||||
|
&fixer::Mode::Generate,
|
||||||
|
)?;
|
||||||
|
checks.sort_by_key(|check| check.location);
|
||||||
|
insta::assert_yaml_snapshot!(checks);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,8 @@ expression: checks
|
||||||
column: 18
|
column: 18
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedNOQA: E501
|
UnusedNOQA:
|
||||||
|
- E501
|
||||||
location:
|
location:
|
||||||
row: 13
|
row: 13
|
||||||
column: 10
|
column: 10
|
||||||
|
@ -37,7 +38,9 @@ expression: checks
|
||||||
column: 24
|
column: 24
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedNOQA: E501
|
UnusedNOQA:
|
||||||
|
- F841
|
||||||
|
- E501
|
||||||
location:
|
location:
|
||||||
row: 16
|
row: 16
|
||||||
column: 10
|
column: 10
|
||||||
|
@ -45,7 +48,7 @@ expression: checks
|
||||||
row: 16
|
row: 16
|
||||||
column: 30
|
column: 30
|
||||||
fix:
|
fix:
|
||||||
content: " # noqa: F841"
|
content: ""
|
||||||
location:
|
location:
|
||||||
row: 16
|
row: 16
|
||||||
column: 10
|
column: 10
|
||||||
|
@ -54,54 +57,74 @@ expression: checks
|
||||||
column: 30
|
column: 30
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedNOQA: F841
|
UnusedNOQA:
|
||||||
|
- W191
|
||||||
location:
|
location:
|
||||||
row: 41
|
row: 19
|
||||||
|
column: 10
|
||||||
|
end_location:
|
||||||
|
row: 19
|
||||||
|
column: 30
|
||||||
|
fix:
|
||||||
|
content: " # noqa: F841"
|
||||||
|
location:
|
||||||
|
row: 19
|
||||||
|
column: 10
|
||||||
|
end_location:
|
||||||
|
row: 19
|
||||||
|
column: 30
|
||||||
|
applied: false
|
||||||
|
- kind:
|
||||||
|
UnusedNOQA:
|
||||||
|
- F841
|
||||||
|
location:
|
||||||
|
row: 44
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 41
|
row: 44
|
||||||
column: 24
|
column: 24
|
||||||
fix:
|
fix:
|
||||||
content: " # noqa: E501"
|
content: " # noqa: E501"
|
||||||
location:
|
location:
|
||||||
row: 41
|
row: 44
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 41
|
row: 44
|
||||||
column: 24
|
column: 24
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedNOQA: E501
|
UnusedNOQA:
|
||||||
|
- E501
|
||||||
location:
|
location:
|
||||||
row: 49
|
row: 52
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 49
|
row: 52
|
||||||
column: 18
|
column: 18
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: ""
|
||||||
location:
|
location:
|
||||||
row: 49
|
row: 52
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 49
|
row: 52
|
||||||
column: 18
|
column: 18
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedNOQA: ~
|
UnusedNOQA: ~
|
||||||
location:
|
location:
|
||||||
row: 57
|
row: 60
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 57
|
row: 60
|
||||||
column: 12
|
column: 12
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: ""
|
||||||
location:
|
location:
|
||||||
row: 57
|
row: 60
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 57
|
row: 60
|
||||||
column: 12
|
column: 12
|
||||||
applied: false
|
applied: false
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue