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

Fix E231 bug: Inconsistent catch compared to pycodestyle, such as when dict nested in list #10469

Merged
merged 4 commits into from Mar 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/pycodestyle/E23.py
Expand Up @@ -47,4 +47,32 @@ def foo() -> None:
{len(f's3://{self.s3_bucket_name}/'):1}

#: Okay
a = (1,
a = (1,)


# https://github.com/astral-sh/ruff/issues/10113
"""Minimal repo."""

def main() -> None:
"""Primary function."""
results = {
"k1": [1],
"k2":[2],
}
results_in_tuple = (
{
"k1": [1],
"k2":[2],
},
)
results_in_list = [
{
"k1": [1],
"k2":[2],
}
]
results_in_list_first = [
{
"k2":[2],
}
]
Expand Up @@ -53,10 +53,9 @@ impl AlwaysFixableViolation for MissingWhitespace {

/// E231
pub(crate) fn missing_whitespace(line: &LogicalLine, context: &mut LogicalLinesContext) {
let mut open_parentheses = 0u32;
let mut fstrings = 0u32;
let mut prev_lsqb = TextSize::default();
let mut prev_lbrace = TextSize::default();
let mut lsqb_stack = vec![TextSize::default()];
let mut lbrace_stack = vec![TextSize::default()];
augustelalande marked this conversation as resolved.
Show resolved Hide resolved
let mut iter = line.tokens().iter().peekable();

while let Some(token) = iter.next() {
Expand All @@ -65,14 +64,16 @@ pub(crate) fn missing_whitespace(line: &LogicalLine, context: &mut LogicalLinesC
TokenKind::FStringStart => fstrings += 1,
TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1),
TokenKind::Lsqb if fstrings == 0 => {
open_parentheses = open_parentheses.saturating_add(1);
prev_lsqb = token.start();
lsqb_stack.push(token.start());
}
TokenKind::Rsqb if fstrings == 0 => {
open_parentheses = open_parentheses.saturating_sub(1);
lsqb_stack.pop();
}
TokenKind::Lbrace if fstrings == 0 => {
prev_lbrace = token.start();
lbrace_stack.push(token.start());
}
TokenKind::Rbrace if fstrings == 0 => {
lbrace_stack.pop();
}
TokenKind::Colon if fstrings > 0 => {
// Colon in f-string, no space required. This will yield false
Expand All @@ -96,9 +97,7 @@ pub(crate) fn missing_whitespace(line: &LogicalLine, context: &mut LogicalLinesC
{
if let Some(next_token) = iter.peek() {
match (kind, next_token.kind()) {
(TokenKind::Colon, _)
if open_parentheses > 0 && prev_lsqb > prev_lbrace =>
{
(TokenKind::Colon, _) if lsqb_stack.last() > lbrace_stack.last() => {
continue; // Slice syntax, no space required
}
(TokenKind::Comma, TokenKind::Rpar | TokenKind::Rsqb) => {
Expand Down
Expand Up @@ -139,6 +139,87 @@ E23.py:47:37: E231 [*] Missing whitespace after ':'
47 |+{len(f's3://{self.s3_bucket_name}/'): 1}
48 48 |
49 49 | #: Okay
50 50 | a = (1,
50 50 | a = (1,)

E23.py:60:13: E231 [*] Missing whitespace after ':'
|
58 | results = {
59 | "k1": [1],
60 | "k2":[2],
| ^ E231
61 | }
62 | results_in_tuple = (
|
= help: Add missing whitespace
augustelalande marked this conversation as resolved.
Show resolved Hide resolved

ℹ Safe fix
57 57 | """Primary function."""
58 58 | results = {
59 59 | "k1": [1],
60 |- "k2":[2],
60 |+ "k2": [2],
61 61 | }
62 62 | results_in_tuple = (
63 63 | {

E23.py:65:17: E231 [*] Missing whitespace after ':'
|
63 | {
64 | "k1": [1],
65 | "k2":[2],
| ^ E231
66 | },
67 | )
|
= help: Add missing whitespace

ℹ Safe fix
62 62 | results_in_tuple = (
63 63 | {
64 64 | "k1": [1],
65 |- "k2":[2],
65 |+ "k2": [2],
66 66 | },
67 67 | )
68 68 | results_in_list = [

E23.py:71:17: E231 [*] Missing whitespace after ':'
|
69 | {
70 | "k1": [1],
71 | "k2":[2],
| ^ E231
72 | }
73 | ]
|
= help: Add missing whitespace

ℹ Safe fix
68 68 | results_in_list = [
69 69 | {
70 70 | "k1": [1],
71 |- "k2":[2],
71 |+ "k2": [2],
72 72 | }
73 73 | ]
74 74 | results_in_list_first = [

E23.py:76:17: E231 [*] Missing whitespace after ':'
|
74 | results_in_list_first = [
75 | {
76 | "k2":[2],
| ^ E231
77 | }
78 | ]
|
= help: Add missing whitespace

ℹ Safe fix
73 73 | ]
74 74 | results_in_list_first = [
75 75 | {
76 |- "k2":[2],
76 |+ "k2": [2],
77 77 | }
78 78 | ]