[Python-ideas] Let’s make escaping in f-literals impossible

Random832 random832 at fastmail.com
Sat Aug 20 00:02:52 EDT 2016


On Fri, Aug 19, 2016, at 19:09, Paul Moore wrote:
> So, to me
>
>  f'{x.partition(' + ')[0]}'
>
> reads as a string concatenation. I'm not sure how you'd expect a
> syntax highlighter to make it look like anything else, to be honest

One possible syntax highlighting scheme:

- f' and ' are hilighted in pink, along with any plain text content of
  the string.
- - Incidentally, any backslash escapes, not shown here, are highlighted
  in orange.
- { and } are hilighted in blue; along with format specifiers, maybe, or
  maybe they get another color.
- The code inside the expression is highlighted in orange.
- Any keywords, builtins, constants, etc, within the expression are
  highlighted in their usual colors.
- - In this example in particular, ' + ' and 0 are highlighted in pink.

A pink + is a character within a string, a gray or orange + is an
operator.

In terms of Vim's basic syntax groups:
- C = Constant [pink]
- P = PreProc [blue], by precedent as use for the $(...)
  delimiters in sh)
- S = Special [orange], by precedent as use for the content within
  $(...) in sh, and longstanding near-universal precedent, including in
  python, for backslash escapes. These would, naturally, have separate
  non-basic highlight groups, in case a particular user wanted to change
  one of them.

f'foo {x.partition(' + ')[0]:aaa} bar\n'
CCCCCCPSSSSSSSSSSSSCCCCCSSCSPPPPPCCCCSSC

> (given that you're arguing *not* to highlight the whole of the content
> of the f-string the same way).

I'm not sure what you mean by "the same way" here, I haven't followed
the discussion closely enough to know what statement by whom you're
referring to here.


More information about the Python-ideas mailing list