Skip to content

Commit

Permalink
Add target Python version as a configurable setting (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Oct 7, 2022
1 parent ad23d6a commit 0b9eda8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Options:
Enable automatic additions of noqa directives to failing lines
--dummy-variable-rgx <DUMMY_VARIABLE_RGX>
Regular expression matching the name of dummy variables
--target-version <TARGET_VERSION>
The minimum Python version that should be supported
-h, --help
Print help information
-V, --version
Expand Down
6 changes: 4 additions & 2 deletions src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::plugins;
use crate::python::builtins::{BUILTINS, MAGIC_GLOBALS};
use crate::python::future::ALL_FEATURE_NAMES;
use crate::python::typing;
use crate::settings::Settings;
use crate::settings::{PythonVersion, Settings};

pub const GLOBAL_SCOPE_INDEX: usize = 0;

Expand Down Expand Up @@ -795,7 +795,9 @@ where
}

// pyupgrade
if self.settings.enabled.contains(&CheckCode::U002) {
if self.settings.enabled.contains(&CheckCode::U002)
&& self.settings.target_version >= PythonVersion::Py310
{
plugins::unnecessary_abspath(self, expr, func, args);
}

Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use ruff::logging::set_up_logging;
use ruff::message::Message;
use ruff::printer::{Printer, SerializationFormat};
use ruff::pyproject::{self, StrCheckCodePair};
use ruff::settings::CurrentSettings;
use ruff::settings::RawSettings;
use ruff::settings::{CurrentSettings, PythonVersion};
use ruff::settings::{FilePattern, PerFileIgnore, Settings};
use ruff::tell_user;

Expand Down Expand Up @@ -92,6 +92,9 @@ struct Cli {
/// Regular expression matching the name of dummy variables.
#[arg(long)]
dummy_variable_rgx: Option<Regex>,
/// The minimum Python version that should be supported.
#[arg(long)]
target_version: Option<PythonVersion>,
/// Round-trip auto-formatting.
// TODO(charlie): This should be a sub-command.
#[arg(long, hide = true)]
Expand Down Expand Up @@ -311,6 +314,9 @@ fn inner_main() -> Result<ExitCode> {
if !cli.extend_ignore.is_empty() {
settings.extend_ignore = cli.extend_ignore;
}
if let Some(target_version) = cli.target_version {
settings.target_version = target_version;
}
if let Some(dummy_variable_rgx) = cli.dummy_variable_rgx {
settings.dummy_variable_rgx = dummy_variable_rgx;
}
Expand Down
8 changes: 8 additions & 0 deletions src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Deserializer};

use crate::checks::CheckCode;
use crate::fs;
use crate::settings::PythonVersion;

pub fn load_config(pyproject: &Option<PathBuf>) -> Result<Config> {
match pyproject {
Expand Down Expand Up @@ -41,6 +42,7 @@ pub struct Config {
#[serde(default)]
pub per_file_ignores: Vec<StrCheckCodePair>,
pub dummy_variable_rgx: Option<String>,
pub target_version: Option<PythonVersion>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -189,6 +191,7 @@ mod tests {
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
})
})
);
Expand All @@ -213,6 +216,7 @@ line-length = 79
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
})
})
);
Expand All @@ -237,6 +241,7 @@ exclude = ["foo.py"]
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
})
})
);
Expand All @@ -261,6 +266,7 @@ select = ["E501"]
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
})
})
);
Expand All @@ -286,6 +292,7 @@ ignore = ["E501"]
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
})
})
);
Expand Down Expand Up @@ -354,6 +361,7 @@ other-attribute = 1
extend_ignore: vec![],
per_file_ignores: vec![],
dummy_variable_rgx: None,
target_version: None,
}
);

Expand Down
48 changes: 45 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::{anyhow, Result};
use glob::Pattern;
Expand All @@ -11,6 +13,38 @@ use crate::checks::{CheckCode, DEFAULT_CHECK_CODES};
use crate::fs;
use crate::pyproject::{load_config, StrCheckCodePair};

