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] Improve syntax check for aliases supplied in configuration for unconventional-import-alias (ICN001) #14745

Merged
merged 4 commits into from
Dec 3, 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
36 changes: 36 additions & 0 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,42 @@ fn flake8_import_convention_invalid_aliases_config_alias_name() -> Result<()> {
Ok(())
}

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

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.extend-aliases]
| ^^^^
invalid value: string "__debug__", expected an assignable Python identifier
"###);});
Ok(())
}

#[test]
fn flake8_import_convention_invalid_aliases_config_module_name() -> Result<()> {
let tempdir = TempDir::new()?;
Expand Down
17 changes: 15 additions & 2 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ pub struct Flake8ImportConventionsOptions {
"dask.dataframe" = "dd"
"#
)]
pub extend_aliases: Option<FxHashMap<String, String>>,
pub extend_aliases: Option<FxHashMap<ModuleName, Alias>>,

/// A mapping from module to its banned import aliases.
#[option(
Expand Down Expand Up @@ -1415,6 +1415,15 @@ impl<'de> Deserialize<'de> for Alias {
D: Deserializer<'de>,
{
let name = String::deserialize(deserializer)?;
// Assigning to "__debug__" is a SyntaxError
// see the note here:
// https://docs.python.org/3/library/constants.html#debug__
if &*name == "__debug__" {
return Err(de::Error::invalid_value(
de::Unexpected::Str(&name),
&"an assignable Python identifier",
));
}
if is_identifier(&name) {
Ok(Self(name))
} else {
Expand All @@ -1436,7 +1445,11 @@ impl Flake8ImportConventionsOptions {
None => flake8_import_conventions::settings::default_aliases(),
};
if let Some(extend_aliases) = self.extend_aliases {
aliases.extend(extend_aliases);
aliases.extend(
extend_aliases
.into_iter()
.map(|(module, alias)| (module.into_string(), alias.into_string())),
);
}

flake8_import_conventions::settings::Settings {
Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

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

Loading