mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[flake8-import-conventions
] Syntax check aliases supplied in configuration for unconventional-import-alias (ICN001)
(#14477)
Co-authored-by: Micha Reiser <micha@reiser.io> Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
parent
b9da4305e6
commit
2efa3fbb62
5 changed files with 141 additions and 5 deletions
|
@ -21,6 +21,7 @@ ruff_macros = { workspace = true }
|
|||
ruff_python_ast = { workspace = true }
|
||||
ruff_python_formatter = { workspace = true, features = ["serde"] }
|
||||
ruff_python_semantic = { workspace = true, features = ["serde"] }
|
||||
ruff_python_stdlib = {workspace = true}
|
||||
ruff_source_file = { workspace = true }
|
||||
|
||||
anyhow = { workspace = true }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use regex::Regex;
|
||||
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::de::{self};
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
use strum::IntoEnumIterator;
|
||||
|
@ -34,6 +35,7 @@ use ruff_macros::{CombineOptions, OptionsMetadata};
|
|||
use ruff_python_ast::name::Name;
|
||||
use ruff_python_formatter::{DocstringCodeLineWidth, QuoteStyle};
|
||||
use ruff_python_semantic::NameImports;
|
||||
use ruff_python_stdlib::identifiers::is_identifier;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Default, OptionsMetadata, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||
|
@ -1327,7 +1329,7 @@ pub struct Flake8ImportConventionsOptions {
|
|||
scipy = "sp"
|
||||
"#
|
||||
)]
|
||||
pub aliases: Option<FxHashMap<String, String>>,
|
||||
pub aliases: Option<FxHashMap<ModuleName, Alias>>,
|
||||
|
||||
/// A mapping from module to conventional import alias. These aliases will
|
||||
/// be added to the [`aliases`](#lint_flake8-import-conventions_aliases) mapping.
|
||||
|
@ -1370,10 +1372,67 @@ pub struct Flake8ImportConventionsOptions {
|
|||
pub banned_from: Option<FxHashSet<String>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub struct ModuleName(String);
|
||||
|
||||
impl ModuleName {
|
||||
pub fn into_string(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ModuleName {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let name = String::deserialize(deserializer)?;
|
||||
if name.is_empty() || name.split('.').any(|part| !is_identifier(part)) {
|
||||
Err(de::Error::invalid_value(
|
||||
de::Unexpected::Str(&name),
|
||||
&"a sequence of Python identifiers delimited by periods",
|
||||
))
|
||||
} else {
|
||||
Ok(Self(name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub struct Alias(String);
|
||||
|
||||
impl Alias {
|
||||
pub fn into_string(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Alias {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let name = String::deserialize(deserializer)?;
|
||||
if is_identifier(&name) {
|
||||
Ok(Self(name))
|
||||
} else {
|
||||
Err(de::Error::invalid_value(
|
||||
de::Unexpected::Str(&name),
|
||||
&"a Python identifier",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Flake8ImportConventionsOptions {
|
||||
pub fn into_settings(self) -> flake8_import_conventions::settings::Settings {
|
||||
let mut aliases = match self.aliases {
|
||||
Some(options_aliases) => options_aliases,
|
||||
let mut aliases: FxHashMap<String, String> = match self.aliases {
|
||||
Some(options_aliases) => options_aliases
|
||||
.into_iter()
|
||||
.map(|(module, alias)| (module.into_string(), alias.into_string()))
|
||||
.collect(),
|
||||
None => flake8_import_conventions::settings::default_aliases(),
|
||||
};
|
||||
if let Some(extend_aliases) = self.extend_aliases {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue