Doesn't handle indexed format strings
See original GitHub issueThe following code which I tried to reduce (which I had no idea was possible):
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 🤷♂️
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
python string format suppress/silent keyerror/indexerror
Is there a way to use python string.format such that no exception is thrown when an index is missing, instead an empty string...
Read more >String.Format Method (System) - Microsoft Learn
Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to...
Read more >How To Use String Formatters in Python 3 - DigitalOcean
format () method are concatenated into the string in order. The string values contained in the tuple correspond to the following index numbers:...
Read more >Format string attack - OWASP Foundation
Description. The Format String exploit occurs when the submitted data of an input string is evaluated as a command by the application.
Read more >How to Handle String Index Out Of Bounds Exception in Java
The StringOutOfBoundsException in Java that occurs when accessing a string at an index which is negative/greater than the string length.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just a note that may be useful:
format
uses a mini-language where keys for item access are not quoted, but f-strings contain real, arbitrary expressions and require quotes.indeed, a bit of an oversight. oddly enough I thought far enough ahead to support dotted names but not brackets