[issue45086] f-string unmatched ']'

2021-09-04 Thread Greg Kuhn


Greg Kuhn  added the comment:

I see, thank you all for the detailed investigation and explanation!!

Agreed Terry, anyone who reads the error should be able to parse it themselves 
and see what the errors is. Pointing the user to the error site is the most 
important piece.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Thank you Eric.  I can see now that the actual process is a somewhat 
complicated mix of the simple options 1 and 2 I imagined above.  It is like 
option 2, except that everything between '{' and '}' is partially parsed enough 
to create a format object.  This is required to ignore quoted braces

>>> f'{"}"'
SyntaxError: f-string: expecting '}'

and detect valid '!' and ':' markers.  In that partial parsing, unmatched 
fences are detected and reported, while other syntax errors are not.  If my 
option 1 above were true, the first example below would instead report the 'a 
a' error.

>>> f'{a a'
SyntaxError: f-string: expecting '}'
>>> f'{a a]'
SyntaxError: f-string: unmatched ']'
>>> f'{a a}'
SyntaxError: f-string: invalid syntax. Perhaps you forgot a comma?

The second plausibly could, but outside of the f-string context, the error is 
the same.
>>> a a]
SyntaxError: unmatched ']'
 
Greg, the fuller answer to your question is that the interpreter is only 
*required* to report that there is an error and some indication of where.  
"SyntaxError: invalid syntax" is the default.  It may have once been all that 
was ever reported.

A lot of recent effort has gone into adding detail into what is wrong and what 
the fix might be.  But both additions sometimes involve choices that may not 
meet a particular person's expectation.  Another person, expecting linear 
rather than nested parsing, might look at the first example above and ask 
whether the interpreter should be complaining about the 'a a' syntax error 
instead of the following lack of '}' f-string error.  And I would not call it a 
bug if it were to do so in the future.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-04 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

> But we've never gotten past just talking about it

Stay tuned! :)

 https://github.com/we-like-parsers/cpython/tree/fstring-grammar

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-03 Thread Eric V. Smith


Eric V. Smith  added the comment:

I don't think it really makes a difference, but here's some background:

For f-strings, the parser itself does not break apart the f-string into 
(, ) parts. There's a custom parser (at 
https://github.com/python/cpython/blob/0b58e863df9970b290a4de90c67f9ac30c443817/Parser/string_parser.c#L837)
 which does that. Then the normal parser is used to parse the expression 
portion.

I think the error shown here is not in the expression parser, but in the 
fstring parser in fstring_find_expr(), at 
https://github.com/python/cpython/blob/0b58e863df9970b290a4de90c67f9ac30c443817/Parser/string_parser.c#L665

As Terry says, it's not incorrect to print the error show in this bug report.

To further diverge:

There's been talk about using the normal parser to pull apart the entire 
f-string, instead of using the two-pass version I mention above. But we've 
never gotten past just talking about it. There are pros and cons for doing it 
with the normal parser, but that's a discussion for a different forum.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-03 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

The behavior remains the same in 3.11 ('main' branch).  New PEG parser parses 
this the same.

(Pablo, if there is a mistake below, please correct.)

Normally, the parser copies code chars between quotes, with or without '\' 
interpretation, into a string object.  When the 'f' prefix is given, the string 
gets divided into substrings and replacement fields.  The code part of each 
replacement field is separately parsed.  There are two options: 1. parse the 
field code while looking for an endcode marker; 2. look ahead for an endcode 
marker and then parse the code.

The current behavior is consistent with opotion 1 and the python policy of 
reporting the first error found and exiting, rather than trying to 
resynchronize to try to find more errors.

--
nosy: +pablogsal, terry.reedy
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed
versions: +Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-02 Thread Eric V. Smith


Eric V. Smith  added the comment:

I think it's basically this error:

>>> num]
  File "", line 1
num]
   ^
SyntaxError: unmatched ']'

Although I'd have to look at it more to see why that's the error it chose to 
display, instead of the curly brace.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-02 Thread Greg Kuhn


Greg Kuhn  added the comment:

But doesn't the square bracket have no relevance here?
It's not within a curly bracketed string so shouldn't be treated specially.

I would have expected the error to be: SyntaxError: f-string: unmatched '}'.

Unless I need to go back and reread pep498...

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-02 Thread Eric V. Smith


Eric V. Smith  added the comment:

I think the error is short for "I found a ']' without a matching '['".

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45086] f-string unmatched ']'

2021-09-02 Thread Greg Kuhn


New submission from Greg Kuhn :

Hi All, 
Is the below a bug? Shouldn't the interpreter be complaining about a curly 
brace?

$ python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> num = 10
>>> f'[{num]'
  File "", line 1
SyntaxError: f-string: unmatched ']'
>>>

--
messages: 400920
nosy: Greg Kuhn
priority: normal
severity: normal
status: open
title: f-string unmatched ']'
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com