Skip to content

Commit 0be8a22

Browse files
committed
Recognise debug specifier with a format spec
1 parent 4803e90 commit 0be8a22

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/python_minifier/f_string.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def is_correct_ast(self, code):
3737
except Exception as e:
3838
return False
3939

40+
def complete_debug_specifier(self, partial_specifier_candidates, value_node):
41+
assert isinstance(value_node, ast.FormattedValue)
42+
43+
conversion = ''
44+
if value_node.conversion == 115:
45+
conversion = '!s'
46+
elif value_node.conversion == 114 and value_node.format_spec is not None:
47+
# This is the default for debug specifiers, unless there's a format_spec
48+
conversion = '!r'
49+
elif value_node.conversion == 97:
50+
conversion = '!a'
51+
52+
conversion_candidates = [x + conversion for x in partial_specifier_candidates]
53+
54+
if value_node.format_spec is not None:
55+
conversion_candidates = [c + ':' + fs for c in conversion_candidates for fs in FormatSpec(value_node.format_spec, self.allowed_quotes).candidates()]
56+
57+
return [x + '}' for x in conversion_candidates]
58+
4059
def candidates(self):
4160
actual_candidates = []
4261

@@ -54,7 +73,7 @@ def candidates(self):
5473
if debug_specifier:
5574
# Maybe!
5675
try:
57-
debug_specifier_candidates = [x + '{' + v.s + '}' for x in candidates]
76+
debug_specifier_candidates = [x + '{' + v.s for x in candidates]
5877
except Exception as e:
5978
continue
6079

@@ -64,9 +83,10 @@ def candidates(self):
6483
continue
6584
elif isinstance(v, ast.FormattedValue):
6685
try:
86+
completed = self.complete_debug_specifier(debug_specifier_candidates, v)
6787
candidates = [
6888
x + y for x in candidates for y in FormattedValue(v, nested_allowed).get_candidates()
69-
] + debug_specifier_candidates
89+
] + completed
7090
debug_specifier_candidates = []
7191
except Exception as e:
7292
continue

0 commit comments

Comments
 (0)