diff --git a/src/flynt/transform/percent_transformer.py b/src/flynt/transform/percent_transformer.py index 7dfe62e..d296091 100644 --- a/src/flynt/transform/percent_transformer.py +++ b/src/flynt/transform/percent_transformer.py @@ -11,7 +11,7 @@ FORMAT_GROUP = f"[hlL]?[{FORMATS}]" FORMAT_GROUP_MATCH = f"[hlL]?([{FORMATS}])" -PREFIX_GROUP = "[0-9]*[.]?[0-9]*" +PREFIX_GROUP = "[+-]?[0-9]*[.]?[0-9]*" ANY_DICT = re.compile(r"(?{fmt_prefix}", + conversion=conversion_methods[fmt_spec], + ) if not aggressive and fmt_prefix: raise ConversionRefused( "Default text alignment has changed between percent fmt and fstrings. " diff --git a/test/test_edits.py b/test/test_edits.py index 7cbb503..64500da 100644 --- a/test/test_edits.py +++ b/test/test_edits.py @@ -40,6 +40,24 @@ def test_call(state: State): def test_string_specific_len(state: State): s_in = """'%5s' % CLASS_NAMES[labels[j]]""" + s_expected = """f'{CLASS_NAMES[labels[j]]:>5}'""" + + state.aggressive = True + s_out, count = code_editor.fstringify_code_by_line(s_in, state) + assert s_out == s_expected + + +def test_string_specific_len_right_aligned(state: State): + s_in = """'%5s' % CLASS_NAMES[labels[j]]""" + s_expected = """f'{CLASS_NAMES[labels[j]]:>5}'""" + + state.aggressive = True + s_out, count = code_editor.fstringify_code_by_line(s_in, state) + assert s_out == s_expected + + +def test_string_specific_len_left_aligned(state: State): + s_in = """'%-5s' % CLASS_NAMES[labels[j]]""" s_expected = """f'{CLASS_NAMES[labels[j]]:5}'""" state.aggressive = True