@@ -39,29 +39,31 @@ def expr_term(self, args: List) -> Any:
39
39
40
40
def index_expr_term (self , args : List ) -> str :
41
41
args = self .strip_new_line_tokens (args )
42
- return "%s%s" % ( str ( args [0 ]), str ( args [1 ]))
42
+ return f" { args [0 ]} { args [1 ]} "
43
43
44
44
def index (self , args : List ) -> str :
45
45
args = self .strip_new_line_tokens (args )
46
- return "[%s]" % ( str ( args [0 ]))
46
+ return f"[ { args [0 ]} ]"
47
47
48
48
def get_attr_expr_term (self , args : List ) -> str :
49
- return "%s%s" % ( str ( args [0 ]), str ( args [1 ]))
49
+ return f" { args [0 ]} { args [1 ]} "
50
50
51
51
def get_attr (self , args : List ) -> str :
52
- return ".%s" % args [0 ]
52
+ return f". { args [0 ]} "
53
53
54
54
def attr_splat_expr_term (self , args : List ) -> str :
55
- return "%s%s" % ( args [0 ], args [1 ])
55
+ return f" { args [0 ]} { args [1 ]} "
56
56
57
57
def attr_splat (self , args : List ) -> str :
58
- return ".*%s" % ("" .join (args ))
58
+ args_str = "" .join (str (arg ) for arg in args )
59
+ return f".*{ args_str } "
59
60
60
61
def full_splat_expr_term (self , args : List ) -> str :
61
- return "%s%s" % ( str ( args [0 ]), str ( args [1 ]))
62
+ return f" { args [0 ]} { args [1 ]} "
62
63
63
64
def full_splat (self , args : List ) -> str :
64
- return "[*]%s" % ("" .join (args ))
65
+ args_str = "" .join (str (arg ) for arg in args )
66
+ return f"[*]{ args_str } "
65
67
66
68
def tuple (self , args : List ) -> List :
67
69
return [self .to_string_dollar (arg ) for arg in self .strip_new_line_tokens (args )]
@@ -88,7 +90,7 @@ def function_call(self, args: List) -> str:
88
90
args_str = ''
89
91
if len (args ) > 1 :
90
92
args_str = ", " .join ([str (arg ) for arg in args [1 ]])
91
- return "%s(%s)" % ( str ( args [0 ]), args_str )
93
+ return f" { args [0 ]} ( { args_str } )"
92
94
93
95
def arguments (self , args : List ) -> List :
94
96
return args
@@ -127,7 +129,7 @@ def attribute(self, args: List) -> Attribute:
127
129
128
130
def conditional (self , args : List ) -> str :
129
131
args = self .strip_new_line_tokens (args )
130
- return "%s ? %s : %s" % ( args [0 ], args [1 ], args [2 ])
132
+ return f" { args [0 ]} ? { args [1 ]} : { args [2 ]} "
131
133
132
134
def binary_op (self , args : List ) -> str :
133
135
return " " .join ([str (arg ) for arg in args ])
@@ -162,7 +164,7 @@ def body(self, args: List) -> Dict[str, List]:
162
164
for arg in args :
163
165
if isinstance (arg , Attribute ):
164
166
if arg .key in result :
165
- raise RuntimeError ("{ } already defined". format ( arg . key ) )
167
+ raise RuntimeError (f" { arg . key } already defined" )
166
168
result [arg .key ] = arg .value
167
169
attributes .add (arg .key )
168
170
else :
@@ -171,7 +173,7 @@ def body(self, args: List) -> Dict[str, List]:
171
173
key = str (key )
172
174
if key in result :
173
175
if key in attributes :
174
- raise RuntimeError ("{ } already defined". format ( key ) )
176
+ raise RuntimeError (f" { key } already defined" )
175
177
result [key ].append (value )
176
178
else :
177
179
result [key ] = [value ]
@@ -188,8 +190,8 @@ def binary_operator(self, args: List) -> str:
188
190
def heredoc_template (self , args : List ) -> str :
189
191
match = HEREDOC_PATTERN .match (str (args [0 ]))
190
192
if not match :
191
- raise RuntimeError ("Invalid Heredoc token: %s" % args [0 ])
192
- return '"%s"' % match .group (2 )
193
+ raise RuntimeError (f "Invalid Heredoc token: { args [0 ]} " )
194
+ return f" \" { match .group (2 )} \" "
193
195
194
196
def heredoc_template_trim (self , args : List ) -> str :
195
197
# See https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions
@@ -198,7 +200,7 @@ def heredoc_template_trim(self, args: List) -> str:
198
200
# and then remove that number of spaces from each line
199
201
match = HEREDOC_TRIM_PATTERN .match (str (args [0 ]))
200
202
if not match :
201
- raise RuntimeError ("Invalid Heredoc token: %s" % args [0 ])
203
+ raise RuntimeError (f "Invalid Heredoc token: { args [0 ]} " )
202
204
203
205
text = match .group (2 )
204
206
lines = text .split ('\n ' )
@@ -220,7 +222,7 @@ def new_line_or_comment(self, args: List) -> Discard:
220
222
def for_tuple_expr (self , args : List ) -> str :
221
223
args = self .strip_new_line_tokens (args )
222
224
for_expr = " " .join ([str (arg ) for arg in args [1 :- 1 ]])
223
- return '[%s]' % for_expr
225
+ return f"[ { for_expr } ]"
224
226
225
227
def for_intro (self , args : List ) -> str :
226
228
args = self .strip_new_line_tokens (args )
@@ -233,7 +235,10 @@ def for_cond(self, args: List) -> str:
233
235
def for_object_expr (self , args : List ) -> str :
234
236
args = self .strip_new_line_tokens (args )
235
237
for_expr = " " .join ([str (arg ) for arg in args [1 :- 1 ]])
236
- return '{%s}' % for_expr
238
+ # doubled curly braces stands for inlining the braces
239
+ # and the third pair of braces is for the interpolation
240
+ # e.g. f"{2 + 2} {{2 + 2}}" == "4 {2 + 2}"
241
+ return f"{{{ for_expr } }}"
237
242
238
243
def strip_new_line_tokens (self , args : List ) -> List :
239
244
"""
@@ -247,7 +252,7 @@ def to_string_dollar(self, value: Any) -> Any:
247
252
if isinstance (value , str ):
248
253
if value .startswith ('"' ) and value .endswith ('"' ):
249
254
return str (value )[1 :- 1 ]
250
- return '${%s}' % value
255
+ return f"${{ { value } }}"
251
256
return value
252
257
253
258
def strip_quotes (self , value : Any ) -> Any :
0 commit comments