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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 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 All @@ -42,6 +43,7 @@ serde = { workspace = true }
shellexpand = { workspace = true }
strum = { workspace = true }
toml = { workspace = true }
serde_json.workspace = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a dev-dependency if it is only used in tests


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
etcetera = { workspace = true }
Expand Down
37 changes: 36 additions & 1 deletion 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,6 +1329,7 @@ pub struct Flake8ImportConventionsOptions {
scipy = "sp"
"#
)]
#[serde(deserialize_with = "deserialize_and_validate_aliases")]
pub aliases: Option<FxHashMap<String, String>>,

/// A mapping from module to conventional import alias. These aliases will
Expand Down Expand Up @@ -1370,6 +1373,30 @@ pub struct Flake8ImportConventionsOptions {
pub banned_from: Option<FxHashSet<String>>,
}

fn deserialize_and_validate_aliases<'de, D>(
deserializer: D,
) -> Result<Option<FxHashMap<String, String>>, D::Error>
where
D: Deserializer<'de>,
{
let Some(aliases) = Option::<FxHashMap<String, String>>::deserialize(deserializer)? else {
return Ok(None);
};

for (module, alias) in &aliases {
if module.is_empty()
|| module.split('.').any(|part| !is_identifier(part))
|| !is_identifier(alias)
{
return Err(de::Error::custom(
"Module must be valid identifier separated by single periods and alias must be valid identifier",
));
}
Comment on lines +1387 to +1394
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we use two dedicated errors: One for alias and one for module.

}

Ok(Some(aliases)) // Return the validated map.
}

impl Flake8ImportConventionsOptions {
pub fn into_settings(self) -> flake8_import_conventions::settings::Settings {
let mut aliases = match self.aliases {
Expand Down Expand Up @@ -3406,12 +3433,20 @@ pub struct AnalyzeOptions {

#[cfg(test)]
mod tests {
use crate::options::Flake8ImportConventionsOptions;
use crate::options::Flake8SelfOptions;
use ruff_linter::rules::flake8_self;
use ruff_linter::settings::types::PythonVersion as LinterPythonVersion;
use ruff_python_ast::name::Name;
use ruff_python_formatter::PythonVersion as FormatterPythonVersion;

#[test]
fn flake8_import_conventions_validate_aliases() {
let json_options = r#"{"aliases": {"a.b":"a.b"}}"#;
let result: Result<Flake8ImportConventionsOptions, _> = serde_json::from_str(json_options);
assert!(result.unwrap_err().to_string().contains("Module must be valid identifier separated by single periods and alias must be valid identifier"));
}

Comment on lines +3443 to +3449
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards making this a ruff cli integration test. It then also demonstrates how the error message is displayed to the users.

See

fn top_level_options() -> Result<()> {

#[test]
fn flake8_self_options() {
let default_settings = flake8_self::settings::Settings::default();
Expand Down
Loading