Skip to content

Commit

Permalink
Document flake8-commas (#4142)
Browse files Browse the repository at this point in the history
  • Loading branch information
calumy committed Apr 29, 2023
1 parent 0172cc5 commit 03144b2
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions crates/ruff/src/rules/flake8_commas/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ impl Context {
}
}

/// ## What it does
/// Checks for the absence of trailing commas.
///
/// ## Why is this bad?
/// The presence of a trailing comma can reduce diff size when parameters or
/// elements are added or removed from function calls, function definitions,
/// literals, etc.
///
/// ## Example
/// ```python
/// foo = {
/// "bar": 1,
/// "baz": 2
/// }
/// ```
///
/// Use instead:
/// ```python
/// foo = {
/// "bar": 1,
/// "baz": 2,
/// }
/// ```
#[violation]
pub struct MissingTrailingComma;

Expand All @@ -124,6 +147,45 @@ impl AlwaysAutofixableViolation for MissingTrailingComma {
}
}

/// ## What it does
/// Checks for the presence of trailing commas on bare (i.e., unparenthesized)
/// tuples.
///
/// ## Why is this bad?
/// The presence of a misplaced comma will cause Python to interpret the value
/// as a tuple, which can lead to unexpected behaviour.
///
/// ## Example
/// ```python
/// import json
///
///
/// foo = json.dumps({
/// "bar": 1,
/// }),
/// ```
///
/// Use instead:
/// ```python
/// import json
///
///
/// foo = json.dumps({
/// "bar": 1,
/// })
/// ```
///
/// In the event that a tuple is intended, then use instead:
/// ```python
/// import json
///
///
/// foo = (
/// json.dumps({
/// "bar": 1,
/// }),
/// )
/// ```
#[violation]
pub struct TrailingCommaOnBareTuple;

Expand All @@ -134,6 +196,22 @@ impl Violation for TrailingCommaOnBareTuple {
}
}

/// ## What it does
/// Checks for the presence of prohibited trailing commas.
///
/// ## Why is this bad?
/// Trailing commas are not essential in some cases and can therefore be viewed
/// as unnecessary.
///
/// ## Example
/// ```python
/// foo = (1, 2, 3,)
/// ```
///
/// Use instead:
/// ```python
/// foo = (1, 2, 3)
/// ```
#[violation]
pub struct ProhibitedTrailingComma;

Expand Down

0 comments on commit 03144b2

Please sign in to comment.