Skip to content

Commit

Permalink
Add known limitation to C416 with tuple keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 4, 2024
1 parent d726f09 commit 5bf4f37
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use crate::rules::flake8_comprehensions::fixes;
/// Checks for unnecessary `dict`, `list`, and `set` comprehension.
///
/// ## Why is this bad?
/// It's unnecessary to use a `dict`/`list`/`set` comprehension to build a
/// data structure if the elements are unchanged. Wrap the iterable with
/// `dict()`, `list()`, or `set()` instead.
/// It's unnecessary to use a `dict`/`list`/`set` comprehension to build a data structure if the
/// elements are unchanged. Wrap the iterable with `dict()`, `list()`, or `set()` instead.
///
/// ## Examples
/// ```python
Expand All @@ -30,10 +29,23 @@ use crate::rules::flake8_comprehensions::fixes;
/// set(iterable)
/// ```
///
/// ## Known problems
///
/// When the dictionary key is a tuple, e.g.:
///
/// ```python
/// d1 = {(1, 2): 3, (3, 4): 5}
/// d2 = {x: y for x, y in d1}
/// ```
///
/// The tuple key is unpackaged into `x` and `y` instead of the key and values. This means that
/// the suggested fix of `d2 = dict(d1)` would result in different runtime behavior. Ruff
/// cannot yet detect the tuple key.
///
/// ## Fix safety
/// This rule's fix is marked as unsafe, as it may occasionally drop comments
/// when rewriting the comprehension. In most cases, though, comments will be
/// preserved.
/// Due to the known problem with tuple keys, this fix is marked as unsafe.
///
/// Additionally, this fix may drop comments when rewriting the comprehension.
#[violation]
pub struct UnnecessaryComprehension {
obj_type: String,
Expand Down

0 comments on commit 5bf4f37

Please sign in to comment.