3636
3737import argparse
3838import json
39- import os
40- import subprocess
39+ import subprocess # nosec B404
4140import sys
4241import tempfile
4342import textwrap
@@ -144,7 +143,7 @@ def run_version(version: str, paths: list[Path]) -> dict[str, Any]:
144143 * str_paths ,
145144 ]
146145 try :
147- result = subprocess .run (
146+ result = subprocess .run ( # nosec B603
148147 cmd ,
149148 capture_output = True ,
150149 text = True ,
@@ -173,7 +172,8 @@ def run_version(version: str, paths: list[Path]) -> dict[str, Any]:
173172 sys .exit (2 )
174173
175174 try :
176- return json .loads (result .stdout )
175+ parsed : dict [str , Any ] = json .loads (result .stdout )
176+ return parsed
177177 except json .JSONDecodeError as exc :
178178 print (
179179 f"{ C .RED } Error:{ C .RESET } invalid JSON from nac-yaml=={ version } : { exc } " ,
@@ -182,9 +182,7 @@ def run_version(version: str, paths: list[Path]) -> dict[str, Any]:
182182 sys .exit (2 )
183183
184184
185- def diff_values (
186- old : Any , new : Any , path : str = "$"
187- ) -> list [dict [str , Any ]]:
185+ def diff_values (old : Any , new : Any , path : str = "$" ) -> list [dict [str , Any ]]:
188186 """Recursively compare two structures and return a list of differences.
189187
190188 Args:
@@ -215,29 +213,35 @@ def diff_values(
215213 diffs .extend (diff_values (old [key ], new [key ], f"{ path } .{ key } " ))
216214 elif isinstance (old , list ):
217215 if len (old ) != len (new ):
218- diffs .append ({
219- "path" : path ,
220- "type" : "list_length" ,
221- "old" : len (old ),
222- "new" : len (new ),
223- })
216+ diffs .append (
217+ {
218+ "path" : path ,
219+ "type" : "list_length" ,
220+ "old" : len (old ),
221+ "new" : len (new ),
222+ }
223+ )
224224 for i in range (min (len (old ), len (new ))):
225225 diffs .extend (diff_values (old [i ], new [i ], f"{ path } [{ i } ]" ))
226226 # Report extra elements on the longer side
227227 if len (old ) > len (new ):
228228 for i in range (len (new ), len (old )):
229- diffs .append ({
230- "path" : f"{ path } [{ i } ]" ,
231- "type" : "removed" ,
232- "old" : old [i ],
233- })
229+ diffs .append (
230+ {
231+ "path" : f"{ path } [{ i } ]" ,
232+ "type" : "removed" ,
233+ "old" : old [i ],
234+ }
235+ )
234236 elif len (new ) > len (old ):
235237 for i in range (len (old ), len (new )):
236- diffs .append ({
237- "path" : f"{ path } [{ i } ]" ,
238- "type" : "added" ,
239- "new" : new [i ],
240- })
238+ diffs .append (
239+ {
240+ "path" : f"{ path } [{ i } ]" ,
241+ "type" : "added" ,
242+ "new" : new [i ],
243+ }
244+ )
241245 else :
242246 if old != new :
243247 diffs .append ({"path" : path , "type" : "changed" , "old" : old , "new" : new })
@@ -265,19 +269,33 @@ def format_diff(diffs: list[dict[str, Any]]) -> str:
265269 path = f"{ C .CYAN } { d ['path' ]} { C .RESET } "
266270 dtype = d ["type" ]
267271 if dtype == "added" :
268- lines .append (f" { path } : { C .GREEN } + { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } " )
272+ lines .append (
273+ f" { path } : { C .GREEN } + { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } "
274+ )
269275 elif dtype == "removed" :
270- lines .append (f" { path } : { C .RED } - { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } " )
276+ lines .append (
277+ f" { path } : { C .RED } - { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } "
278+ )
271279 elif dtype == "changed" :
272280 lines .append (f" { path } :" )
273- lines .append (f" { C .RED } old: { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } " )
274- lines .append (f" { C .GREEN } new: { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } " )
281+ lines .append (
282+ f" { C .RED } old: { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } "
283+ )
284+ lines .append (
285+ f" { C .GREEN } new: { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } "
286+ )
275287 elif dtype == "type_changed" :
276288 lines .append (f" { path } : type changed" )
277- lines .append (f" { C .RED } old ({ type (d ['old' ]).__name__ } ): { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } " )
278- lines .append (f" { C .GREEN } new ({ type (d ['new' ]).__name__ } ): { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } " )
289+ lines .append (
290+ f" { C .RED } old ({ type (d ['old' ]).__name__ } ): { json .dumps (d ['old' ], sort_keys = True )} { C .RESET } "
291+ )
292+ lines .append (
293+ f" { C .GREEN } new ({ type (d ['new' ]).__name__ } ): { json .dumps (d ['new' ], sort_keys = True )} { C .RESET } "
294+ )
279295 elif dtype == "list_length" :
280- lines .append (f" { path } : list length { C .RED } { d ['old' ]} { C .RESET } -> { C .GREEN } { d ['new' ]} { C .RESET } " )
296+ lines .append (
297+ f" { path } : list length { C .RED } { d ['old' ]} { C .RESET } -> { C .GREEN } { d ['new' ]} { C .RESET } "
298+ )
281299
282300 return "\n " .join (lines )
283301
@@ -305,16 +323,22 @@ def find_list_diffs(
305323 scalar_diffs : list [dict [str , Any ]] = []
306324
307325 if type (old ) is not type (new ):
308- scalar_diffs .append ({"path" : path , "type" : "type_changed" , "old" : old , "new" : new })
326+ scalar_diffs .append (
327+ {"path" : path , "type" : "type_changed" , "old" : old , "new" : new }
328+ )
309329 return list_diffs , scalar_diffs
310330
311331 if isinstance (old , dict ):
312332 old_keys = set (old .keys ())
313333 new_keys = set (new .keys ())
314334 for key in sorted (old_keys - new_keys ):
315- scalar_diffs .append ({"path" : f"{ path } .{ key } " , "type" : "removed" , "old" : old [key ]})
335+ scalar_diffs .append (
336+ {"path" : f"{ path } .{ key } " , "type" : "removed" , "old" : old [key ]}
337+ )
316338 for key in sorted (new_keys - old_keys ):
317- scalar_diffs .append ({"path" : f"{ path } .{ key } " , "type" : "added" , "new" : new [key ]})
339+ scalar_diffs .append (
340+ {"path" : f"{ path } .{ key } " , "type" : "added" , "new" : new [key ]}
341+ )
318342 for key in sorted (old_keys & new_keys ):
319343 ld , sd = find_list_diffs (old [key ], new [key ], f"{ path } .{ key } " )
320344 list_diffs .extend (ld )
@@ -324,7 +348,9 @@ def find_list_diffs(
324348 list_diffs .append ((path , old , new ))
325349 else :
326350 if old != new :
327- scalar_diffs .append ({"path" : path , "type" : "changed" , "old" : old , "new" : new })
351+ scalar_diffs .append (
352+ {"path" : path , "type" : "changed" , "old" : old , "new" : new }
353+ )
328354
329355 return list_diffs , scalar_diffs
330356
@@ -382,7 +408,7 @@ def _format_yaml_item(item: Any, indent: int = 0) -> list[str]:
382408 for i , k in enumerate (keys ):
383409 v = item [k ]
384410 leader = f"{ prefix } - " if i == 0 else f"{ prefix } "
385- if isinstance (v , (dict , list )):
411+ if isinstance (v , (dict , list )): # noqa: UP038
386412 lines .append (f"{ leader } { k } :" )
387413 for sub in _format_yaml_item (v , indent + 4 ):
388414 lines .append (sub )
@@ -450,9 +476,7 @@ def format_enhanced_diff(
450476
451477 if list_diffs :
452478 n = len (list_diffs )
453- lines .append (
454- f"{ C .BOLD } { C .YELLOW } Found { n } list(s) with differences:{ C .RESET } "
455- )
479+ lines .append (f"{ C .BOLD } { C .YELLOW } Found { n } list(s) with differences:{ C .RESET } " )
456480 lines .append ("" )
457481
458482 for path , old_list , new_list in sorted (list_diffs ):
0 commit comments