From 957a899736d2437b61ebae72a3e48f9f3c48cc3a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 20 Sep 2024 15:25:21 -0400 Subject: [PATCH] Add Python version support to ruff analyze CLI --- crates/red_knot_python_semantic/src/python_version.rs | 7 +++++++ crates/ruff/src/args.rs | 10 +++++++--- crates/ruff/src/commands/analyze_graph.rs | 6 ++++++ .../show_settings__display_default_settings.snap | 1 + crates/ruff_graph/src/db.rs | 7 +++++-- crates/ruff_graph/src/settings.rs | 4 +++- crates/ruff_workspace/src/configuration.rs | 1 + 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/red_knot_python_semantic/src/python_version.rs b/crates/red_knot_python_semantic/src/python_version.rs index 37aff2ce65ce3..58d15d76f900e 100644 --- a/crates/red_knot_python_semantic/src/python_version.rs +++ b/crates/red_knot_python_semantic/src/python_version.rs @@ -54,6 +54,13 @@ impl TryFrom<(&str, &str)> for PythonVersion { } } +impl From<(u8, u8)> for PythonVersion { + fn from(value: (u8, u8)) -> Self { + let (major, minor) = value; + Self { major, minor } + } +} + impl fmt::Display for PythonVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let PythonVersion { major, minor } = self; diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index abd6d1a4f1ab8..688514ce2cb4b 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -152,20 +152,23 @@ pub enum AnalyzeCommand { pub struct AnalyzeGraphCommand { /// List of files or directories to include. #[clap(help = "List of files or directories to include [default: .]")] - pub files: Vec, + files: Vec, /// The direction of the import map. By default, generates a dependency map, i.e., a map from /// file to files that it depends on. Use `--direction dependents` to generate a map from file /// to files that depend on it. #[clap(long, value_enum, default_value_t)] - pub direction: Direction, + direction: Direction, /// Attempt to detect imports from string literals. #[clap(long)] - pub detect_string_imports: bool, + detect_string_imports: bool, /// Enable preview mode. Use `--no-preview` to disable. #[arg(long, overrides_with("no_preview"))] preview: bool, #[clap(long, overrides_with("preview"), hide = true)] no_preview: bool, + /// The minimum Python version that should be supported. + #[arg(long, value_enum)] + target_version: Option, } // The `Parser` derive is for ruff_dev, for ruff `Args` would be sufficient @@ -789,6 +792,7 @@ impl AnalyzeGraphCommand { None }, preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from), + target_version: self.target_version, ..ExplicitConfigOverrides::default() }; diff --git a/crates/ruff/src/commands/analyze_graph.rs b/crates/ruff/src/commands/analyze_graph.rs index 33cc96d6b17cf..ce10ed69d3995 100644 --- a/crates/ruff/src/commands/analyze_graph.rs +++ b/crates/ruff/src/commands/analyze_graph.rs @@ -59,6 +59,12 @@ pub(crate) fn analyze_graph( .filter_map(|package| package.parent()) .map(Path::to_path_buf) .filter_map(|path| SystemPathBuf::from_path_buf(path).ok()), + pyproject_config + .settings + .analyze + .target_version + .as_tuple() + .into(), )?; // Create a cache for resolved globs. diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index 41be1cd7e746b..d6e12085fe0f4 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -391,6 +391,7 @@ formatter.docstring_code_line_width = dynamic # Analyze Settings analyze.exclude = [] analyze.preview = disabled +analyze.target_version = Py37 analyze.detect_string_imports = false analyze.extension = ExtensionMapping({}) analyze.include_dependencies = {} diff --git a/crates/ruff_graph/src/db.rs b/crates/ruff_graph/src/db.rs index 9e786eee0549b..5b3e660248c63 100644 --- a/crates/ruff_graph/src/db.rs +++ b/crates/ruff_graph/src/db.rs @@ -16,7 +16,10 @@ pub struct ModuleDb { impl ModuleDb { /// Initialize a [`ModuleDb`] from the given source root. - pub fn from_src_roots(mut src_roots: impl Iterator) -> Result { + pub fn from_src_roots( + mut src_roots: impl Iterator, + target_version: PythonVersion, + ) -> Result { let search_paths = { // Use the first source root. let src_root = src_roots @@ -37,7 +40,7 @@ impl ModuleDb { Program::from_settings( &db, &ProgramSettings { - target_version: PythonVersion::default(), + target_version, search_paths, }, )?; diff --git a/crates/ruff_graph/src/settings.rs b/crates/ruff_graph/src/settings.rs index 6cc7365208e82..a91e24f2e3ed5 100644 --- a/crates/ruff_graph/src/settings.rs +++ b/crates/ruff_graph/src/settings.rs @@ -1,5 +1,5 @@ use ruff_linter::display_settings; -use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode}; +use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode, PythonVersion}; use ruff_macros::CacheKey; use std::collections::BTreeMap; use std::fmt; @@ -9,6 +9,7 @@ use std::path::PathBuf; pub struct AnalyzeSettings { pub exclude: FilePatternSet, pub preview: PreviewMode, + pub target_version: PythonVersion, pub detect_string_imports: bool, pub include_dependencies: BTreeMap)>, pub extension: ExtensionMapping, @@ -23,6 +24,7 @@ impl fmt::Display for AnalyzeSettings { fields = [ self.exclude, self.preview, + self.target_version | debug, self.detect_string_imports, self.extension | debug, self.include_dependencies | debug, diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 86e556fb03030..7628fb96e233c 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -217,6 +217,7 @@ impl Configuration { let analyze = AnalyzeSettings { exclude: FilePatternSet::try_from_iter(analyze.exclude.unwrap_or_default())?, preview: analyze_preview, + target_version, extension: self.extension.clone().unwrap_or_default(), detect_string_imports: analyze .detect_string_imports