#[derive(Clone, Debug, PartialOrd, PartialEq, Eq, Serialize, Deserialize)]
pub enum PythonVersion {
Py33,
Py34,
Py35,
Py36,
Py37,
Py38,
Py39,
Py310,
Py311,
}

impl FromStr for PythonVersion {
type Err = anyhow::Error;

fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
"py33" => Ok(PythonVersion::Py33),
"py34" => Ok(PythonVersion::Py34),
"py35" => Ok(PythonVersion::Py35),
"py36" => Ok(PythonVersion::Py36),
"py37" => Ok(PythonVersion::Py37),
"py38" => Ok(PythonVersion::Py38),
"py39" => Ok(PythonVersion::Py39),
"py310" => Ok(PythonVersion::Py310),
"py311" => Ok(PythonVersion::Py311),
_ => Err(anyhow!("Unknown version: {}", string)),
}
}
}

#[derive(Debug, Clone, Hash)]
pub enum FilePattern {
Simple(&'static str),
Expand Down Expand Up @@ -63,6 +97,7 @@ pub struct RawSettings {
pub project_root: Option<PathBuf>,
pub pyproject: Option<PathBuf>,
pub select: Vec<CheckCode>,
pub target_version: PythonVersion,
}

static DEFAULT_EXCLUDE: Lazy<Vec<FilePattern>> = Lazy::new(|| {
Expand Down Expand Up @@ -104,6 +139,7 @@ impl RawSettings {
.map_err(|e| anyhow!("Invalid dummy-variable-rgx value: {e}"))?,
None => DEFAULT_DUMMY_VARIABLE_RGX.clone(),
},
target_version: config.target_version.unwrap_or(PythonVersion::Py310),
exclude: config
.exclude
.map(|paths| {
Expand All @@ -119,6 +155,9 @@ impl RawSettings {
.map(|path| FilePattern::from_user(path, &project_root))
.collect(),
extend_ignore: config.extend_ignore,
select: config
.select
.unwrap_or_else(|| DEFAULT_CHECK_CODES.to_vec()),
extend_select: config.extend_select,
ignore: config.ignore,
line_length: config.line_length.unwrap_or(88),
Expand All @@ -129,9 +168,6 @@ impl RawSettings {
.collect(),
project_root,
pyproject,
select: config
.select
.unwrap_or_else(|| DEFAULT_CHECK_CODES.to_vec()),
})
}
}
Expand All @@ -144,6 +180,7 @@ pub struct Settings {
pub extend_exclude: Vec<FilePattern>,
pub line_length: usize,
pub per_file_ignores: Vec<PerFileIgnore>,
pub target_version: PythonVersion,
}

impl Settings {
Expand All @@ -165,6 +202,7 @@ impl Settings {
extend_exclude: settings.extend_exclude,
line_length: settings.line_length,
per_file_ignores: settings.per_file_ignores,
target_version: PythonVersion::Py310,
}
}

Expand All @@ -176,6 +214,7 @@ impl Settings {
extend_exclude: vec![],
line_length: 88,
per_file_ignores: vec![],
target_version: PythonVersion::Py310,
}
}

Expand All @@ -187,6 +226,7 @@ impl Settings {
extend_exclude: vec![],
line_length: 88,
per_file_ignores: vec![],
target_version: PythonVersion::Py310,
}
}
}
Expand Down Expand Up @@ -241,6 +281,7 @@ pub struct CurrentSettings {
pub project_root: Option<PathBuf>,
pub pyproject: Option<PathBuf>,
pub select: Vec<CheckCode>,
pub target_version: PythonVersion,
}

impl CurrentSettings {
Expand All @@ -265,6 +306,7 @@ impl CurrentSettings {
project_root: settings.project_root,
pyproject: settings.pyproject,
select: settings.select,
target_version: settings.target_version,
}
}
}

0 comments on commit 0b9eda8

Please sign in to comment.