2222# Allowed
2323f_single_chained_attr = "f'{attr1.attr2}'"
2424f_variable_lookup = "f'smth {value}'"
25+ f_multi_variable_lookup = "f'smth {value1} {value2} {value3}'"
2526f_dict_lookup_str_key = 'f\' smth {dict_value["key"]}\' '
2627f_list_index_lookup = "f'smth {list_value[0]}'"
2728f_function_empty_args = "f'smth {user.get_full_name()}'"
3334f_function_with_single_arg = "f'smth {func(arg)}'"
3435f_function_with_three_args = "f'{func(arg1, arg2, arg3)}'"
3536f_method_with_three_args = "f'{obj.method(arg1, arg2, arg3)}'"
37+ # Allowed format specifiers
38+ f_format_str = "f'{value!s}'"
39+ f_format_repr = "f'{value!r}'"
40+ f_format_code = "f'{value!a}'"
41+ f_format_hex_lower_short = "f'{value:x}'"
42+ f_format_hex_upper_short = "f'{value:X}'"
43+ f_format_hex_lower_long = "f'{value:#x}'"
44+ f_format_hex_upper_long = "f'{value:#X}'"
45+ f_format_char = "f'{value:c}'"
46+ f_format_rounded = "f'{value:.123456f}'"
47+ f_format_scientific = "f'{value:.456789e}'"
48+ f_format_var_single = "f'{value:{fmt}}'"
49+ f_format_var_single2 = "f'{value1:{fmt1}} {value2:{fmt2}}'"
50+ f_format_var_single_index = "f'{value:{fmt[0]}}'"
51+ f_format_var_single_call = "f'{value:{fmt()}}'"
52+ f_format_convertions = "f'{value1!r} {value2!s} {value3!a}'"
3653
3754# Disallowed
3855f_string = "f'x + y = {2 + 2}'"
5370f_single_chained_functions = "f'{f1().f2()}'"
5471f_function_with_four_args = "f'{func(arg1, arg2, arg3, arg4)}'"
5572f_method_with_four_args = "f'{obj.meth(arg1, arg2, arg3, arg4)}-post'"
73+ f_nested_string = "f'{f\" {value}\" }'"
74+ # Disallowed format specifiers
75+ f_format_var_multi = "f'pre {value:{fmt1}{fmt2}}'"
76+ f_format_var_chain = "f'{value:{fmt.attr.attr}}'"
77+ f_format_var_before1 = "f'{value:{fmt}10}'"
78+ f_format_var_before2 = "f'{value:{fmt}.4f}'"
79+ f_format_var_after1 = "f'{value:_{fmt}}'"
80+ f_format_var_after2 = "f'{value:_^{fmt}}'"
81+ f_format_var_between1 = "f'{value:_{fmt}10}'"
82+ f_format_var_between2 = "f'{value:.{precision}f}'"
83+ f_format_var_around = "f'{value:{fmt1}^{fmt2}}'"
84+ f_format_str_const = "f'{value!s:10}'"
85+ f_format_repr_const = "f'{value!r:10}'"
86+ f_format_code_const = "f'{value!a:10}'"
87+ f_format_str_var = "f'{value!s:{fmt}}'"
88+ f_format_repr_var = "f'{value!r:{fmt}}'"
89+ f_format_code_var = "f'{value!a:{fmt}}'"
90+ f_format_hex_lower_short_const = "f'{value:10x}'"
91+ f_format_hex_upper_short_const = "f'{value:10X}'"
92+ f_format_hex_lower_long_const = "f'{value:#10x}'"
93+ f_format_hex_upper_long_const = "f'{value:#10X}'"
94+ f_format_char_const = "f'{value:10c}'"
95+ f_format_round_const = "f'{value:10.4f}'"
96+ f_format_scientific_const = "f'{value:10.7e}'"
97+ f_format_useless1 = "f'{value:_}'"
98+ f_format_useless2 = "f'{value:_<}'"
5699
57100# regression 1921
58101f_string_comma_format = 'f"Count={count:,}"'
@@ -103,6 +146,33 @@ def test_string_normal(
103146 f_calling_returned_function ,
104147 f_single_chained_functions ,
105148 f_function_with_four_args ,
149+ f_method_with_four_args ,
150+ f_nested_string ,
151+ # format specifiers
152+ f_format_var_multi ,
153+ f_format_var_chain ,
154+ f_format_var_before1 ,
155+ f_format_var_before2 ,
156+ f_format_var_after1 ,
157+ f_format_var_after2 ,
158+ f_format_var_between1 ,
159+ f_format_var_between2 ,
160+ f_format_var_around ,
161+ f_format_str_const ,
162+ f_format_repr_const ,
163+ f_format_code_const ,
164+ f_format_str_var ,
165+ f_format_repr_var ,
166+ f_format_code_var ,
167+ f_format_hex_lower_short_const ,
168+ f_format_hex_upper_short_const ,
169+ f_format_hex_lower_long_const ,
170+ f_format_hex_upper_long_const ,
171+ f_format_char_const ,
172+ f_format_round_const ,
173+ f_format_scientific_const ,
174+ f_format_useless1 ,
175+ f_format_useless2 ,
106176 ],
107177)
108178def test_complex_f_string (assert_errors , parse_ast_tree , code , default_options ):
@@ -125,6 +195,7 @@ def test_complex_f_string(assert_errors, parse_ast_tree, code, default_options):
125195 f_function_empty_args ,
126196 f_list_index_lookup ,
127197 f_variable_lookup ,
198+ f_multi_variable_lookup ,
128199 f_single_chained_attr ,
129200 f_attr_on_function ,
130201 f_true_index ,
@@ -135,6 +206,22 @@ def test_complex_f_string(assert_errors, parse_ast_tree, code, default_options):
135206 f_function_with_single_arg ,
136207 f_function_with_three_args ,
137208 f_method_with_three_args ,
209+ # format specifiers
210+ f_format_str ,
211+ f_format_repr ,
212+ f_format_code ,
213+ f_format_hex_lower_short ,
214+ f_format_hex_upper_short ,
215+ f_format_hex_lower_long ,
216+ f_format_hex_upper_long ,
217+ f_format_char ,
218+ f_format_rounded ,
219+ f_format_scientific ,
220+ f_format_var_single ,
221+ f_format_var_single2 ,
222+ f_format_var_single_index ,
223+ f_format_var_single_call ,
224+ f_format_convertions ,
138225 ],
139226)
140227def test_simple_f_string (assert_errors , parse_ast_tree , code , default_options ):
0 commit comments