Skip to content

Commit 3c6f86b

Browse files
authored
Merge pull request #1176 from PyCQA/noqa-fstrings-312
handle noqa for multiline fstrings in 3.12
2 parents 27d2add + 7701377 commit 3c6f86b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

pycodestyle.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ def __init__(self, filename=None, lines=None,
18421842
self.max_line_length = options.max_line_length
18431843
self.max_doc_length = options.max_doc_length
18441844
self.indent_size = options.indent_size
1845+
self.fstring_start = 0
18451846
self.multiline = False # in a multiline string?
18461847
self.hang_closing = options.hang_closing
18471848
self.indent_size = options.indent_size
@@ -2030,13 +2031,15 @@ def maybe_check_physical(self, token, prev_physical):
20302031
# if the file does not end with a newline, the NEWLINE
20312032
# token is inserted by the parser, but it does not contain
20322033
# the previous physical line in `token[4]`
2033-
if token[4] == '':
2034+
if token.line == '':
20342035
self.check_physical(prev_physical)
20352036
else:
2036-
self.check_physical(token[4])
2037+
self.check_physical(token.line)
2038+
elif token.type == FSTRING_START: # pragma: >=3.12 cover
2039+
self.fstring_start = token.start[0]
20372040
elif (
2038-
token[0] in {tokenize.STRING, FSTRING_MIDDLE} and
2039-
'\n' in token[1]
2041+
token.type == tokenize.STRING and '\n' in token.string or
2042+
token.type == FSTRING_END
20402043
):
20412044
# Less obviously, a string that contains newlines is a
20422045
# multiline string, either triple-quoted or with internal
@@ -2053,14 +2056,18 @@ def maybe_check_physical(self, token, prev_physical):
20532056
# - have to wind self.line_number back because initially it
20542057
# points to the last line of the string, and we want
20552058
# check_physical() to give accurate feedback
2056-
if noqa(token[4]):
2059+
if noqa(token.line):
20572060
return
2061+
if token.type == FSTRING_END: # pragma: >=3.12 cover
2062+
start = self.fstring_start
2063+
else:
2064+
start = token.start[0]
2065+
end = token.end[0]
2066+
20582067
self.multiline = True
2059-
self.line_number = token[2][0]
2060-
_, src, (_, offset), _, _ = token
2061-
src = self.lines[self.line_number - 1][:offset] + src
2062-
for line in src.split('\n')[:-1]:
2063-
self.check_physical(line + '\n')
2068+
self.line_number = start
2069+
for line_number in range(start, end):
2070+
self.check_physical(self.lines[line_number - 1] + '\n')
20642071
self.line_number += 1
20652072
self.multiline = False
20662073

testing/data/noqa.py

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
a = 1
1313
if a == None: # noqa
1414
pass
15+
16+
# should silence E501
17+
s = f'''
18+
loong {y} looooooooooooooong loooooooooooooong looooooooong loooooooong looooooooong
19+
{x}
20+
''' # noqa
1521
#:

0 commit comments

Comments
 (0)