Repository: asottile/pyupgrade

Issue:
Doesn't handle indexed format strings
The following code which I tried to reduce (which I had no idea was possible):
```python
thing = {'a': 1, 'b': 2}
print('{0[a]} and {0[b]}'.format(thing))
```
leads to 
```
Traceback (most recent call last):
  File "/home/ethan/venv/bin/pyupgrade", line 10, in <module>
    sys.exit(main())
  File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1396, in main
    ret |= fix_file(filename, args)
  File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1372, in fix_file
    contents_text = _fix_fstrings(contents_text)
  File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1348, in _fix_fstrings
    tokens[i] = token._replace(src=_to_fstring(token.src, node))
  File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1310, in _to_fstring
    name = ''.join((params[k or str(i)], dot, rest))
KeyError: '0[a]'
```

This is a very esoteric format string usage, but 🤷‍♂️

Discussion:

Patch:
diff --git a/pyupgrade.py b/pyupgrade.py
index 6f2669b..bf04191 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1279,6 +1279,9 @@ class FindSimpleFormats(ast.NodeVisitor):
                     # timid: could make the f-string longer
                     if candidate and candidate in seen:
                         break
+                    # timid: bracketed
+                    elif '[' in candidate:
+                        break
                     seen.add(candidate)
             else:
                 self.found[_ast_to_offset(node)] = node


Test patch:
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index b6988d7..dbf747a 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -1366,6 +1366,7 @@ def test_fix_classes_py3only(s, expected):
         'b"{} {}".format(a, b)',
         # for now, too difficult to rewrite correctly
         '"{:{}}".format(x, y)',
+        '"{a[b]}".format(a=a)',
     ),
 )
 def test_fix_fstrings_noop(s):