[pyflakes] Fix preview-mode bugs in F401 when attempting to autofix unused first-party submodule imports in an __init__.py file (#12569)

This commit is contained in:
Alex Waygood 2024-07-31 13:34:30 +01:00 committed by GitHub
parent 83b1c48a93
commit a3900d2b0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 220 additions and 86 deletions

View file

@ -1,7 +1,5 @@
//! Interface for generating fix edits from higher-level actions (e.g., "remove an argument").
use std::borrow::Cow;
use anyhow::{Context, Result};
use ruff_diagnostics::Edit;
@ -126,7 +124,7 @@ pub(crate) fn remove_unused_imports<'a>(
/// Edits to make the specified imports explicit, e.g. change `import x` to `import x as x`.
pub(crate) fn make_redundant_alias<'a>(
member_names: impl Iterator<Item = Cow<'a, str>>,
member_names: impl Iterator<Item = &'a str>,
stmt: &Stmt,
) -> Vec<Edit> {
let aliases = match stmt {
@ -527,7 +525,6 @@ fn all_lines_fit(
#[cfg(test)]
mod tests {
use anyhow::{anyhow, Result};
use std::borrow::Cow;
use test_case::test_case;
use ruff_diagnostics::{Diagnostic, Edit, Fix};
@ -619,7 +616,7 @@ x = 1 \
let contents = "import x, y as y, z as bees";
let stmt = parse_first_stmt(contents)?;
assert_eq!(
make_redundant_alias(["x"].into_iter().map(Cow::from), &stmt),
make_redundant_alias(["x"].into_iter(), &stmt),
vec![Edit::range_replacement(
String::from("x as x"),
TextRange::new(TextSize::new(7), TextSize::new(8)),
@ -627,7 +624,7 @@ x = 1 \
"make just one item redundant"
);
assert_eq!(
make_redundant_alias(vec!["x", "y"].into_iter().map(Cow::from), &stmt),
make_redundant_alias(vec!["x", "y"].into_iter(), &stmt),
vec![Edit::range_replacement(
String::from("x as x"),
TextRange::new(TextSize::new(7), TextSize::new(8)),
@ -635,7 +632,7 @@ x = 1 \
"the second item is already a redundant alias"
);
assert_eq!(
make_redundant_alias(vec!["x", "z"].into_iter().map(Cow::from), &stmt),
make_redundant_alias(vec!["x", "z"].into_iter(), &stmt),
vec![Edit::range_replacement(
String::from("x as x"),
TextRange::new(TextSize::new(7), TextSize::new(8)),