mirror of https://github.com/astral-sh/ruff
Merge 2222bcf51a into b0bc990cbf
This commit is contained in:
commit
4bbd17a8bf
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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<String, String> {
|
|||
.collect::<FxHashMap<_, _>>()
|
||||
}
|
||||
|
||||
pub fn preview_aliases() -> FxHashMap<String, String> {
|
||||
PREVIEW_ALIASES
|
||||
.iter()
|
||||
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
|
||||
.collect::<FxHashMap<_, _>>()
|
||||
}
|
||||
|
||||
pub fn preview_banned_aliases() -> FxHashMap<String, BannedAliases> {
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
||||
|
|
@ -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)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<flake8_import_conventions::settings::Settings> {
|
||||
let mut aliases: FxHashMap<String, String> = 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<String, String> = FxHashMap::default();
|
||||
for (module, alias) in aliases {
|
||||
let normalized_alias = alias.nfkc().collect::<String>();
|
||||
|
|
@ -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(),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue