Flag unimplemented error codes in M001 (#388)

This commit is contained in:
Charlie Marsh 2022-10-10 10:03:40 -04:00 committed by GitHub
parent 9cc902b802
commit 46750a3e17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 59 deletions

View file

@ -15,6 +15,9 @@ def f() -> None:
# Invalid
d = 1 # noqa: F841, E501
# Invalid (and unimplemented)
d = 1 # noqa: F841, W191
# Valid
_ = """Lorem ipsum dolor sit amet.

View file

@ -184,15 +184,15 @@ pub fn check_lines(
let mut valid_codes = vec![];
for code in codes {
if !matches.contains(&code) {
invalid_codes.push(code);
invalid_codes.push(code.to_string());
} else {
valid_codes.push(code);
valid_codes.push(code.to_string());
}
}
if !invalid_codes.is_empty() {
let mut check = Check::new(
CheckKind::UnusedNOQA(Some(invalid_codes.join(", "))),
CheckKind::UnusedNOQA(Some(invalid_codes)),
Range {
location: Location::new(row + 1, start + 1),
end_location: Location::new(row + 1, end + 1),

View file

@ -1,6 +1,7 @@
use itertools::Itertools;
use rustpython_parser::ast::Location;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum_macros::{AsRefStr, EnumIter, EnumString};
use crate::ast::checks::Primitive;
@ -237,7 +238,7 @@ pub enum CheckKind {
UsePEP604Annotation,
SuperCallWithParameters,
// Meta
UnusedNOQA(Option<String>),
UnusedNOQA(Option<Vec<String>>),
}
impl CheckCode {
@ -650,9 +651,21 @@ impl CheckKind {
"Use `super()` instead of `super(__class__, self)`".to_string()
}
// Meta
CheckKind::UnusedNOQA(code) => match code {
CheckKind::UnusedNOQA(codes) => match codes {
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}")
}
},
}
}

View file

@ -762,42 +762,6 @@ mod tests {
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]
fn e999() -> Result<()> {
let mut checks = check_path(
@ -1097,4 +1061,40 @@ mod tests {
insta::assert_yaml_snapshot!(checks);
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(())
}
}

View file

@ -20,7 +20,8 @@ expression: checks
column: 18
applied: false
- kind:
UnusedNOQA: E501
UnusedNOQA:
- E501
location:
row: 13
column: 10
@ -37,71 +38,93 @@ expression: checks
column: 24
applied: false
- kind:
UnusedNOQA: E501
UnusedNOQA:
- F841
- E501
location:
row: 16
column: 10
end_location:
row: 16
column: 30
fix:
content: ""
location:
row: 16
column: 10
end_location:
row: 16
column: 30
applied: false
- kind:
UnusedNOQA:
- W191
location:
row: 19
column: 10
end_location:
row: 19
column: 30
fix:
content: " # noqa: F841"
location:
row: 16
row: 19
column: 10
end_location:
row: 16
row: 19
column: 30
applied: false
- kind:
UnusedNOQA: F841
UnusedNOQA:
- F841
location:
row: 41
row: 44
column: 4
end_location:
row: 41
row: 44
column: 24
fix:
content: " # noqa: E501"
location:
row: 41
row: 44
column: 4
end_location:
row: 41
row: 44
column: 24
applied: false
- kind:
UnusedNOQA: E501
UnusedNOQA:
- E501
location:
row: 49
row: 52
column: 4
end_location:
row: 49
row: 52
column: 18
fix:
content: ""
location:
row: 49
row: 52
column: 4
end_location:
row: 49
row: 52
column: 18
applied: false
- kind:
UnusedNOQA: ~
location:
row: 57
row: 60
column: 4
end_location:
row: 57
row: 60
column: 12
fix:
content: ""
location:
row: 57
row: 60
column: 4
end_location:
row: 57
row: 60
column: 12
applied: false