Support 'ignore' in pyproject.toml (#126)

This commit is contained in:
Charlie Marsh 2022-09-07 22:34:51 -04:00 committed by GitHub
parent fad4e4c51d
commit 994f12050d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 12 deletions

View File

@ -1,4 +1,3 @@
use std::collections::BTreeSet;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::Result; use anyhow::Result;
@ -40,7 +39,8 @@ pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<(Pat
pub struct Config { pub struct Config {
pub line_length: Option<usize>, pub line_length: Option<usize>,
pub exclude: Option<Vec<PathBuf>>, pub exclude: Option<Vec<PathBuf>>,
pub select: Option<BTreeSet<CheckCode>>, pub select: Option<Vec<CheckCode>>,
pub ignore: Option<Vec<CheckCode>>,
} }
#[derive(Debug, PartialEq, Eq, Deserialize)] #[derive(Debug, PartialEq, Eq, Deserialize)]
@ -90,7 +90,6 @@ fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::BTreeSet;
use std::path::Path; use std::path::Path;
use anyhow::Result; use anyhow::Result;
@ -125,6 +124,7 @@ mod tests {
line_length: None, line_length: None,
exclude: None, exclude: None,
select: None, select: None,
ignore: None,
}) })
}) })
); );
@ -143,6 +143,7 @@ line-length = 79
line_length: Some(79), line_length: Some(79),
exclude: None, exclude: None,
select: None, select: None,
ignore: None,
}) })
}) })
); );
@ -161,6 +162,7 @@ exclude = ["foo.py"]
line_length: None, line_length: None,
exclude: Some(vec![Path::new("foo.py").to_path_buf()]), exclude: Some(vec![Path::new("foo.py").to_path_buf()]),
select: None, select: None,
ignore: None,
}) })
}) })
); );
@ -178,7 +180,27 @@ select = ["E501"]
ruff: Some(Config { ruff: Some(Config {
line_length: None, line_length: None,
exclude: None, exclude: None,
select: Some(BTreeSet::from([CheckCode::E501])), select: Some(vec![CheckCode::E501]),
ignore: None,
})
})
);
let pyproject: PyProject = toml::from_str(
r#"
[tool.black]
[tool.ruff]
ignore = ["E501"]
"#,
)?;
assert_eq!(
pyproject.tool,
Some(Tools {
ruff: Some(Config {
line_length: None,
exclude: None,
select: None,
ignore: Some(vec![CheckCode::E501]),
}) })
}) })
); );
@ -236,7 +258,7 @@ other-attribute = 1
Path::new("excluded.py").to_path_buf(), Path::new("excluded.py").to_path_buf(),
Path::new("**/migrations").to_path_buf() Path::new("**/migrations").to_path_buf()
]), ]),
select: Some(BTreeSet::from([ select: Some(vec![
CheckCode::E402, CheckCode::E402,
CheckCode::E501, CheckCode::E501,
CheckCode::E711, CheckCode::E711,
@ -263,7 +285,8 @@ other-attribute = 1
CheckCode::F901, CheckCode::F901,
CheckCode::R001, CheckCode::R001,
CheckCode::R002, CheckCode::R002,
])), ]),
ignore: None,
} }
); );

View File

@ -27,7 +27,7 @@ impl Hash for Settings {
impl Settings { impl Settings {
pub fn from_paths<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<Self> { pub fn from_paths<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<Self> {
let (project_root, config) = load_config(paths)?; let (project_root, config) = load_config(paths)?;
Ok(Settings { let mut settings = Settings {
line_length: config.line_length.unwrap_or(88), line_length: config.line_length.unwrap_or(88),
exclude: config exclude: config
.exclude .exclude
@ -42,8 +42,8 @@ impl Settings {
}) })
.map(|path| Pattern::new(&path.to_string_lossy()).expect("Invalid pattern.")) .map(|path| Pattern::new(&path.to_string_lossy()).expect("Invalid pattern."))
.collect(), .collect(),
select: config.select.unwrap_or_else(|| { select: BTreeSet::from_iter(config.select.unwrap_or_else(|| {
BTreeSet::from([ vec![
CheckCode::E402, CheckCode::E402,
CheckCode::E501, CheckCode::E501,
CheckCode::E711, CheckCode::E711,
@ -71,9 +71,13 @@ impl Settings {
// Disable refactoring codes by default. // Disable refactoring codes by default.
// CheckCode::R001, // CheckCode::R001,
// CheckCode::R002, // CheckCode::R002,
]) ]
}), })),
}) };
if let Some(ignore) = &config.ignore {
settings.ignore(ignore);
}
Ok(settings)
} }
pub fn select(&mut self, codes: Vec<CheckCode>) { pub fn select(&mut self, codes: Vec<CheckCode>) {