Remove unnecessary Option wrapper from some pyproject::Config fields (#326)

This commit is contained in:
Anders Kaseorg 2022-10-04 13:27:40 -07:00 committed by GitHub
parent 03e1397427
commit d1bcc919a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 39 deletions

View file

@ -29,10 +29,13 @@ pub fn load_config(pyproject: &Option<PathBuf>) -> Result<Config> {
pub struct Config { pub struct Config {
pub line_length: Option<usize>, pub line_length: Option<usize>,
pub exclude: Option<Vec<String>>, pub exclude: Option<Vec<String>>,
pub extend_exclude: Option<Vec<String>>, #[serde(default)]
pub extend_exclude: Vec<String>,
pub select: Option<Vec<CheckCode>>, pub select: Option<Vec<CheckCode>>,
pub ignore: Option<Vec<CheckCode>>, #[serde(default)]
pub per_file_ignores: Option<Vec<StrCheckCodePair>>, pub ignore: Vec<CheckCode>,
#[serde(default)]
pub per_file_ignores: Vec<StrCheckCodePair>,
pub dummy_variable_rgx: Option<String>, pub dummy_variable_rgx: Option<String>,
} }
@ -175,10 +178,10 @@ mod tests {
ruff: Some(Config { ruff: Some(Config {
line_length: None, line_length: None,
exclude: None, exclude: None,
extend_exclude: None, extend_exclude: vec![],
select: None, select: None,
ignore: None, ignore: vec![],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
}) })
}) })
@ -197,10 +200,10 @@ line-length = 79
ruff: Some(Config { ruff: Some(Config {
line_length: Some(79), line_length: Some(79),
exclude: None, exclude: None,
extend_exclude: None, extend_exclude: vec![],
select: None, select: None,
ignore: None, ignore: vec![],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
}) })
}) })
@ -219,10 +222,10 @@ exclude = ["foo.py"]
ruff: Some(Config { ruff: Some(Config {
line_length: None, line_length: None,
exclude: Some(vec!["foo.py".to_string()]), exclude: Some(vec!["foo.py".to_string()]),
extend_exclude: None, extend_exclude: vec![],
select: None, select: None,
ignore: None, ignore: vec![],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
}) })
}) })
@ -241,10 +244,10 @@ select = ["E501"]
ruff: Some(Config { ruff: Some(Config {
line_length: None, line_length: None,
exclude: None, exclude: None,
extend_exclude: None, extend_exclude: vec![],
select: Some(vec![CheckCode::E501]), select: Some(vec![CheckCode::E501]),
ignore: None, ignore: vec![],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
}) })
}) })
@ -263,10 +266,10 @@ ignore = ["E501"]
ruff: Some(Config { ruff: Some(Config {
line_length: None, line_length: None,
exclude: None, exclude: None,
extend_exclude: None, extend_exclude: vec![],
select: None, select: None,
ignore: Some(vec![CheckCode::E501]), ignore: vec![CheckCode::E501],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
}) })
}) })
@ -325,14 +328,14 @@ other-attribute = 1
Config { Config {
line_length: Some(88), line_length: Some(88),
exclude: None, exclude: None,
extend_exclude: Some(vec![ extend_exclude: vec![
"excluded.py".to_string(), "excluded.py".to_string(),
"migrations".to_string(), "migrations".to_string(),
"directory/also_excluded.py".to_string(), "directory/also_excluded.py".to_string(),
]), ],
select: None, select: None,
ignore: None, ignore: vec![],
per_file_ignores: None, per_file_ignores: vec![],
dummy_variable_rgx: None, dummy_variable_rgx: None,
} }
); );

View file

@ -149,13 +149,9 @@ impl Settings {
.unwrap_or_else(|| DEFAULT_EXCLUDE.clone()), .unwrap_or_else(|| DEFAULT_EXCLUDE.clone()),
extend_exclude: config extend_exclude: config
.extend_exclude .extend_exclude
.map(|paths| {
paths
.iter() .iter()
.map(|path| FilePattern::from_user(path, &project_root)) .map(|path| FilePattern::from_user(path, &project_root))
.collect() .collect(),
})
.unwrap_or_default(),
select: if let Some(select) = config.select { select: if let Some(select) = config.select {
BTreeSet::from_iter(select) BTreeSet::from_iter(select)
} else { } else {
@ -163,13 +159,9 @@ impl Settings {
}, },
per_file_ignores: config per_file_ignores: config
.per_file_ignores .per_file_ignores
.map(|ignore_strings| {
ignore_strings
.into_iter() .into_iter()
.map(|pair| PerFileIgnore::new(pair, &project_root)) .map(|pair| PerFileIgnore::new(pair, &project_root))
.collect() .collect(),
})
.unwrap_or_default(),
dummy_variable_rgx: match config.dummy_variable_rgx { dummy_variable_rgx: match config.dummy_variable_rgx {
Some(pattern) => Regex::new(&pattern) Some(pattern) => Regex::new(&pattern)
.map_err(|e| anyhow!("Invalid dummy-variable-rgx value: {e}"))?, .map_err(|e| anyhow!("Invalid dummy-variable-rgx value: {e}"))?,
@ -178,9 +170,7 @@ impl Settings {
pyproject, pyproject,
project_root, project_root,
}; };
if let Some(ignore) = &config.ignore { settings.ignore(&config.ignore);
settings.ignore(ignore);
}
Ok(settings) Ok(settings)
} }