From 3633bcfed84f3d13548b39fdde28666a0db62993 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 10 Nov 2025 23:23:55 -0500 Subject: [PATCH 1/6] fix-21300 --- .../missing_conventions.py | 15 +++++++++ .../rules/flake8_import_conventions/mod.rs | 16 ++++++++++ .../flake8_import_conventions/settings.rs | 13 ++++++-- ...nventions__tests__missing_conventions.snap | 32 +++++++++++++++++++ crates/ruff_workspace/src/options.rs | 8 +++-- 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py create mode 100644 crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py b/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py new file mode 100644 index 0000000000..ec6a0a93ba --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py @@ -0,0 +1,15 @@ +# Test cases for missing conventions from flake8-import-conventions + +# 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..9541e757d1 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs @@ -204,4 +204,20 @@ mod tests { assert_diagnostics!(diagnostics); Ok(()) } + + #[test] + fn missing_conventions() -> Result<()> { + let diagnostics = test_path( + Path::new("flake8_import_conventions/missing_conventions.py"), + &LinterSettings { + flake8_import_conventions: super::settings::Settings::default(), + ..LinterSettings::for_rules([ + Rule::UnconventionalImportAlias, + Rule::BannedImportAlias, + ]) + }, + )?; + assert_diagnostics!(diagnostics); + Ok(()) + } } 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..c8e4f947d0 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs @@ -17,12 +17,14 @@ const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("numpy", "np"), ("numpy.typing", "npt"), ("pandas", "pd"), + ("plotly.express", "px"), + ("plotly.graph_objects", "go"), ("seaborn", "sns"), + ("statsmodels.api", "sm"), ("tensorflow", "tf"), ("tkinter", "tk"), ("holoviews", "hv"), ("panel", "pn"), - ("plotly.express", "px"), ("polars", "pl"), ("pyarrow", "pa"), ("xml.etree.ElementTree", "ET"), @@ -73,11 +75,18 @@ pub fn default_aliases() -> FxHashMap { .collect::>() } +pub fn default_banned_aliases() -> FxHashMap { + FxHashMap::from_iter([( + "geopandas".to_string(), + BannedAliases::from_iter(["gpd".to_string()]), + )]) +} + impl Default for Settings { fn default() -> Self { Self { aliases: default_aliases(), - banned_aliases: FxHashMap::default(), + banned_aliases: default_banned_aliases(), banned_from: FxHashSet::default(), } } diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap new file mode 100644 index 0000000000..a41a6057a5 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap @@ -0,0 +1,32 @@ +--- +source: crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +--- +ICN001 `plotly.graph_objects` should be imported as `go` + --> missing_conventions.py:4:8 + | +3 | # ICN001: plotly.graph_objects should be imported as go +4 | import plotly.graph_objects # should require alias + | ^^^^^^^^^^^^^^^^^^^^ +5 | import plotly.graph_objects as go # ok + | +help: Alias `plotly.graph_objects` to `go` + +ICN001 `statsmodels.api` should be imported as `sm` + --> missing_conventions.py:8:8 + | +7 | # ICN001: statsmodels.api should be imported as sm +8 | import statsmodels.api # should require alias + | ^^^^^^^^^^^^^^^ +9 | import statsmodels.api as sm # ok + | +help: Alias `statsmodels.api` to `sm` + +ICN002 `geopandas` should not be imported as `gpd` + --> missing_conventions.py:12:1 + | +11 | # ICN002: geopandas should not be imported as gpd +12 | import geopandas as gpd # banned + | ^^^^^^^^^^^^^^^^^^^^^^^ +13 | import geopandas # ok +14 | import geopandas as gdf # ok + | diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 708d6dcf0b..c2e8f53a98 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -1534,7 +1534,7 @@ pub struct Flake8ImportConventionsOptions { /// The conventional aliases for imports. These aliases can be extended by /// the [`extend-aliases`](#lint_flake8-import-conventions_extend-aliases) option. #[option( - default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "numpy.typing": "npt", "pandas": "pd", "seaborn": "sns", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "plotly.express": "px", "polars": "pl", "pyarrow": "pa", "xml.etree.ElementTree": "ET"}"#, + default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "numpy.typing": "npt", "pandas": "pd", "plotly.express": "px", "plotly.graph_objects": "go", "seaborn": "sns", "statsmodels.api": "sm", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "polars": "pl", "pyarrow": "pa", "xml.etree.ElementTree": "ET"}"#, value_type = "dict[str, str]", scope = "aliases", example = r#" @@ -1564,7 +1564,7 @@ pub struct Flake8ImportConventionsOptions { /// A mapping from module to its banned import aliases. #[option( - default = r#"{}"#, + default = r#"{"geopandas": ["gpd"]}"#, value_type = "dict[str, list[str]]", scope = "banned-aliases", example = r#" @@ -1685,7 +1685,9 @@ impl Flake8ImportConventionsOptions { Ok(flake8_import_conventions::settings::Settings { aliases: normalized_aliases, - banned_aliases: self.banned_aliases.unwrap_or_default(), + banned_aliases: self + .banned_aliases + .unwrap_or_else(flake8_import_conventions::settings::default_banned_aliases), banned_from: self.banned_from.unwrap_or_default(), }) } From 0c44086746cbf6176a7682884a38edc5285ae256 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 10 Nov 2025 23:37:51 -0500 Subject: [PATCH 2/6] Update remaining snaps --- ...i__lint__requires_python_extend_from_shared_config.snap | 7 +++++-- .../cli/snapshots/cli__lint__requires_python_no_tool.snap | 7 +++++-- ...cli__lint__requires_python_no_tool_preview_enabled.snap | 7 +++++-- ...t__requires_python_no_tool_target_version_override.snap | 7 +++++-- .../cli__lint__requires_python_pyproject_toml_above.snap | 7 +++++-- ...nt__requires_python_pyproject_toml_above_with_tool.snap | 7 +++++-- .../cli__lint__requires_python_ruff_toml_above.snap | 6 +++++- ...lint__requires_python_ruff_toml_no_target_fallback.snap | 7 +++++-- .../snapshots/show_settings__display_default_settings.snap | 6 +++++- 9 files changed, 45 insertions(+), 16 deletions(-) diff --git a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap index 62bde10fe3..7a67abe52e 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap @@ -9,7 +9,6 @@ info: - concise - "--show-settings" - test.py -snapshot_kind: text --- success: true exit_code: 0 @@ -143,14 +142,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/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap index a7b4b2c978..455f58bec9 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap @@ -12,7 +12,6 @@ info: - UP007 - test.py - "-" -snapshot_kind: text --- success: true exit_code: 0 @@ -145,14 +144,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/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 4e33c123a4..53577860d8 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 @@ -13,7 +13,6 @@ info: - UP007 - test.py - "-" -snapshot_kind: text --- success: true exit_code: 0 @@ -147,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/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap index 929943558e..00ff41dc62 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap @@ -14,7 +14,6 @@ info: - py310 - test.py - "-" -snapshot_kind: text --- success: true exit_code: 0 @@ -147,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/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap index 291cb62d6e..5752bc3296 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap @@ -11,7 +11,6 @@ info: - "--select" - UP007 - foo/test.py -snapshot_kind: text --- success: true exit_code: 0 @@ -144,14 +143,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/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap index d9f9402895..76b34de492 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap @@ -11,7 +11,6 @@ info: - "--select" - UP007 - foo/test.py -snapshot_kind: text --- success: true exit_code: 0 @@ -144,14 +143,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/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap index e9ca7bd400..6c23d8774f 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap @@ -142,14 +142,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/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap index a3f9343314..19207ecaf4 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap @@ -9,7 +9,6 @@ info: - concise - test.py - "--show-settings" -snapshot_kind: text --- success: true exit_code: 0 @@ -143,14 +142,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/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index 28a6607816..04fdec5c5e 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -255,14 +255,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 From 3b468dad40a82b7c81603709212bd3d0d684e155 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 10 Nov 2025 23:51:21 -0500 Subject: [PATCH 3/6] Update cli__lint__requires_python_ruff_toml_above-2.snap --- .../cli__lint__requires_python_ruff_toml_above-2.snap | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap index da3eb66afc..a40d8c27e9 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap @@ -142,14 +142,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 From 7cb119653540cc3f4a8fdf96218945ff2e5f7ae0 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 11 Nov 2025 16:34:06 -0500 Subject: [PATCH 4/6] Move tests to `defaults.py` --- .../flake8_import_conventions/defaults.py | 14 ++++++++ .../missing_conventions.py | 15 --------- .../rules/flake8_import_conventions/mod.rs | 24 ++++---------- ...8_import_conventions__tests__defaults.snap | 30 +++++++++++++++++ ...nventions__tests__missing_conventions.snap | 32 ------------------- 5 files changed, 51 insertions(+), 64 deletions(-) delete mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py delete mode 100644 crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap 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/resources/test/fixtures/flake8_import_conventions/missing_conventions.py b/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py deleted file mode 100644 index ec6a0a93ba..0000000000 --- a/crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py +++ /dev/null @@ -1,15 +0,0 @@ -# Test cases for missing conventions from flake8-import-conventions - -# 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 9541e757d1..96c6b00f65 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs @@ -19,7 +19,13 @@ mod tests { fn defaults() -> Result<()> { let diagnostics = test_path( Path::new("flake8_import_conventions/defaults.py"), - &LinterSettings::for_rule(Rule::UnconventionalImportAlias), + &LinterSettings { + flake8_import_conventions: super::settings::Settings::default(), + ..LinterSettings::for_rules([ + Rule::UnconventionalImportAlias, + Rule::BannedImportAlias, + ]) + }, )?; assert_diagnostics!(diagnostics); Ok(()) @@ -204,20 +210,4 @@ mod tests { assert_diagnostics!(diagnostics); Ok(()) } - - #[test] - fn missing_conventions() -> Result<()> { - let diagnostics = test_path( - Path::new("flake8_import_conventions/missing_conventions.py"), - &LinterSettings { - flake8_import_conventions: super::settings::Settings::default(), - ..LinterSettings::for_rules([ - Rule::UnconventionalImportAlias, - Rule::BannedImportAlias, - ]) - }, - )?; - assert_diagnostics!(diagnostics); - Ok(()) - } } diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults.snap b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults.snap index 52708a8b47..b540824e9b 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults.snap +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__defaults.snap @@ -277,3 +277,33 @@ help: Alias `networkx` to `nx` 24 | 25 | def conventional_aliases(): note: This is an unsafe fix and may change runtime behavior + +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_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap b/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap deleted file mode 100644 index a41a6057a5..0000000000 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/snapshots/ruff_linter__rules__flake8_import_conventions__tests__missing_conventions.snap +++ /dev/null @@ -1,32 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs ---- -ICN001 `plotly.graph_objects` should be imported as `go` - --> missing_conventions.py:4:8 - | -3 | # ICN001: plotly.graph_objects should be imported as go -4 | import plotly.graph_objects # should require alias - | ^^^^^^^^^^^^^^^^^^^^ -5 | import plotly.graph_objects as go # ok - | -help: Alias `plotly.graph_objects` to `go` - -ICN001 `statsmodels.api` should be imported as `sm` - --> missing_conventions.py:8:8 - | -7 | # ICN001: statsmodels.api should be imported as sm -8 | import statsmodels.api # should require alias - | ^^^^^^^^^^^^^^^ -9 | import statsmodels.api as sm # ok - | -help: Alias `statsmodels.api` to `sm` - -ICN002 `geopandas` should not be imported as `gpd` - --> missing_conventions.py:12:1 - | -11 | # ICN002: geopandas should not be imported as gpd -12 | import geopandas as gpd # banned - | ^^^^^^^^^^^^^^^^^^^^^^^ -13 | import geopandas # ok -14 | import geopandas as gdf # ok - | From 17bbb535de9309a4e31778a8637bb7d96ad174cf Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 13 Nov 2025 20:34:25 -0500 Subject: [PATCH 5/6] Apply preview --- crates/ruff_linter/src/preview.rs | 5 ++++ .../rules/flake8_import_conventions/mod.rs | 16 ++++++------- .../rules/banned_import_alias.rs | 15 ++++++++++-- .../rules/unconventional_import_alias.rs | 24 +++++++++++++++---- .../flake8_import_conventions/settings.rs | 16 +++++++++---- crates/ruff_workspace/src/options.rs | 6 ++--- 6 files changed, 61 insertions(+), 21 deletions(-) diff --git a/crates/ruff_linter/src/preview.rs b/crates/ruff_linter/src/preview.rs index 836ba4feea..342f35ae93 100644 --- a/crates/ruff_linter/src/preview.rs +++ b/crates/ruff_linter/src/preview.rs @@ -254,6 +254,11 @@ pub(crate) const fn is_b006_check_guaranteed_mutable_expr_enabled( settings.preview.is_enabled() } +// https://github.com/astral-sh/ruff/pull/21373 +pub(crate) const fn is_import_conventions_preview_enabled(settings: &LinterSettings) -> bool { + settings.preview.is_enabled() +} + // github.com/astral-sh/ruff/issues/20004 pub(crate) const fn is_b006_unsafe_fix_preserve_assignment_expr_enabled( settings: &LinterSettings, 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 96c6b00f65..aaeae94ed4 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs @@ -12,20 +12,20 @@ mod tests { 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; #[test] fn defaults() -> Result<()> { + let mut settings = LinterSettings { + flake8_import_conventions: super::settings::Settings::default(), + ..LinterSettings::for_rules([Rule::UnconventionalImportAlias, Rule::BannedImportAlias]) + }; + // Enable preview mode to test preview-only conventions + settings.preview = PreviewMode::Enabled; let diagnostics = test_path( Path::new("flake8_import_conventions/defaults.py"), - &LinterSettings { - flake8_import_conventions: super::settings::Settings::default(), - ..LinterSettings::for_rules([ - Rule::UnconventionalImportAlias, - Rule::BannedImportAlias, - ]) - }, + &settings, )?; assert_diagnostics!(diagnostics); Ok(()) diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/rules/banned_import_alias.rs b/crates/ruff_linter/src/rules/flake8_import_conventions/rules/banned_import_alias.rs index 080e5f3977..b64261437a 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/rules/banned_import_alias.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/rules/banned_import_alias.rs @@ -6,7 +6,8 @@ use ruff_text_size::Ranged; use crate::Violation; use crate::checkers::ast::Checker; -use crate::rules::flake8_import_conventions::settings::BannedAliases; +use crate::preview::is_import_conventions_preview_enabled; +use crate::rules::flake8_import_conventions::settings::{BannedAliases, preview_banned_aliases}; /// ## What it does /// Checks for imports that use non-standard naming conventions, like @@ -56,7 +57,17 @@ pub(crate) fn banned_import_alias( asname: &str, banned_conventions: &FxHashMap, ) { - if let Some(banned_aliases) = banned_conventions.get(name) { + // Merge preview banned aliases if preview mode is enabled + let banned_aliases = if is_import_conventions_preview_enabled(checker.settings()) { + banned_conventions.get(name).cloned().or_else(|| { + let preview_banned = preview_banned_aliases(); + preview_banned.get(name).cloned() + }) + } else { + banned_conventions.get(name).cloned() + }; + + if let Some(banned_aliases) = banned_aliases.as_ref() { if banned_aliases .iter() .any(|banned_alias| banned_alias == asname) diff --git a/crates/ruff_linter/src/rules/flake8_import_conventions/rules/unconventional_import_alias.rs b/crates/ruff_linter/src/rules/flake8_import_conventions/rules/unconventional_import_alias.rs index 6827e99b93..0d2f1b094d 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/rules/unconventional_import_alias.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/rules/unconventional_import_alias.rs @@ -5,6 +5,8 @@ use ruff_python_semantic::{Binding, Imported}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; +use crate::preview::is_import_conventions_preview_enabled; +use crate::rules::flake8_import_conventions::settings::preview_aliases; use crate::{Fix, FixAvailability, Violation}; use crate::renamer::Renamer; @@ -66,12 +68,26 @@ pub(crate) fn unconventional_import_alias( return; }; let qualified_name = import.qualified_name().to_string(); - let Some(expected_alias) = conventions.get(qualified_name.as_str()) else { + + // Merge preview conventions if preview mode is enabled + let expected_alias = if is_import_conventions_preview_enabled(checker.settings()) { + conventions + .get(qualified_name.as_str()) + .cloned() + .or_else(|| { + let preview_aliases_map = preview_aliases(); + preview_aliases_map.get(qualified_name.as_str()).cloned() + }) + } else { + conventions.get(qualified_name.as_str()).cloned() + }; + + let Some(expected_alias) = expected_alias else { return; }; let name = binding.name(checker.source()); - if name == expected_alias { + if name == expected_alias.as_str() { return; } @@ -83,12 +99,12 @@ pub(crate) fn unconventional_import_alias( binding.range(), ); if !import.is_submodule_import() { - if checker.semantic().is_available(expected_alias) { + if checker.semantic().is_available(&expected_alias) { diagnostic.try_set_fix(|| { let scope = &checker.semantic().scopes[binding.scope]; let (edit, rest) = Renamer::rename( name, - expected_alias, + &expected_alias, scope, checker.semantic(), checker.stylist(), 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 c8e4f947d0..53023c5e55 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs @@ -18,9 +18,7 @@ const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("numpy.typing", "npt"), ("pandas", "pd"), ("plotly.express", "px"), - ("plotly.graph_objects", "go"), ("seaborn", "sns"), - ("statsmodels.api", "sm"), ("tensorflow", "tf"), ("tkinter", "tk"), ("holoviews", "hv"), @@ -30,6 +28,9 @@ const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("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))] @@ -75,7 +76,14 @@ pub fn default_aliases() -> FxHashMap { .collect::>() } -pub fn default_banned_aliases() -> FxHashMap { +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()]), @@ -86,7 +94,7 @@ impl Default for Settings { fn default() -> Self { Self { aliases: default_aliases(), - banned_aliases: default_banned_aliases(), + banned_aliases: FxHashMap::default(), banned_from: FxHashSet::default(), } } diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index c2e8f53a98..d90641cd30 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -1534,7 +1534,7 @@ pub struct Flake8ImportConventionsOptions { /// The conventional aliases for imports. These aliases can be extended by /// the [`extend-aliases`](#lint_flake8-import-conventions_extend-aliases) option. #[option( - default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "numpy.typing": "npt", "pandas": "pd", "plotly.express": "px", "plotly.graph_objects": "go", "seaborn": "sns", "statsmodels.api": "sm", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "polars": "pl", "pyarrow": "pa", "xml.etree.ElementTree": "ET"}"#, + default = r#"{"altair": "alt", "matplotlib": "mpl", "matplotlib.pyplot": "plt", "numpy": "np", "numpy.typing": "npt", "pandas": "pd", "plotly.express": "px", "seaborn": "sns", "tensorflow": "tf", "tkinter": "tk", "holoviews": "hv", "panel": "pn", "polars": "pl", "pyarrow": "pa", "xml.etree.ElementTree": "ET"}"#, value_type = "dict[str, str]", scope = "aliases", example = r#" @@ -1564,7 +1564,7 @@ pub struct Flake8ImportConventionsOptions { /// A mapping from module to its banned import aliases. #[option( - default = r#"{"geopandas": ["gpd"]}"#, + default = r#"{}"#, value_type = "dict[str, list[str]]", scope = "banned-aliases", example = r#" @@ -1687,7 +1687,7 @@ impl Flake8ImportConventionsOptions { aliases: normalized_aliases, banned_aliases: self .banned_aliases - .unwrap_or_else(flake8_import_conventions::settings::default_banned_aliases), + .unwrap_or_else(FxHashMap::default), banned_from: self.banned_from.unwrap_or_default(), }) } From 924fd616273c028ea08a255ed1a4778b950d4712 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 13 Nov 2025 21:11:42 -0500 Subject: [PATCH 6/6] Fix preview snaps; cargo clippy --- ...t__requires_python_extend_from_shared_config.snap | 6 +----- .../cli__lint__requires_python_no_tool.snap | 6 +----- ...uires_python_no_tool_target_version_override.snap | 6 +----- ...__lint__requires_python_pyproject_toml_above.snap | 6 +----- ...quires_python_pyproject_toml_above_with_tool.snap | 6 +----- ...cli__lint__requires_python_ruff_toml_above-2.snap | 6 +----- .../cli__lint__requires_python_ruff_toml_above.snap | 6 +----- ...requires_python_ruff_toml_no_target_fallback.snap | 6 +----- .../show_settings__display_default_settings.snap | 6 +----- .../src/rules/flake8_import_conventions/settings.rs | 12 ++++++++++++ crates/ruff_workspace/src/configuration.rs | 6 +++++- crates/ruff_workspace/src/options.rs | 4 +--- 12 files changed, 27 insertions(+), 49 deletions(-) diff --git a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap index 7a67abe52e..cad465dc4e 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_extend_from_shared_config.snap @@ -142,18 +142,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap index 455f58bec9..47b6f47c1a 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool.snap @@ -144,18 +144,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap index 00ff41dc62..4de849c6a1 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_no_tool_target_version_override.snap @@ -146,18 +146,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap index 5752bc3296..c7f04bcbe2 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above.snap @@ -143,18 +143,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap index 76b34de492..66e3dd06ba 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_pyproject_toml_above_with_tool.snap @@ -143,18 +143,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap index a40d8c27e9..da3eb66afc 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above-2.snap @@ -142,18 +142,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap index 6c23d8774f..e9ca7bd400 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_above.snap @@ -142,18 +142,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap index 19207ecaf4..350ca02474 100644 --- a/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap +++ b/crates/ruff/tests/cli/snapshots/cli__lint__requires_python_ruff_toml_no_target_fallback.snap @@ -142,18 +142,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index 04fdec5c5e..28a6607816 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -255,18 +255,14 @@ 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 = { - geopandas = [gpd], -} +linter.flake8_import_conventions.banned_aliases = {} 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/src/rules/flake8_import_conventions/settings.rs b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs index 53023c5e55..1cf69e5e53 100644 --- a/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs @@ -90,6 +90,18 @@ pub fn preview_banned_aliases() -> FxHashMap { )]) } +impl Settings { + /// Merge preview aliases and banned aliases into the settings if preview mode is enabled. + #[must_use] + pub fn with_preview(mut self, preview_enabled: bool) -> Self { + if preview_enabled { + self.aliases.extend(preview_aliases()); + self.banned_aliases.extend(preview_banned_aliases()); + } + self + } +} + impl Default for Settings { fn default() -> Self { Self { diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 8ca5350d11..0bf9a50a48 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -247,12 +247,16 @@ impl Configuration { .map(IsortOptions::try_into_settings) .transpose()? .unwrap_or_default(); - let flake8_import_conventions = lint + let mut flake8_import_conventions = lint .flake8_import_conventions .map(Flake8ImportConventionsOptions::try_into_settings) .transpose()? .unwrap_or_default(); + // Merge preview aliases and banned aliases when preview mode is enabled + flake8_import_conventions = + flake8_import_conventions.with_preview(matches!(lint_preview, PreviewMode::Enabled)); + conflicting_import_settings(&isort, &flake8_import_conventions)?; let future_annotations = lint.future_annotations.unwrap_or_default(); diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index d90641cd30..e5205b3539 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -1685,9 +1685,7 @@ impl Flake8ImportConventionsOptions { Ok(flake8_import_conventions::settings::Settings { aliases: normalized_aliases, - banned_aliases: self - .banned_aliases - .unwrap_or_else(FxHashMap::default), + banned_aliases: self.banned_aliases.unwrap_or_default(), banned_from: self.banned_from.unwrap_or_default(), }) }