-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 whitespace around colon in slices for whitespace-before-punctuation
(E203
)
#8654
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -127,14 +127,28 @@ impl AlwaysFixableViolation for WhitespaceBeforePunctuation { | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// E201, E202, E203 | ||||||||||||||||||||||||||||||||||||||
pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLinesContext) { | ||||||||||||||||||||||||||||||||||||||
let mut prev_token = None; | ||||||||||||||||||||||||||||||||||||||
let mut fstrings = 0u32; | ||||||||||||||||||||||||||||||||||||||
let mut brackets = vec![]; | ||||||||||||||||||||||||||||||||||||||
let mut prev_token = None; | ||||||||||||||||||||||||||||||||||||||
let mut iter = line.tokens().iter().peekable(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
for token in line.tokens() { | ||||||||||||||||||||||||||||||||||||||
while let Some(token) = iter.next() { | ||||||||||||||||||||||||||||||||||||||
let kind = token.kind(); | ||||||||||||||||||||||||||||||||||||||
match kind { | ||||||||||||||||||||||||||||||||||||||
TokenKind::FStringStart => fstrings += 1, | ||||||||||||||||||||||||||||||||||||||
TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1), | ||||||||||||||||||||||||||||||||||||||
TokenKind::Lsqb if fstrings == 0 => { | ||||||||||||||||||||||||||||||||||||||
brackets.push(kind); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand what has changed or how the new detection should work, which is why I'm unable to come up with a good example. Is it possible that this change does not work as intended for parenthesized expressions inside f-strings because they are in between the start end token (fstrings > 0)? f"start of f-string {a[(the_paren_logic) :b]} end of fstring" |
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
TokenKind::Rsqb if fstrings == 0 => { | ||||||||||||||||||||||||||||||||||||||
brackets.pop(); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
TokenKind::Lbrace if fstrings == 0 => { | ||||||||||||||||||||||||||||||||||||||
brackets.push(kind); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
TokenKind::Rbrace if fstrings == 0 => { | ||||||||||||||||||||||||||||||||||||||
brackets.pop(); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+140
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit
Suggested change
|
||||||||||||||||||||||||||||||||||||||
_ => {} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
if let Some(symbol) = BracketOrPunctuation::from_kind(kind) { | ||||||||||||||||||||||||||||||||||||||
|
@@ -177,16 +191,56 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin | |||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
BracketOrPunctuation::Punctuation(symbol) => { | ||||||||||||||||||||||||||||||||||||||
if !matches!(prev_token, Some(TokenKind::Comma)) { | ||||||||||||||||||||||||||||||||||||||
let whitespace = line.leading_whitespace(token); | ||||||||||||||||||||||||||||||||||||||
if let (Whitespace::Single | Whitespace::Many | Whitespace::Tab, offset) = | ||||||||||||||||||||||||||||||||||||||
line.leading_whitespace(token) | ||||||||||||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||||||||||||
// If we're in a slice, and the token is a colon, and it has | ||||||||||||||||||||||||||||||||||||||
// equivalent spacing on both sides, allow it. | ||||||||||||||||||||||||||||||||||||||
if symbol == ':' | ||||||||||||||||||||||||||||||||||||||
&& brackets | ||||||||||||||||||||||||||||||||||||||
.last() | ||||||||||||||||||||||||||||||||||||||
.is_some_and(|kind| matches!(kind, TokenKind::Lsqb)) | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+201
to
+203
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
|
||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
// If we're in the second half of a double colon, disallow | ||||||||||||||||||||||||||||||||||||||
// any whitespace (e.g., `foo[1: :2]` or `foo[1 : : 2]`). | ||||||||||||||||||||||||||||||||||||||
if matches!(prev_token, Some(TokenKind::Colon)) { | ||||||||||||||||||||||||||||||||||||||
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 { | ||||||||||||||||||||||||||||||||||||||
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 }, | ||||||||||||||||||||||||||||||||||||||
TextRange::at(token.start() - offset, offset), | ||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion( | ||||||||||||||||||||||||||||||||||||||
diagnostic.range(), | ||||||||||||||||||||||||||||||||||||||
))); | ||||||||||||||||||||||||||||||||||||||
context.push_diagnostic(diagnostic); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this now be not okay, since the spacing is different on the other side?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intent was to allow either this or the variant with the space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh okay, interesting choice