diff --git a/test/lint/lint-python-format-tests.txt b/test/lint/lint-python-format-tests.txt --- a/test/lint/lint-python-format-tests.txt +++ b/test/lint/lint-python-format-tests.txt @@ -68,3 +68,13 @@ "test % \ 1" a = 10 % 2 + +# An array +"test %s" % an_array[0] +# A matrix +"test %s" % an_array[0][0] +# An array in an array +["test %s" % an_array[0]] +# An matrix in an array in a dict +{"test":" ["test %s" % an_array[0][0]]} + diff --git a/test/lint/lint-python-format.py b/test/lint/lint-python-format.py --- a/test/lint/lint-python-format.py +++ b/test/lint/lint-python-format.py @@ -120,9 +120,18 @@ # Where to close the parenthesis if there is a single specifier ? # It is whether at the end or before the first ',', ']', '}' (if # enclosed in a function call, a list or a dictionary). + # + # There is a special case to be handled when the qualifier is an array. + # In this case, ensure there is one more ']' than '['. close_before = [",", "]", "}"] + opening_count = 0 for i, c in enumerate(qualifier): + if c == "[": + opening_count += 1 if c in close_before: + if(c == "]" and opening_count > 0): + opening_count -= 1 + continue return qualifier[:i] + ")" + qualifier[i:] return qualifier + ")" @@ -249,6 +258,14 @@ "%d %-10s %%" % (len("string2"), "string2"))] => ["test {:05d} % {}".format(len("string1"), "{} {:10s} %".format(len("string2"), "string2"))] + (73) "test %s" % an_array[0] + => "test {}".format(an_array[0]) + (75) "test %s" % an_array[0][0] + => "test {}".format(an_array[0][0]) + (77) ["test %s" % an_array[0]] + => ["test {}".format(an_array[0])] + (79) {"test":" ["test %s" % an_array[0][0]]} + => {"test":" ["test {}".format(an_array[0][0])]} """ errors = find_errors(file) # Python dictionnaries do not guarantee ordering, sort by line number