handle multiline fstrings in 3.12

This commit is contained in:
Anthony Sottile 2023-07-29 14:25:54 -04:00
parent f2641954c4
commit 1ed78d592a
5 changed files with 83 additions and 53 deletions

View file

@ -199,6 +199,38 @@ t.py:3:1: T001 '"""\\n'
assert out == expected
def test_physical_line_plugin_multiline_fstring(tmpdir, capsys):
cfg_s = f"""\
[flake8:local-plugins]
extension =
T = {yields_physical_line.__module__}:{yields_physical_line.__name__}
"""
cfg = tmpdir.join("tox.ini")
cfg.write(cfg_s)
src = '''\
y = 1
x = f"""
hello {y}
"""
'''
t_py = tmpdir.join("t.py")
t_py.write_binary(src.encode())
with tmpdir.as_cwd():
assert main(("t.py", "--config", str(cfg))) == 1
expected = '''\
t.py:1:1: T001 'y = 1\\n'
t.py:2:1: T001 'x = f"""\\n'
t.py:3:1: T001 'hello {y}\\n'
t.py:4:1: T001 '"""\\n'
'''
out, err = capsys.readouterr()
assert out == expected
def yields_logical_line(logical_line):
yield 0, f"T001 {logical_line!r}"

View file

@ -275,13 +275,15 @@ def test_processor_split_line(default_options):
(3, 3),
'x = """\ncontents\n"""\n',
)
expected = [('x = """\n', 0), ("contents\n", 1)]
expected = [('x = """\n', 1, True), ("contents\n", 2, True)]
assert file_processor.multiline is False
actual = [
(line, file_processor.line_number)
for line in file_processor.split_line(token)
(line, file_processor.line_number, file_processor.multiline)
for line in file_processor.multiline_string(token)
]
assert file_processor.multiline is False
assert expected == actual
assert file_processor.line_number == 2
assert file_processor.line_number == 3
def test_build_ast(default_options):
@ -321,21 +323,6 @@ def test_visited_new_blank_line(default_options):
assert file_processor.blank_lines == 1
def test_inside_multiline(default_options):
"""Verify we update the line number and reset multiline."""
file_processor = processor.FileProcessor(
"-", default_options, lines=["a = 1\n"]
)
assert file_processor.multiline is False
assert file_processor.line_number == 0
with file_processor.inside_multiline(10):
assert file_processor.multiline is True
assert file_processor.line_number == 10
assert file_processor.multiline is False
@pytest.mark.parametrize(
"string, expected",
[