-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update logical line rules for the new f-string tokens #7690
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,7 @@ pub(crate) fn missing_whitespace_around_operator( | |
prev_token.kind(), | ||
TokenKind::Lpar | TokenKind::Lambda | ||
)); | ||
let mut fstrings = u32::from(matches!(prev_token.kind(), TokenKind::FStringStart)); | ||
|
||
while let Some(token) = tokens.next() { | ||
let kind = token.kind(); | ||
|
@@ -150,13 +151,15 @@ pub(crate) fn missing_whitespace_around_operator( | |
} | ||
|
||
match kind { | ||
TokenKind::FStringStart => fstrings += 1, | ||
TokenKind::FStringEnd => fstrings = fstrings.saturating_sub(1), | ||
TokenKind::Lpar | TokenKind::Lambda => parens += 1, | ||
TokenKind::Rpar => parens = parens.saturating_sub(1), | ||
_ => {} | ||
}; | ||
|
||
let needs_space = if kind == TokenKind::Equal && parens > 0 { | ||
// Allow keyword args or defaults: foo(bar=None). | ||
let needs_space = if kind == TokenKind::Equal && (parens > 0 || fstrings > 0) { | ||
// Allow keyword args, defaults: foo(bar=None) and f-strings: f'{foo=}' | ||
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. Again, should we just have something like: if fstrings > 0 {
continue;
} Or is it important that we run this body logic? 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. Oh, so we're trying to catch some of these errors within f-strings, but not all -- is that right? 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. Yes, it's only when the equal sign is present which in f-string context will be used as debug expression. So, |
||
NeedsSpace::No | ||
} else if kind == TokenKind::Slash { | ||
// Tolerate the "/" operator in function definition | ||
|
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.
Should we just skip all of this when we're inside an f-string?
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, or actually, we do want to catch some errors within f-strings -- is that right?
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.
Removing whitespaces before the punctuations should be fine. It's only when they're removed after when the semantics changes.
So,
f"{foo :.3f}"
andf"{foo:.3f}"
are equivalent.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.
Yes