Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python version support to ruff analyze CLI #13426

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/red_knot_python_semantic/src/python_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
files: Vec<PathBuf>,
/// 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<PythonVersion>,
}

// The `Parser` derive is for ruff_dev, for ruff `Args` would be sufficient
Expand Down Expand Up @@ -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()
};

Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/src/commands/analyze_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
7 changes: 5 additions & 2 deletions crates/ruff_graph/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = SystemPathBuf>) -> Result<Self> {
pub fn from_src_roots(
mut src_roots: impl Iterator<Item = SystemPathBuf>,
target_version: PythonVersion,
) -> Result<Self> {
let search_paths = {
// Use the first source root.
let src_root = src_roots
Expand All @@ -37,7 +40,7 @@ impl ModuleDb {
Program::from_settings(
&db,
&ProgramSettings {
target_version: PythonVersion::default(),
target_version,
search_paths,
},
)?;
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_graph/src/settings.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<PathBuf, (PathBuf, Vec<String>)>,
pub extension: ExtensionMapping,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading