@@ -1842,6 +1842,7 @@ def __init__(self, filename=None, lines=None,
1842
1842
self .max_line_length = options .max_line_length
1843
1843
self .max_doc_length = options .max_doc_length
1844
1844
self .indent_size = options .indent_size
1845
+ self .fstring_start = 0
1845
1846
self .multiline = False # in a multiline string?
1846
1847
self .hang_closing = options .hang_closing
1847
1848
self .indent_size = options .indent_size
@@ -2030,13 +2031,15 @@ def maybe_check_physical(self, token, prev_physical):
2030
2031
# if the file does not end with a newline, the NEWLINE
2031
2032
# token is inserted by the parser, but it does not contain
2032
2033
# the previous physical line in `token[4]`
2033
- if token [ 4 ] == '' :
2034
+ if token . line == '' :
2034
2035
self .check_physical (prev_physical )
2035
2036
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 ]
2037
2040
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
2040
2043
):
2041
2044
# Less obviously, a string that contains newlines is a
2042
2045
# multiline string, either triple-quoted or with internal
@@ -2053,14 +2056,18 @@ def maybe_check_physical(self, token, prev_physical):
2053
2056
# - have to wind self.line_number back because initially it
2054
2057
# points to the last line of the string, and we want
2055
2058
# check_physical() to give accurate feedback
2056
- if noqa (token [ 4 ] ):
2059
+ if noqa (token . line ):
2057
2060
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
+
2058
2067
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 ' )
2064
2071
self .line_number += 1
2065
2072
self .multiline = False
2066
2073
0 commit comments