Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: all flake8-comprehension rules #3631

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ use crate::rules::flake8_comprehensions::settings::Settings;

use super::helpers;

/// ## What it does
/// Checks for unnecessary `dict`, `list` or `tuple` calls.
///
/// ## Why is this bad?
/// It's unnecessary to call e.g., `dict()` than using the empty literal.
/// The former is slower because the name `dict` must be looked up in the
/// global scope in case it has been rebound.
///
/// ## Examples
/// ```python
/// dict()
/// dict(a=1, b=2)
/// list()
/// tuple()
/// ```
///
/// Use instead:
/// ```python
/// {}
/// {"a": 1, "b": 2}
/// []
/// ()
/// ```
#[violation]
pub struct UnnecessaryCollectionCall {
pub obj_type: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use super::helpers;
///
/// This rule applies to a variety of functions, including `list`, `reversed`,
/// `set`, `sorted`, and `tuple`. For example:
///
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to avoid grouping all the list elements into a single line: https://beta.ruff.rs/docs/rules/unnecessary-double-cast-or-process/

I generated the docs locally and confirmed that the elements are now rendered as required.

/// - Instead of `list(list(iterable))`, use `list(iterable)`.
/// - Instead of `list(tuple(iterable))`, use `list(iterable)`.
/// - Instead of `tuple(list(iterable))`, use `tuple(iterable)`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary `list` calls around a list comprehension.
///
/// ## Why is it bad?
/// It is unnecessary to use a `list` around a list comprehension, since
/// it is equivalent without it.
///
/// ## Examples
/// ```python
/// list([f(x) for x in foo])
/// ```
///
/// Use instead
/// ```python
/// [f(x) for x in foo]
/// ```
#[violation]
pub struct UnnecessaryListCall;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary list comprehensions.
///
/// ## Why is it bad?
/// It's unnecessary to use a list comprehension inside a call to `dict`,
/// since there are equivalent comprehensions for these types.
///
/// ## Examples
/// ```python
/// dict([(x, f(x)) for x in foo])
/// ```
///
/// Use instead:
/// ```python
/// {x: f(x) for x in foo}
/// ```
#[violation]
pub struct UnnecessaryListComprehensionDict;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary list comprehensions.
///
/// ## Why is it bad?
/// It's unnecessary to use a list comprehension inside a call to `set`,
/// since there are equivalent comprehensions for these types.
///
/// ## Examples
/// ```python
/// set([f(x) for x in foo])
/// ```
///
/// Use instead:
/// ```python
/// {f(x) for x in foo}
/// ```
#[violation]
pub struct UnnecessaryListComprehensionSet;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary `list` or `tuple` literal.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a call to `dict`.
/// It can be rewritten as a dict literal.
///
/// ## Examples
/// ```python
/// dict([(1, 2), (3, 4)])
/// dict(((1, 2), (3, 4)))
/// dict([])
/// ```
///
/// Use instead:
/// ```python
/// {1: 2, 3: 4}
/// {1: 2, 3: 4}
/// {}
/// ```
#[violation]
pub struct UnnecessaryLiteralDict {
pub obj_type: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary `list` or `tuple` literal.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a call to `set`.
/// It can be rewritten as a set literal.
///
/// ## Examples
/// ```python
/// set([1, 2])
/// set((1, 2))
/// set([])
/// ```
///
/// Use instead:
/// ```python
/// {1, 2}
/// {1, 2}
/// set()
/// ```
#[violation]
pub struct UnnecessaryLiteralSet {
pub obj_type: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary list or tuple literal passed to a `list()` call.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a `list()` call,
/// since there is a literal syntax for these types.
///
/// If a list literal was passed, then the outer call to `list()` should be
/// removed. Otherwise, if a tuple literal was passed, then it should be
/// rewritten as a `list` literal.
///
/// ## Examples
/// ```python
/// list([1, 2])
/// list((1, 2))
/// ```
///
/// Use instead:
/// ```python
/// [1, 2]
/// [1, 2]
/// ```
#[violation]
pub struct UnnecessaryLiteralWithinListCall {
pub literal: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ use crate::rules::flake8_comprehensions::fixes;

use super::helpers;

/// ## What it does
/// Checks for unnecessary list or tuple literal passed to a `tuple()` call.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a `tuple()` call,
/// since there is a literal syntax for these types.
///
/// If a list literal was passed, then it should be rewritten as a `tuple`
/// literal. Otherwise, if a tuple literal was passed, then the outer call
/// to `list()` should be removed.
///
/// ## Examples
/// ```python
/// tuple([1, 2])
/// tuple((1, 2))
/// ```
///
/// Use instead:
/// ```python
/// (1, 2)
/// (1, 2)
/// ```
#[violation]
pub struct UnnecessaryLiteralWithinTupleCall {
pub literal: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use super::helpers;
///
/// This rule also applies to `map` calls within `list`, `set`, and `dict`
/// calls. For example:
///
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// - Instead of `list(map(lambda num: num * 2, nums))`, use
/// `[num * 2 for num in nums]`.
/// - Instead of `set(map(lambda num: num % 2 == 0, nums))`, use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ use crate::checkers::ast::Checker;

use super::helpers;

/// ## What it does
/// Checks for unnecessary subscript reversal of iterable.
///
/// ## Why is it bad?
/// It's unnecessary to reverse the order of an iterable when passing it
/// into `reversed()`, `set()` or `sorted()` functions as they will change
/// the order of the elements again.
///
/// ## Examples
/// ```python
/// reversed(iterable[::-1])
/// set(iterable[::-1])
/// sorted(iterable)[::-1]
/// ```
///
/// Use instead:
/// ```python
/// reversed(iterable)
/// set(iterable)
/// sorted(iterable, reverse=True)
/// ```
#[violation]
pub struct UnnecessarySubscriptReversal {
pub func: String,
Expand Down