Skip to content

Incorrect highlighting when using new-style string formatting and a % sign. #7

Closed

Description

@fpierfed

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:
screen shot 2015-10-20 at 12 20 25

In this case, of course, the % sign is not part of the formatting, just like the space and the following o.

Activity

  1. 1st1 commented on Oct 20, 2015

    @1st1
    Member

    This is because "% s" % 123 and %o are valid in Python -- so we highlight both new and old formatting...

  2. fpierfed commented on Oct 20, 2015

    @fpierfed
    Author

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

  3. vpetrovykh commented on Oct 20, 2015

    @vpetrovykh
    Contributor

    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
    
  4. 1st1 commented on Oct 20, 2015

    @1st1
    Member

    (see also 11342d4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions