Skip to content

Commit ff11db6

Browse files
Add Python version support to ruff analyze CLI (#13426)
1 parent 2823487 commit ff11db6

File tree

7 files changed

+30
-6
lines changed

7 files changed

+30
-6
lines changed

Diff for: crates/red_knot_python_semantic/src/python_version.rs

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ impl TryFrom<(&str, &str)> for PythonVersion {
5454
}
5555
}
5656

57+
impl From<(u8, u8)> for PythonVersion {
58+
fn from(value: (u8, u8)) -> Self {
59+
let (major, minor) = value;
60+
Self { major, minor }
61+
}
62+
}
63+
5764
impl fmt::Display for PythonVersion {
5865
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5966
let PythonVersion { major, minor } = self;

Diff for: crates/ruff/src/args.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,23 @@ pub enum AnalyzeCommand {
152152
pub struct AnalyzeGraphCommand {
153153
/// List of files or directories to include.
154154
#[clap(help = "List of files or directories to include [default: .]")]
155-
pub files: Vec<PathBuf>,
155+
files: Vec<PathBuf>,
156156
/// The direction of the import map. By default, generates a dependency map, i.e., a map from
157157
/// file to files that it depends on. Use `--direction dependents` to generate a map from file
158158
/// to files that depend on it.
159159
#[clap(long, value_enum, default_value_t)]
160-
pub direction: Direction,
160+
direction: Direction,
161161
/// Attempt to detect imports from string literals.
162162
#[clap(long)]
163-
pub detect_string_imports: bool,
163+
detect_string_imports: bool,
164164
/// Enable preview mode. Use `--no-preview` to disable.
165165
#[arg(long, overrides_with("no_preview"))]
166166
preview: bool,
167167
#[clap(long, overrides_with("preview"), hide = true)]
168168
no_preview: bool,
169+
/// The minimum Python version that should be supported.
170+
#[arg(long, value_enum)]
171+
target_version: Option<PythonVersion>,
169172
}
170173

171174
// The `Parser` derive is for ruff_dev, for ruff `Args` would be sufficient
@@ -789,6 +792,7 @@ impl AnalyzeGraphCommand {
789792
None
790793
},
791794
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
795+
target_version: self.target_version,
792796
..ExplicitConfigOverrides::default()
793797
};
794798

Diff for: crates/ruff/src/commands/analyze_graph.rs

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ pub(crate) fn analyze_graph(
5959
.filter_map(|package| package.parent())
6060
.map(Path::to_path_buf)
6161
.filter_map(|path| SystemPathBuf::from_path_buf(path).ok()),
62+
pyproject_config
63+
.settings
64+
.analyze
65+
.target_version
66+
.as_tuple()
67+
.into(),
6268
)?;
6369

6470
// Create a cache for resolved globs.

Diff for: crates/ruff/tests/snapshots/show_settings__display_default_settings.snap

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ formatter.docstring_code_line_width = dynamic
391391
# Analyze Settings
392392
analyze.exclude = []
393393
analyze.preview = disabled
394+
analyze.target_version = Py37
394395
analyze.detect_string_imports = false
395396
analyze.extension = ExtensionMapping({})
396397
analyze.include_dependencies = {}

Diff for: crates/ruff_graph/src/db.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ pub struct ModuleDb {
1616

1717
impl ModuleDb {
1818
/// Initialize a [`ModuleDb`] from the given source root.
19-
pub fn from_src_roots(mut src_roots: impl Iterator<Item = SystemPathBuf>) -> Result<Self> {
19+
pub fn from_src_roots(
20+
mut src_roots: impl Iterator<Item = SystemPathBuf>,
21+
target_version: PythonVersion,
22+
) -> Result<Self> {
2023
let search_paths = {
2124
// Use the first source root.
2225
let src_root = src_roots
@@ -37,7 +40,7 @@ impl ModuleDb {
3740
Program::from_settings(
3841
&db,
3942
&ProgramSettings {
40-
target_version: PythonVersion::default(),
43+
target_version,
4144
search_paths,
4245
},
4346
)?;

Diff for: crates/ruff_graph/src/settings.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ruff_linter::display_settings;
2-
use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode};
2+
use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode, PythonVersion};
33
use ruff_macros::CacheKey;
44
use std::collections::BTreeMap;
55
use std::fmt;
@@ -9,6 +9,7 @@ use std::path::PathBuf;
99
pub struct AnalyzeSettings {
1010
pub exclude: FilePatternSet,
1111
pub preview: PreviewMode,
12+
pub target_version: PythonVersion,
1213
pub detect_string_imports: bool,
1314
pub include_dependencies: BTreeMap<PathBuf, (PathBuf, Vec<String>)>,
1415
pub extension: ExtensionMapping,
@@ -23,6 +24,7 @@ impl fmt::Display for AnalyzeSettings {
2324
fields = [
2425
self.exclude,
2526
self.preview,
27+
self.target_version | debug,
2628
self.detect_string_imports,
2729
self.extension | debug,
2830
self.include_dependencies | debug,

Diff for: crates/ruff_workspace/src/configuration.rs

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl Configuration {
217217
let analyze = AnalyzeSettings {
218218
exclude: FilePatternSet::try_from_iter(analyze.exclude.unwrap_or_default())?,
219219
preview: analyze_preview,
220+
target_version,
220221
extension: self.extension.clone().unwrap_or_default(),
221222
detect_string_imports: analyze
222223
.detect_string_imports

0 commit comments

Comments
 (0)