You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect highlighting when using new-style string formatting and a % sign. #7
Hi, I have just installed MagicPython in Sublime Text 3 and already love it. I have noticed a small inconsistency in the highlighting of strings like these
print('This happens {:.02f}% of the times'.format(percent))
See the result below:
In this case, of course, the % sign is not part of the formatting, just like the space and the following o.
I agree that it is a tricky situation. In the example above, however, it seems to me that the correct behavior would have been to highlight {:.02f} only and leave % o alone. Discriminating this in code would require looking at the whole line and seeing that we have a call to .format without a % operator...
Looking at the whole line is not a viable strategy. In principle, you're never guaranteed that .format will even be on the same line, so it requires an infinite lookahead, which cannot be implemented. Having inconsistent highlighting based on the presence of .format on the same line is also undesirable (not to mention tricky due to quoting, escaping and comments potentially having .format in them). There is also a fair amount of code where .format does not appear on the same line as the string at all (example taken from here):
msg = '{arg!r} parameter is positional only, ' \
'but was passed as a keyword'
msg = msg.format(arg=param.name)
raise TypeError(msg) from None
Since we cannot reliably tell which formatting is actually going to be used, we highlight both kinds to help spot issues like this:
>>> '% of the time "{token}" has been seen: % 10d' % 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
Activity
1st1 commentedon Oct 20, 2015
This is because
"% s" % 123and%oare valid in Python -- so we highlight both new and old formatting...fpierfed commentedon Oct 20, 2015
I agree that it is a tricky situation. In the example above, however, it seems to me that the correct behavior would have been to highlight
{:.02f}only and leave% oalone. Discriminating this in code would require looking at the whole line and seeing that we have a call to .format without a % operator...vpetrovykh commentedon Oct 20, 2015
Looking at the whole line is not a viable strategy. In principle, you're never guaranteed that
.formatwill even be on the same line, so it requires an infinite lookahead, which cannot be implemented. Having inconsistent highlighting based on the presence of.formaton the same line is also undesirable (not to mention tricky due to quoting, escaping and comments potentially having.formatin them). There is also a fair amount of code where.formatdoes not appear on the same line as the string at all (example taken from here):Since we cannot reliably tell which formatting is actually going to be used, we highlight both kinds to help spot issues like this:
1st1 commentedon Oct 20, 2015
(see also 11342d4)