Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-import-conventions] Syntax check aliases supplied in configuration for unconventional-import-alias (ICN001) #14477

Merged
merged 9 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,3 +1966,75 @@ fn nested_implicit_namespace_package() -> Result<()> {

Ok(())
}

#[test]
fn flake8_import_convention_invalid_aliases_config_alias_name() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
[lint.flake8-import-conventions.aliases]
"module.name" = "invalid.alias"
"#,
)?;

insta::with_settings!({
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
, @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
ruff failed
Cause: Failed to parse [TMP]/ruff.toml
Cause: TOML parse error at line 2, column 2
|
2 | [lint.flake8-import-conventions.aliases]
| ^^^^
invalid value: string "invalid.alias", expected a Python identifier
"###);});
Ok(())
}

#[test]
fn flake8_import_convention_invalid_aliases_config_module_name() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
[lint.flake8-import-conventions.aliases]
"module..invalid" = "alias"
"#,
)?;

insta::with_settings!({
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
, @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
ruff failed
Cause: Failed to parse [TMP]/ruff.toml
Cause: TOML parse error at line 2, column 2
|
2 | [lint.flake8-import-conventions.aliases]
| ^^^^
invalid value: string "module..invalid", expected a sequence of Python identifiers delimited by periods
"###);});
Ok(())
}
1 change: 1 addition & 0 deletions crates/ruff_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
67 changes: 63 additions & 4 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading