Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aafb689
fix invalid hoist
w0nder1ng Nov 15, 2024
fd71328
tentative fix
w0nder1ng Nov 15, 2024
f30118e
don't use a comprehension when the list is referenced
w0nder1ng Nov 15, 2024
238d17a
check for external references to the for loop variable
w0nder1ng Nov 18, 2024
46bd851
stop duplicating comments inside the fixed append
w0nder1ng Nov 18, 2024
9a336fd
fix annotated returns, but lose type info
w0nder1ng Nov 18, 2024
63b0e1e
apply fix to type-annotated lists
w0nder1ng Nov 18, 2024
527a050
fix async extends, implicit tuples, and comments in iterator
w0nder1ng Dec 3, 2024
f339d70
stop deleting semicolon statements in the same line as binding
w0nder1ng Dec 6, 2024
f6ccb39
move manual list comprehension to deferred check
w0nder1ng Dec 6, 2024
6a98542
add debugging code to show binding
w0nder1ng Dec 6, 2024
e2eec2e
find the right binding for the loop variable
w0nder1ng Dec 9, 2024
9d41fb7
check that the correct for loop is picked
w0nder1ng Dec 9, 2024
104b69e
don't stop fix if a reference to target is actually another binding
w0nder1ng Dec 9, 2024
e44fcaa
allow shadowed bindings from any scope
w0nder1ng Dec 9, 2024
4696878
simplify check for loop variable usages
w0nder1ng Dec 10, 2024
74afdb0
cargo fmt
w0nder1ng Dec 10, 2024
db8d0d6
Few simplifications
MichaReiser Dec 11, 2024
77a0290
Merge branch 'main' into perf401_invalid_hoist
w0nder1ng Dec 11, 2024
c9b394c
fix merge
w0nder1ng Dec 11, 2024
9c55f1b
replace tokenization with simple character iterator
w0nder1ng Dec 11, 2024
523808d
Use simple tokenizer
MichaReiser Dec 12, 2024
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
Prev Previous commit
Next Next commit
apply fix to type-annotated lists
  • Loading branch information
w0nder1ng committed Nov 18, 2024
commit 63b0e1e4dc3cbd7724a564e591c994d8e9366130
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ pub(crate) fn manual_list_comprehension(checker: &mut Checker, for_stmt: &ast::S
ast::Stmt::Assign(assign) => Some(&assign.value),
_ => None,
});
dbg!(binding_value);
// If the variable is an empty list literal, then we might be able to replace it with a full list comprehension
// otherwise, it has to be replaced with a `list.extend`
let binding_is_empty_list =
Expand Down Expand Up @@ -413,8 +412,16 @@ fn convert_to_list_extend(
.ok_or(anyhow!(
"Binding must have a statement to convert into a list comprehension"
))?;
let mut comments_to_move =
comment_strings_in_range(binding_stmt_range);

let annotations = match binding
.statement(checker.semantic())
.and_then(|stmt| stmt.as_ann_assign_stmt())
{
Some(assign) => format!(": {}", locator.slice(assign.annotation.range())),
None => String::new(),
};

let mut comments_to_move = comment_strings_in_range(binding_stmt_range);
comments_to_move.extend(for_loop_inline_comments);

let indentation = if comments_to_move.is_empty() {
Expand All @@ -425,7 +432,7 @@ fn convert_to_list_extend(
let leading_comments = format!("{}{indentation}", comments_to_move.join(&indentation));

let comprehension_body =
format!("{leading_comments}{variable_name} = [{generator_str}]");
format!("{leading_comments}{variable_name}{annotations} = [{generator_str}]");

Ok(Fix::unsafe_edits(
Edit::range_deletion(binding_stmt_range),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,16 @@ PERF401.py:175:9: PERF401 Use a list comprehension to create a transformed list
180 | | )
181 | | ) # PERF401
| |_________^ PERF401
182 |
183 | def f():
|
= help: Replace for loop with list comprehension

PERF401.py:186:9: PERF401 Use a list comprehension to create a transformed list
|
184 | result: list[int] = []
185 | for i in range(10):
186 | result.append(i*2) # PERF401
| ^^^^^^^^^^^^^^^^^^ PERF401
|
= help: Replace for loop with list comprehension
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ PERF401.py:175:9: PERF401 [*] Use a list comprehension to create a transformed l
180 | | )
181 | | ) # PERF401
| |_________^ PERF401
182 |
183 | def f():
|
= help: Replace for loop with list comprehension

Expand All @@ -348,3 +350,24 @@ PERF401.py:175:9: PERF401 [*] Use a list comprehension to create a transformed l
180 |- )
181 |- ) # PERF401
177 |+ ) for i in range(2)] # PERF401
182 178 |
183 179 | def f():
184 180 | result: list[int] = []

PERF401.py:186:9: PERF401 [*] Use a list comprehension to create a transformed list
|
184 | result: list[int] = []
185 | for i in range(10):
186 | result.append(i*2) # PERF401
| ^^^^^^^^^^^^^^^^^^ PERF401
|
= help: Replace for loop with list comprehension

ℹ Unsafe fix
181 181 | ) # PERF401
182 182 |
183 183 | def f():
184 |- result: list[int] = []
185 |- for i in range(10):
186 |- result.append(i*2) # PERF401
184 |+ result: list[int] = [i*2 for i in range(10)] # PERF401