Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fmt_spec():
code = """"my string {:.2f}".format(var)"""
expected = '''f"""my string {var:.2f}"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_expr_no_paren():
code = """"my string {:.2f}".format(var+1)"""
expected = '''f"""my string {var + 1:.2f}"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_fix_fstrings(s, expected):
new, changed = transform_chunk(s)
assert changed
assert new == expected
def test_fix_fstrings_noop(s):
new, changed = transform_chunk(s)
assert new == s
assert not changed
def test_digit_grouping():
code = """"Failed after {:,}".format(x)"""
expected = '''f"""Failed after {x:,}"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_newline():
code = r""""echo '{}'\n".format(self.FLUSH_CMD)"""
expected = '''f"""echo '{self.FLUSH_CMD}'\\n"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_mixed_numbered():
code = (
""""Flask Documentation ({1} {0:.2f} {name})".format(version,sprt,name=NAME)"""
)
expected = '''f"""Flask Documentation ({sprt} {version:.2f} {NAME})"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_digit_grouping_2():
code = """
"Search: finished in {0:,} ms.".format(vm.search_time_elapsed_ms)
""".strip()
expected = '''
f"""Search: finished in {vm.search_time_elapsed_ms:,} ms."""
'''.strip()
new, changed = transform_chunk(code)
assert changed
assert new == expected
def test_multiline():
code = """
"Flask Documentation ({})".format(
version
)
""".strip()
expected = '''f"""Flask Documentation ({version})"""'''
new, changed = transform_chunk(code)
assert changed
assert new == expected
def fstringify_code_by_line(code: str, multiline=True, len_limit=88) -> Tuple[str, int]:
""" returns fstringified version of the code and amount of lines edited."""
return _transform_code(
code, split.get_fstringify_chunks, transform_chunk, multiline, len_limit
)