Skip to content

Commit ca2b44b

Browse files
committed
fix: include workaround to detect f-string in Python < 3.12
1 parent 3076b7c commit ca2b44b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/docformatter/classify.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ def is_f_string(token: tokenize.TokenInfo, prev_token: tokenize.TokenInfo) -> bo
317317
if PY312:
318318
if tokenize.FSTRING_MIDDLE in [token.type, prev_token.type]:
319319
return True
320+
elif any(
321+
[
322+
token.string.startswith('f"""'),
323+
prev_token.string.startswith('f"""'),
324+
token.string.startswith("f'''"),
325+
prev_token.string.startswith("f'''"),
326+
]
327+
):
328+
return True
320329

321330
return False
322331

tests/_data/string_files/do_format_code.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,3 +1213,23 @@ expected='''class Foo:
12131213
"""Baz."""
12141214
...
12151215
'''
1216+
1217+
[do_not_break_f_string_double_quotes]
1218+
source='''foo = f"""
1219+
bar
1220+
"""
1221+
'''
1222+
expected='''foo = f"""
1223+
bar
1224+
"""
1225+
'''
1226+
1227+
[do_not_break_f_string_single_quotes]
1228+
source="""foo = f'''
1229+
bar
1230+
'''
1231+
"""
1232+
expected="""foo = f'''
1233+
bar
1234+
'''
1235+
"""

tests/formatter/test_do_format_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
("two_lines_between_stub_classes", NO_ARGS),
139139
("two_lines_between_stub_classes_with_preceding_comment", NO_ARGS),
140140
("ellipses_is_code_line", NO_ARGS),
141+
("do_not_break_f_string_double_quotes", NO_ARGS),
142+
("do_not_break_f_string_single_quotes", NO_ARGS),
141143
],
142144
)
143145
def test_do_format_code(test_key, test_args, args):

0 commit comments

Comments
 (0)