Fix COM812 false positive in string subscript (#4493)

This commit is contained in:
Jonathan Plasse 2023-05-18 16:35:41 +02:00 committed by GitHub
parent 73efbeb581
commit 0e4d174551
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

View file

@ -631,3 +631,11 @@ result = function(
the_first_one = next(
(i for i in range(10) if i // 2 == 0) # COM812 fix should include the final bracket
)
foo = namedtuple(
name="foo",
status="bar",
message="sfdsdfsdgs fsdfsdf output!dsfdfsdjkg ghfskdjghkdssd sd fsdf s\n"[
:20
],
)

View file

@ -27,6 +27,7 @@ enum TokenType {
Def,
Lambda,
Colon,
String,
}
/// Simplified token specialized for the task.
@ -55,6 +56,7 @@ impl<'tok> Token<'tok> {
// Import treated like a function.
Tok::Import => TokenType::Named,
Tok::Name { .. } => TokenType::Named,
Tok::String { .. } => TokenType::String,
Tok::Comma => TokenType::Comma,
Tok::Lpar => TokenType::OpeningBracket,
Tok::Lsqb => TokenType::OpeningSquareBracket,
@ -265,7 +267,7 @@ pub(crate) fn trailing_commas(
}
},
TokenType::OpeningSquareBracket => match prev.type_ {
TokenType::ClosingBracket | TokenType::Named => {
TokenType::ClosingBracket | TokenType::Named | TokenType::String => {
stack.push(Context::new(ContextType::Subscript));
}
_ => {

View file

@ -936,5 +936,7 @@ COM81.py:632:42: COM812 [*] Trailing comma missing
632 |- (i for i in range(10) if i // 2 == 0) # COM812 fix should include the final bracket
632 |+ (i for i in range(10) if i // 2 == 0), # COM812 fix should include the final bracket
633 633 | )
634 634 |
635 635 | foo = namedtuple(