Skip to content

Commit 703c317

Browse files
authored
Merge pull request #69 from cknd/fix_multiline_fstrings
Fix handling of multi-line f strings in python >= 3.12
2 parents 8c552a5 + 257d68e commit 703c317

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

stackprinter/source_inspection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,19 @@ def _tokenize(source_lines):
144144
head_s = None
145145
head_e = None
146146
name_end = -2
147+
acceptable_multiline_tokens = [tokenize.STRING]
148+
if hasattr(tokenize, "FSTRING_START"):
149+
# we're >= python 3.12
150+
acceptable_multiline_tokens.extend([
151+
tokenize.FSTRING_START,
152+
tokenize.FSTRING_MIDDLE,
153+
tokenize.FSTRING_END])
154+
147155
for ttype, string, (sline, scol), (eline, ecol), line in tokenizer:
148156
sline -= 1 # we deal in line indices counting from 0
149157
eline -= 1
150-
if ttype != tokenize.STRING:
158+
159+
if ttype not in acceptable_multiline_tokens:
151160
assert sline == eline, "Can't accept non-string multiline tokens"
152161

153162
if ttype == tokenize.NAME:

tests/source.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def spam_spam_spam(val):
6363
eels = "here is a "\
6464
"multi line "\
6565
"string"
66+
67+
foo = f"""here is
68+
{outer_scope_thing["and"]}
69+
a multi-line f-string,
70+
even with quotes in it
71+
"""
6672
# np.reshape(bla, 9000)
6773
try:
6874
bla.nonexistant_attribute

0 commit comments

Comments
 (0)