Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ info:
- concise
- "--show-settings"
- test.py
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ info:
- UP007
- test.py
- "-"
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ info:
- UP007
- test.py
- "-"
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ info:
- py310
- test.py
- "-"
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ info:
- "--select"
- UP007
- foo/test.py
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ info:
- "--select"
- UP007
- foo/test.py
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ info:
- concise
- test.py
- "--show-settings"
snapshot_kind: text
---
success: true
exit_code: 0
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
13 changes: 11 additions & 2 deletions crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -73,11 +75,18 @@ pub fn default_aliases() -> FxHashMap<String, String> {
.collect::<FxHashMap<_, _>>()
}

pub fn default_banned_aliases() -> FxHashMap<String, BannedAliases> {
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(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
8 changes: 5 additions & 3 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down Expand Up @@ -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#"
Expand Down Expand Up @@ -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(),
})
}
Expand Down