Skip to content

Commit

Permalink
Mark PLE1141 fix as unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 4, 2024
1 parent d726f09 commit d9a8020
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ use crate::checkers::ast::Checker;
///
/// for city, population in data.items():
/// print(f"{city} has population {population}.")
///
/// ## Fix safety
/// If the dictionary key is a tuple, e.g.:
///
/// ```python
/// d = {(1, 2): 3, (3, 4): 5}
/// for x, y in d:
/// print(x, y)
/// ```
///
/// The tuple key is unpackaged into `x` and `y` instead of the key and values. This means that
/// the suggested fix of using `d.items()` would result in different runtime behavior.
///
#[violation]
pub struct DictIterMissingItems;

Expand All @@ -48,6 +60,7 @@ impl AlwaysFixableViolation for DictIterMissingItems {
}
}

/// PLE1141
pub(crate) fn dict_iter_missing_items(checker: &mut Checker, target: &Expr, iter: &Expr) {
let Expr::Tuple(tuple) = target else {
return;
Expand Down Expand Up @@ -78,7 +91,7 @@ pub(crate) fn dict_iter_missing_items(checker: &mut Checker, target: &Expr, iter
}

let mut diagnostic = Diagnostic::new(DictIterMissingItems, iter.range());
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
format!("{}.items()", name.id),
iter.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dict_iter_missing_items.py:13:13: PLE1141 [*] Unpacking a dictionary in iteratio
|
= help: Add a call to `.items()`

Safe fix
Unsafe fix
10 10 | s2 = {1, 2, 3}
11 11 |
12 12 | # Errors
Expand All @@ -30,14 +30,12 @@ dict_iter_missing_items.py:16:13: PLE1141 [*] Unpacking a dictionary in iteratio
|
= help: Add a call to `.items()`

Safe fix
Unsafe fix
13 13 | for k, v in d:
14 14 | pass
15 15 |
16 |-for k, v in d_tuple_incorrect_tuple:
16 |+for k, v in d_tuple_incorrect_tuple.items():
17 17 | pass
18 18 |
19 19 |


19 19 |

0 comments on commit d9a8020

Please sign in to comment.