diff --git a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_preview_enabled.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_preview_enabled.snap index 5868ceb04f..2f1158bc83 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_preview_enabled.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_preview_enabled.snap @@ -146,14 +146,18 @@ linter.flake8_import_conventions.aliases = { pandas = pd, panel = pn, plotly.express = px, + plotly.graph_objects = go, polars = pl, pyarrow = pa, seaborn = sns, + statsmodels.api = sm, tensorflow = tf, tkinter = tk, xml.etree.ElementTree = ET, } -linter.flake8_import_conventions.banned_aliases = {} +linter.flake8_import_conventions.banned_aliases = { + geopandas = [gpd], +} linter.flake8_import_conventions.banned_from = [] linter.flake8_pytest_style.fixture_parentheses = false linter.flake8_pytest_style.parametrize_names_type = tuple diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/defaults.py b/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/defaults.py index 0342d42034..52a1163e5f 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/defaults.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/defaults.py @@ -30,3 +30,17 @@ def conventional_aliases(): import seaborn as sns import tkinter as tk import networkx as nx + + +# ICN001: plotly.graph_objects should be imported as go +import plotly.graph_objects # should require alias +import plotly.graph_objects as go # ok + +# ICN001: statsmodels.api should be imported as sm +import statsmodels.api # should require alias +import statsmodels.api as sm # ok + +# ICN002: geopandas should not be imported as gpd +import geopandas as gpd # banned +import geopandas # ok +import geopandas as gdf # ok diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs b/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs index 386e35035c..073fe18bb6 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs @@ -9,11 +9,11 @@ mod tests { use anyhow::Result; use rustc_hash::{FxHashMap, FxHashSet}; - use crate::assert_diagnostics; use crate::registry::Rule; use crate::rules::flake8_import_conventions::settings::{BannedAliases, default_aliases}; - use crate::settings::LinterSettings; + use crate::settings::{LinterSettings, types::PreviewMode}; use crate::test::test_path; + use crate::{assert_diagnostics, assert_diagnostics_diff}; #[test] fn defaults() -> Result<()> { @@ -25,6 +25,28 @@ mod tests { Ok(()) } + #[test] + fn defaults_preview() -> Result<()> { + assert_diagnostics_diff!( + Path::new("flake8_import_conventions/defaults.py"), + &LinterSettings { + flake8_import_conventions: super::settings::Settings::new(PreviewMode::Disabled), + ..LinterSettings::for_rules([ + Rule::UnconventionalImportAlias, + Rule::BannedImportAlias + ]) + }, + &LinterSettings { + flake8_import_conventions: super::settings::Settings::new(PreviewMode::Enabled), + ..LinterSettings::for_rules([ + Rule::UnconventionalImportAlias, + Rule::BannedImportAlias + ]) + }, + ); + Ok(()) + } + #[test] fn custom() -> Result<()> { let mut aliases = default_aliases(); diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs index f00406de4a..600ab70baa 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; use ruff_macros::CacheKey; use crate::display_settings; +use crate::settings::types::PreviewMode; const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("altair", "alt"), @@ -17,17 +18,20 @@ const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("numpy", "np"), ("numpy.typing", "npt"), ("pandas", "pd"), + ("plotly.express", "px"), ("seaborn", "sns"), ("tensorflow", "tf"), ("tkinter", "tk"), ("holoviews", "hv"), ("panel", "pn"), - ("plotly.express", "px"), ("polars", "pl"), ("pyarrow", "pa"), ("xml.etree.ElementTree", "ET"), ]; +const PREVIEW_ALIASES: &[(&str, &str)] = + &[("plotly.graph_objects", "go"), ("statsmodels.api", "sm")]; + #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, CacheKey)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -73,6 +77,38 @@ pub fn default_aliases() -> FxHashMap { .collect::>() } +pub fn preview_aliases() -> FxHashMap { + PREVIEW_ALIASES + .iter() + .map(|(k, v)| ((*k).to_string(), (*v).to_string())) + .collect::>() +} + +pub fn preview_banned_aliases() -> FxHashMap { + FxHashMap::from_iter([( + "geopandas".to_string(), + BannedAliases::from_iter(["gpd".to_string()]), + )]) +} + +impl Settings { + pub fn new(preview: PreviewMode) -> Self { + let mut aliases = default_aliases(); + let mut banned_aliases = FxHashMap::default(); + + if preview.is_enabled() { + aliases.extend(preview_aliases()); + banned_aliases.extend(preview_banned_aliases()); + } + + Self { + aliases, + banned_aliases, + banned_from: FxHashSet::default(), + } + } +} + impl Default for Settings { fn default() -> Self { Self { diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults_preview.snap b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults_preview.snap new file mode 100644 index 0000000000..b56ab7fca7 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults_preview.snap @@ -0,0 +1,47 @@ +--- +source: crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +--- +--- Linter settings --- ++ plotly.graph_objects = go, ++ statsmodels.api = sm, +-linter.flake8_import_conventions.banned_aliases = {} ++linter.flake8_import_conventions.banned_aliases = { ++ geopandas = [gpd], ++} + +--- Summary --- +Removed: 0 +Added: 3 + +--- Added --- +ICN001 `plotly.graph_objects` should be imported as `go` + --> defaults.py:36:8 + | +35 | # ICN001: plotly.graph_objects should be imported as go +36 | import plotly.graph_objects # should require alias + | ^^^^^^^^^^^^^^^^^^^^ +37 | import plotly.graph_objects as go # ok + | +help: Alias `plotly.graph_objects` to `go` + + +ICN001 `statsmodels.api` should be imported as `sm` + --> defaults.py:40:8 + | +39 | # ICN001: statsmodels.api should be imported as sm +40 | import statsmodels.api # should require alias + | ^^^^^^^^^^^^^^^ +41 | import statsmodels.api as sm # ok + | +help: Alias `statsmodels.api` to `sm` + + +ICN002 `geopandas` should not be imported as `gpd` + --> defaults.py:44:1 + | +43 | # ICN002: geopandas should not be imported as gpd +44 | import geopandas as gpd # banned + | ^^^^^^^^^^^^^^^^^^^^^^^ +45 | import geopandas # ok +46 | import geopandas as gdf # ok + | diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index bf50749a45..3ecc4f098d 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -252,9 +252,11 @@ impl Configuration { .unwrap_or_default(); let flake8_import_conventions = lint .flake8_import_conventions - .map(Flake8ImportConventionsOptions::try_into_settings) + .map(|options| options.try_into_settings(lint_preview)) .transpose()? - .unwrap_or_default(); + .unwrap_or_else(|| { + ruff_linter::rules::flake8_import_conventions::settings::Settings::new(lint_preview) + }); conflicting_import_settings(&isort, &flake8_import_conventions)?; diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 472b0e66f4..1b767b4369 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -28,7 +28,7 @@ use ruff_linter::rules::{ pycodestyle, pydoclint, pydocstyle, pyflakes, pylint, pyupgrade, ruff, }; use ruff_linter::settings::types::{ - IdentifierPattern, OutputFormat, PythonVersion, RequiredVersion, + IdentifierPattern, OutputFormat, PreviewMode, PythonVersion, RequiredVersion, }; use ruff_linter::{RuleSelector, warn_user_once}; use ruff_macros::{CombineOptions, OptionsMetadata}; @@ -1656,6 +1656,7 @@ impl<'de> Deserialize<'de> for Alias { impl Flake8ImportConventionsOptions { pub fn try_into_settings( self, + preview: PreviewMode, ) -> anyhow::Result { let mut aliases: FxHashMap = match self.aliases { Some(options_aliases) => options_aliases @@ -1672,6 +1673,11 @@ impl Flake8ImportConventionsOptions { ); } + // Merge preview aliases if preview mode is enabled + if preview.is_enabled() { + aliases.extend(flake8_import_conventions::settings::preview_aliases()); + } + let mut normalized_aliases: FxHashMap = FxHashMap::default(); for (module, alias) in aliases { let normalized_alias = alias.nfkc().collect::(); @@ -1683,9 +1689,16 @@ impl Flake8ImportConventionsOptions { normalized_aliases.insert(module, normalized_alias); } + let mut banned_aliases = self.banned_aliases.unwrap_or_default(); + + // Merge preview banned aliases if preview mode is enabled + if preview.is_enabled() { + banned_aliases.extend(flake8_import_conventions::settings::preview_banned_aliases()); + } + Ok(flake8_import_conventions::settings::Settings { aliases: normalized_aliases, - banned_aliases: self.banned_aliases.unwrap_or_default(), + banned_aliases, banned_from: self.banned_from.unwrap_or_default(), }) }