Mailing List Archive

1 2  View All
Re: f-strings in the grammar [ In reply to ]
On Tue, Sep 21, 2021 at 11:49 AM Eric V. Smith <eric@trueblade.com> wrote:

> [Pablo]
>
> * The parser will allow nesting quote characters. This means that we
> **could** allow reusing the same quote type in nested expressions
> like this:
>
> f"some text { my_dict["string1"] } more text"
>
> I'm okay with this, with the caveat that I raised in another email: the
> effect on non-Python tools and alternate Python implementations. To restate
> that here: as long as we survey some (most?) of the affected parties and
> they're okay with it (or at least it doesn't cause them a gigantic amount
> of work), then I'm okay with it. This will of course be subjective. My big
> concern is tools that today use regex's (or similar) to recognize
> f-strings, and then completely ignore what's inside them. They just want to
> "skip over" f-strings in the source code, maybe because they're doing some
> sort of source-to-source transpiling, and they're just going to output the
> f-strings as-is. It seems to me we're creating a lot of work for such
> tools. Are there a lot of such tools? I don't know: maybe there are none.
>

I assume this is primarily an issue for syntax highlighters, which must
work under adverse conditions: the code may contain syntax errors nearby
and they must update fast when the user is typing. (I recall these were the
challenges when I implemented the first syntax coloring for IDLE several
decades ago.)

If the syntax highlighter shows the wrong colors in an edge case, users can
usually live with that. Something that just colors the entire f-string,
including the interpolations, with the "string" color is not optimal
anyways; the editor I currently use, VS Code, knows about f-strings and
colorizes (and otherwise analyzes) the interpolations as expressions.

I imagine if you have a simple-minded highlighter that just uses a regex
that matches string quotes, it will take something like my example and
color it "string" until the first nested quote, then be confused for a bit,
and then start coloring "string" after the second
nested quote, until the end of the f-string. So the confusion is local.

I created a gist with my example. This uses some well-known colorizer
written in JavaScript (I presume). It seems to actually already support
nested quotes?!
https://gist.github.com/gvanrossum/b8ca09175a0d1399a8999f13c7bfa616

And here's a copy-paste from VS Code (it also shows a red underline under
the entire f-string, but the copy doesn't show it):

def generate(source):
print("# What comes before")
print(f"{source.removesuffix(".py")}.c: $(srcdir)/{source}")
print("\t$(COMMAND)")


So these two tools, at least, seem to be doing all right (maybe because
they both come from the JavaScript culture, where nested interpolations are
well-known).

--
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
Re: f-strings in the grammar [ In reply to ]
On 9/21/2021 3:29 PM, Guido van Rossum wrote:
> On Tue, Sep 21, 2021 at 11:49 AM Eric V. Smith <eric@trueblade.com
> <mailto:eric@trueblade.com>> wrote:
>
> [Pablo]
>> * The parser will allow nesting quote characters. This means that
>> we **could** allow reusing the same quote type in nested expressions
>> like this:
>>
>> f"some text { my_dict["string1"] } more text"
> I'm okay with this, with the caveat that I raised in another email:
> the effect on non-Python tools and alternate Python implementations.
> To restate that here: as long as we survey some (most?) of the
> affected parties and they're okay with it (or at least it doesn't
> cause them a gigantic amount of work), then I'm okay with it. This
> will of course be subjective. My big concern is tools that today use
> regex's (or similar) to recognize f-strings, and then completely
> ignore what's inside them. They just want to "skip over" f-strings
> in the source code, maybe because they're doing some sort of
> source-to-source transpiling, and they're just going to output the
> f-strings as-is. It seems to me we're creating a lot of work for
> such tools. Are there a lot of such tools? I don't know: maybe there
> are none.
>
>
> I assume this is primarily an issue for syntax highlighters, which must
> work under adverse conditions: the code may contain syntax errors nearby
> and they must update fast when the user is typing. (I recall these were
> the challenges when I implemented the first syntax coloring for IDLE
> several decades ago.)

