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

Allow space-before-colon after end-of-slice #8838

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@

#: E203:1:20
ham[{lower + offset : upper + offset} : upper + offset]

#: Okay
ham[upper:]

#: Okay
ham[upper :]

#: E202:1:12
ham[upper : ]

#: E203:1:10
ham[upper :]
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,28 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin
diagnostic.range(),
)));
context.push_diagnostic(diagnostic);
} else if iter
.peek()
.is_some_and(|token| token.kind() == TokenKind::Rsqb)
{
// Allow `foo[1 :]`, but not `foo[1 :]`.
if let (Whitespace::Many | Whitespace::Tab, offset) = whitespace
{
let mut diagnostic = Diagnostic::new(
WhitespaceBeforePunctuation { symbol },
TextRange::at(token.start() - offset, offset),
);
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(
diagnostic.range(),
)));
context.push_diagnostic(diagnostic);
}
} else {
// Allow, e.g., `foo[1:2]` or `foo[1 : 2]` or `foo[1 :: 2]`.
let token = iter
.peek()
.filter(|next| matches!(next.kind(), TokenKind::Colon))
.unwrap_or(&token);

// Allow, e.g., `foo[1:2]` or `foo[1 : 2]` or `foo[1 :: 2]`.
if line.trailing_whitespace(token) != whitespace {
let mut diagnostic = Diagnostic::new(
WhitespaceBeforePunctuation { symbol },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,24 @@ E20.py:90:18: E202 [*] Whitespace before ']'
92 92 |
93 93 | #: Okay

E20.py:146:12: E202 [*] Whitespace before ']'
|
145 | #: E202:1:12
146 | ham[upper : ]
| ^ E202
147 |
148 | #: E203:1:10
|
= help: Remove whitespace before ']'

ℹ Safe fix
143 143 | ham[upper :]
144 144 |
145 145 | #: E202:1:12
146 |-ham[upper : ]
146 |+ham[upper :]
147 147 |
148 148 | #: E203:1:10
149 149 | ham[upper :]


Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ E20.py:137:20: E203 [*] Whitespace before ':'
136 | #: E203:1:20
137 | ham[{lower + offset : upper + offset} : upper + offset]
| ^ E203
138 |
139 | #: Okay
|
= help: Remove whitespace before ':'

Expand All @@ -220,5 +222,23 @@ E20.py:137:20: E203 [*] Whitespace before ':'
136 136 | #: E203:1:20
137 |-ham[{lower + offset : upper + offset} : upper + offset]
137 |+ham[{lower + offset: upper + offset} : upper + offset]
138 138 |
139 139 | #: Okay
140 140 | ham[upper:]

E20.py:149:10: E203 [*] Whitespace before ':'
|
148 | #: E203:1:10
149 | ham[upper :]
| ^^ E203
|
= help: Remove whitespace before ':'

ℹ Safe fix
146 146 | ham[upper : ]
147 147 |
148 148 | #: E203:1:10
149 |-ham[upper :]
149 |+ham[upper:]


Loading