diff --git a/Lib/string.py b/Lib/string.py index 489777b10c25df7..6bad050eef5ca34 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -222,8 +222,13 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, def get_value(self, key, args, kwargs): if isinstance(key, int): + # "1234[x]" return args[key] + elif key == "": + # "[x]" + return args[0] else: + # "abcd[x]" return kwargs[key] diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index 0be28fdb609eaee..bca5d165ab83a74 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -96,6 +96,7 @@ def test_index_lookup(self): fmt = string.Formatter() lookup = ["eggs", "and", "spam"] self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') + self.assertEqual(fmt.format("{[2]}{1[0]}", lookup, (1, 2, 3)), 'spam1') with self.assertRaises(IndexError): fmt.format("{0[2]}{0[0]}", []) with self.assertRaises(KeyError): diff --git a/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst b/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst new file mode 100644 index 000000000000000..951e76acceaaef8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-19-00-20-28.bpo-39985.4ki_Ed.rst @@ -0,0 +1 @@ +Make string.Formatter behave like str.format with anonymous subscripts. \ No newline at end of file