mirror of https://github.com/astral-sh/ruff
Add known limitation to `C416` with dictionaries (#13627)
Part of https://github.com/astral-sh/ruff/issues/13625 See also #13629
This commit is contained in:
parent
d7484e6942
commit
fb90f5a13d
|
|
@ -12,9 +12,8 @@ use crate::rules::flake8_comprehensions::fixes;
|
||||||
/// Checks for unnecessary `dict`, `list`, and `set` comprehension.
|
/// Checks for unnecessary `dict`, `list`, and `set` comprehension.
|
||||||
///
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// It's unnecessary to use a `dict`/`list`/`set` comprehension to build a
|
/// It's unnecessary to use a `dict`/`list`/`set` comprehension to build a data structure if the
|
||||||
/// data structure if the elements are unchanged. Wrap the iterable with
|
/// elements are unchanged. Wrap the iterable with `dict()`, `list()`, or `set()` instead.
|
||||||
/// `dict()`, `list()`, or `set()` instead.
|
|
||||||
///
|
///
|
||||||
/// ## Examples
|
/// ## Examples
|
||||||
/// ```python
|
/// ```python
|
||||||
|
|
@ -30,10 +29,33 @@ use crate::rules::flake8_comprehensions::fixes;
|
||||||
/// set(iterable)
|
/// set(iterable)
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// ## Known problems
|
||||||
|
///
|
||||||
|
/// This rule may produce false positives for dictionary comprehensions that iterate over a mapping.
|
||||||
|
/// The `dict` constructor behaves differently depending on if it receives a sequence (e.g., a
|
||||||
|
/// `list`) or a mapping (e.g., a `dict`). When a comprehension iterates over the keys of a mapping,
|
||||||
|
/// replacing it with a `dict` constructor call will give a different result.
|
||||||
|
///
|
||||||
|
/// For example:
|
||||||
|
///
|
||||||
|
/// ```pycon
|
||||||
|
/// >>> d1 = {(1, 2): 3, (4, 5): 6}
|
||||||
|
/// >>> {x: y for x, y in d1} # Iterates over the keys of a mapping
|
||||||
|
/// {1: 2, 4: 5}
|
||||||
|
/// >>> dict(d1) # Ruff's incorrect suggested fix
|
||||||
|
/// (1, 2): 3, (4, 5): 6}
|
||||||
|
/// >>> dict(d1.keys()) # Correct fix
|
||||||
|
/// {1: 2, 4: 5}
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// When the comprehension iterates over a sequence, Ruff's suggested fix is correct. However, Ruff
|
||||||
|
/// cannot consistently infer if the iterable type is a sequence or a mapping and cannot suggest
|
||||||
|
/// the correct fix for mappings.
|
||||||
|
///
|
||||||
/// ## Fix safety
|
/// ## Fix safety
|
||||||
/// This rule's fix is marked as unsafe, as it may occasionally drop comments
|
/// Due to the known problem with dictionary comprehensions, this fix is marked as unsafe.
|
||||||
/// when rewriting the comprehension. In most cases, though, comments will be
|
///
|
||||||
/// preserved.
|
/// Additionally, this fix may drop comments when rewriting the comprehension.
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct UnnecessaryComprehension {
|
pub struct UnnecessaryComprehension {
|
||||||
obj_type: String,
|
obj_type: String,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue