question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't handle indexed format strings

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
merwokcommented, Apr 24, 2019

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.

>>> info = {"name": "Alice", "age": 36}
>>> "{0[name]} is {0[age]}".format(info)
'Alice is 36'
>>> f"{info[name]} is {info[age]}"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> f"{info['name']} is {info['age']}"
'Alice is 36'
2reactions
asottilecommented, Apr 24, 2019

indeed, a bit of an oversight. oddly enough I thought far enough ahead to support dotted names but not brackets

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found