Config error only when flake8-import-conventions alias conflicts with isort.required-imports bound name (#15918)

Previously an error was emitted any time the configuration required both
an import of a module and an alias for that module. However, required
imports could themselves contain an alias, which may or may not agree
with the required alias.

To wit: requiring `import pandas as pd` does not conflict with the
`flake8-import-conventions.alias` config `{"pandas":"pd"}`.

This PR refines the check before throwing an error.

Closes #15911
This commit is contained in:
Dylan 2025-02-04 17:05:35 -06:00 committed by GitHub
parent 4c15d7a559
commit 700e969c56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 56 additions and 4 deletions

View file

@ -1576,9 +1576,20 @@ fn conflicting_import_settings(
use std::fmt::Write;
let mut err_body = String::new();
for required_import in &isort.required_imports {
let name = required_import.qualified_name().to_string();
if let Some(alias) = flake8_import_conventions.aliases.get(&name) {
writeln!(err_body, " - `{name}` -> `{alias}`").unwrap();
// Ex: `from foo import bar as baz` OR `import foo.bar as baz`
// - qualified name: `foo.bar`
// - bound name: `baz`
// - conflicts with: `{"foo.bar":"buzz"}`
// - does not conflict with either of
// - `{"bar":"buzz"}`
// - `{"foo.bar":"baz"}`
let qualified_name = required_import.qualified_name().to_string();
let bound_name = required_import.bound_name();
let Some(alias) = flake8_import_conventions.aliases.get(&qualified_name) else {
continue;
};
if alias != bound_name {
writeln!(err_body, " - `{qualified_name}` -> `{alias}`").unwrap();
}
}