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

Closes https://github.com/astral-sh/ruff/issues/8752.
This commit is contained in:
Charlie Marsh 2023-11-25 18:16:43 +00:00 committed by GitHub
parent 1dbfab9a0c
commit 0d4af9d3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 2 deletions

View File

@ -135,3 +135,15 @@ ham[lower + offset: :upper + offset]
#: 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 :]

View File

@ -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 },

View File

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

View File

@ -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 ':'
@ -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:]