Add support for specifying minimum dots in detected string imports (#19538)

## Summary

Defaults to requiring two dots, which matches the Pants default.
This commit is contained in:
Charlie Marsh 2025-07-24 15:48:23 -04:00 committed by GitHub
parent d77b7312b0
commit d9cab4d242
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 129 additions and 42 deletions

View file

@ -20,7 +20,7 @@ use strum::IntoEnumIterator;
use ruff_cache::cache_dir;
use ruff_formatter::IndentStyle;
use ruff_graph::{AnalyzeSettings, Direction};
use ruff_graph::{AnalyzeSettings, Direction, StringImports};
use ruff_linter::line_width::{IndentWidth, LineLength};
use ruff_linter::registry::{INCOMPATIBLE_CODES, Rule, RuleNamespace, RuleSet};
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
@ -222,9 +222,14 @@ impl Configuration {
preview: analyze_preview,
target_version,
extension: self.extension.clone().unwrap_or_default(),
detect_string_imports: analyze
.detect_string_imports
.unwrap_or(analyze_defaults.detect_string_imports),
string_imports: StringImports {
enabled: analyze
.detect_string_imports
.unwrap_or(analyze_defaults.string_imports.enabled),
min_dots: analyze
.string_imports_min_dots
.unwrap_or(analyze_defaults.string_imports.min_dots),
},
include_dependencies: analyze
.include_dependencies
.unwrap_or(analyze_defaults.include_dependencies),
@ -1271,6 +1276,7 @@ pub struct AnalyzeConfiguration {
pub direction: Option<Direction>,
pub detect_string_imports: Option<bool>,
pub string_imports_min_dots: Option<usize>,
pub include_dependencies: Option<BTreeMap<PathBuf, (PathBuf, Vec<String>)>>,
}
@ -1289,6 +1295,7 @@ impl AnalyzeConfiguration {
preview: options.preview.map(PreviewMode::from),
direction: options.direction,
detect_string_imports: options.detect_string_imports,
string_imports_min_dots: options.string_imports_min_dots,
include_dependencies: options.include_dependencies.map(|dependencies| {
dependencies
.into_iter()
@ -1307,6 +1314,9 @@ impl AnalyzeConfiguration {
preview: self.preview.or(config.preview),
direction: self.direction.or(config.direction),
detect_string_imports: self.detect_string_imports.or(config.detect_string_imports),
string_imports_min_dots: self
.string_imports_min_dots
.or(config.string_imports_min_dots),
include_dependencies: self.include_dependencies.or(config.include_dependencies),
}
}

View file

@ -3843,6 +3843,12 @@ pub struct AnalyzeOptions {
"#
)]
pub detect_string_imports: Option<bool>,
/// The minimum number of dots in a string to consider it a valid import.
///
/// This setting is only relevant when [`detect-string-imports`](#detect-string-imports) is enabled.
/// For example, if this is set to `2`, then only strings with at least two dots (e.g., `"path.to.module"`)
/// would be considered valid imports.
pub string_imports_min_dots: Option<usize>,
/// A map from file path to the list of Python or non-Python file paths or globs that should be
/// considered dependencies of that file, regardless of whether relevant imports are detected.
#[option(