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

Paul Moore p.f.moore at gmail.com
Fri Aug 19 04:16:13 EDT 2016


On 19 August 2016 at 06:07, Terry Reedy <tjreedy at udel.edu> wrote:
> It seems to me that that this is at least somewhat a strawman issue.
>
> If you want to prohibit backslashed quote reuse in expressions, as in
> f'{x.partition(\'-\')[0]}', that is okay with me, as this is unnecessary*
> and arguably bad.  The third alternative above is better. What breaks
> colorizers, and what I therefore object to, is the innovation of adding
> magical escaping of ' or " without \.
>
> Or add a new style rule to PEP 8.
>
> F-strings: avoid unnecessary escaping in the expression part of f-strings.
> Good: f"{x.partition('-')[0]}"
> Bad: f'{x.partition(\'-\')[0]}'
>
> Then PEP-8 checkers will flag such usage.

+1.

While substantial IDEs like PyCharm or PTVS may use a full-scale
parser to do syntax highlighting, I suspect that many tools just use
relatively basic regex parsing (Vim certainly does). For those tools,
unescaped nested quotes will likely be extremely difficult, if not
impossible, to parse correctly. Whereas the current behaviour is
"just" standard string highlighting.

So if the Python parser were to change as proposed, I'd still argue
strongly for a coding style that never uses any construct that would
be interpreted differently from current behaviour (i.e., the changed
behaviour should essentially be irrelevant). Given this, I thing the
argument to change, whether it's theoretically an improvement or not,
is irrelevant, and practicality says there's no point in bothering.

(Python's parser is intentionally simple, to make it easy for humans
and tools to parse Python code - I'm not sure the proposed change to
syntax meets that guideline for simple syntax).

Paul


More information about the Python-ideas mailing list