If same-quote nesting were limited to 1 deep, REs could handle it.
Since nesting is not, and same-quote nesting would not be, they cannot
in general.

f'''length is {len(3*f"{f'{a}'}")}'''
'length is 3'

Still, if this arrives, I would consider a patch to handle the first
nesting level.

> If the syntax highlighter shows the wrong colors in an edge case, users
> can usually live with that.

Since IDLE is a gift, not a product, I've decided a feature falling
short of perfection is OK.

> Something that just colors the entire
> f-string, including the interpolations, with the "string" color is not
> optimal anyways;

To me, there is a tradeoff. Thunderbird bird displays the gmane version
of the example below highlighted. I find the broken chunks to jarring.

> the editor I currently use, VS Code, knows about
> f-strings and colorizes (and otherwise analyzes) the interpolations as
> expressions.

The red underline on the original display is a nice touch. It would
definitely help to tie the whole string together. Assuming VS Code
handles the double nesting, does it give two underlines for the example
above, for the outer and middle strings?
> I imagine if you have a simple-minded highlighter that just uses a regex
> that matches string quotes, it will take something like my example and
> color it "string" until the first nested quote, then be confused for a
> bit, and then start coloring "string" after the second
> nested quote, until the end of the f-string. So the confusion is local.

This is what IDLE does now.

> I created a gist with my example. This uses some well-known colorizer
> written in JavaScript (I presume). It seems to actually already support
> nested quotes?!
> https://gist.github.com/gvanrossum/b8ca09175a0d1399a8999f13c7bfa616
> <https://gist.github.com/gvanrossum/b8ca09175a0d1399a8999f13c7bfa616>
>
> And here's a copy-paste from VS Code (it also shows a red underline
> under the entire f-string, but the copy doesn't show it):
>
> def generate(source):
>     print("# What comes before")
>     print(f"{source.removesuffix(".py")}.c: $(srcdir)/{source}")
>     print("\t$(COMMAND)")
>
>
> So these two tools, at least, seem to be doing all right (maybe because
> they both come from the JavaScript culture, where nested interpolations
> are well-known).

With only 1 or even 2 types of quotes, reusing them would be more
necessary than it is in Python.


--
Terry Jan Reedy


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/SUR5PQY7MBR4S6EYKLALMXFKINPZSJLS/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: f-strings in the grammar [ In reply to ]
On Sep 21, 2021, at 15:03, Terry Reedy <tjreedy@udel.edu> wrote:
>
> If same-quote nesting were limited to 1 deep, REs could handle it. Since nesting is not, and same-quote nesting would not be, they cannot in general.
>
> f'''length is {len(3*f"{f'{a}'}")}'''

I tried this in the latest python-mode.el for Emacs, and while it isn’t able to handle it correctly, at least the damage is local. That’s been one of the problems with Emacs syntax highlighting: it usually works quite well these days but when it gets messed up, the incorrect highlighting can extend deep into the file.

speaking-for-all-3-of-the-remaining-emacs-users-ly y’rs,
-Barry
Re: f-strings in the grammar [ In reply to ]
On 9/21/2021 7:42 PM, Eric V. Smith wrote:
> I don't recall exactly why, but I disallowed backslashes inside
> expressions at the last minute before 3.6 was released. It might have
> been because I was interpreting them in a way that didn't make sense if
> a "real" parser were inspecting f-strings. The idea, even back then, was
> to re-allow them when/if we moved f-string parsing into the parser
> itself. I think it's time.

Yeah, we were still trying to figure out whether escapes like "\\n"
would be evaluated as "\\n" or "\n" in the expression, and decided to
decide later. If we can clearly articulate which it is now, then let's
go ahead and enable it.

>> * The parser will allow nesting quote characters. This means that we
>> **could** allow reusing the same quote type in nested expressions
>> like this:
>>
>> f"some text { my_dict["string1"] } more text"
> I'm okay with this, with the caveat that I raised in another email: the
> effect on non-Python tools and alternate Python implementations.

As a fairly regular user, I would be very happy to not have to worry
about mixing quotes. It's also not going to break any existing code, so
safe enough to enable if we can.

