Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed Mar 19, 2024
1 parent 938118b commit 6e779de
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
30 changes: 29 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/pycodestyle/E23.py
Original file line number Diff line number Diff line change
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],
}
]
Original file line number Diff line number Diff line change
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()];
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
Original file line number Diff line number Diff line change
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

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 | ]

0 comments on commit 6e779de

Please sign in to comment.