Agreed with Eric on the rest.

Cheers,
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ZQDQRUI4JZOQT7IW2MU2IGJCGHWZH7Q4/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: f-strings in the grammar [ In reply to ]
On Tue, Sep 21, 2021 at 4:08 PM Steve Dower <steve.dower@python.org> wrote:

> On 9/21/2021 7:42 PM, Eric V. Smith wrote:
> > I don't recall exactly why, but I disallowed backslashes inside
> > expressions at the last minute before 3.6 was released. It might have
> > been because I was interpreting them in a way that didn't make sense if
> > a "real" parser were inspecting f-strings. The idea, even back then, was
> > to re-allow them when/if we moved f-string parsing into the parser
> > itself. I think it's time.
>
> Yeah, we were still trying to figure out whether escapes like "\\n"
> would be evaluated as "\\n" or "\n" in the expression, and decided to
> decide later. If we can clearly articulate which it is now, then let's
> go ahead and enable it.
>

That would seem easy enough, right?

f"spam {'xyz'.replace('y', '\\n')} spam"

should be equal to

"spam x\\ny spam"

and print as

spam x\ny spam

(i.e. a literal backslash followed by 'n', not a newline).

You shouldn't have to double the \ in the interpolated expression just
because it's in an f-string.

I presume it was trickier at the time because we were coming from
"{xxx}".format(...), where the parser doesn't know that the string is a
format string.

--
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
Re: f-strings in the grammar [ In reply to ]
On 9/21/2021 7:15 PM, Guido van Rossum wrote:
> On Tue, Sep 21, 2021 at 4:08 PM Steve Dower <steve.dower@python.org
> <mailto:steve.dower@python.org>> wrote:
>
> On 9/21/2021 7:42 PM, Eric V. Smith wrote:
> > I don't recall exactly why, but I disallowed backslashes inside
> > expressions at the last minute before 3.6 was released. It might
> have
> > been because I was interpreting them in a way that didn't make
> sense if
> > a "real" parser were inspecting f-strings. The idea, even back
> then, was
> > to re-allow them when/if we moved f-string parsing into the parser
> > itself. I think it's time.
>
> Yeah, we were still trying to figure out whether escapes like "\\n"
> would be evaluated as "\\n" or "\n" in the expression, and decided to
> decide later. If we can clearly articulate which it is now, then
> let's
> go ahead and enable it.
>
>
> That would seem easy enough, right?
>
> f"spam {'xyz'.replace('y', '\\n')} spam"
>
> should be equal to
>
> "spam x\\ny spam"
>
> and print as
>
> spam x\ny spam
>
> (i.e. a literal backslash followed by 'n', not a newline).
>
Yes, I think that's the desired behavior. Before I removed this in 3.6
(during the betas, IIRC), it would have produced an actual newline,
because of where the f-string 'parser' was able to insert itself into
the process. I/we didn't like that behavior, and it was too late to
change it.

We could add this now in the bespoke f-string parser, although I don't
know off the top of my head how much work it would be. But if we're
going to switch to Pablo's parser then I don't think there's any point.

> You shouldn't have to double the \ in the interpolated expression just
> because it's in an f-string.
Right.
> I presume it was trickier at the time because we were coming from
> "{xxx}".format(...), where the parser doesn't know that the string is
> a format string.

Yes, that was part of it.

Eric
Re: f-strings in the grammar [ In reply to ]
On Mon, 20 Sep 2021, 9:19 pm Pablo Galindo Salgado, <pablogsal@gmail.com>
wrote:

> Hi,
>
> I have started a project to move the parsing off-strings to the parser and
> the grammar. Appart
> from some maintenance improvements (we can drop a considerable amount of
> hand-written code),
> there are some interesting things we **could** (emphasis on could) get out
> of this and I wanted
> to discuss what people think about them.
>

The change seems like a good idea, but the consequences should be
summarised in a PEP (either the existing
https://www.python.org/dev/peps/pep-0536/ or a replacement for it).

Cheers,
Nick.

>
>

1 2  View All