New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Left bracket remains in format string result when '\' preceeds it #73290
Comments
In short:
yields:
This is reproducible only when
results in: "\\ 10" |
I'm not sure this counts as an error. The backslash means to treat the next character literally, which this does. And since \{ is not a valid escape sequence, it keeps both characters, exactly like: >>> '\c'
'\\c' Furthermore, since unknown escape sequences are deprecated, this usage is going to be an error in the future. You can see this now with -Werror: $ python.exe -Werror
Python 3.6.0b1+ (3.6:87de1f12c41c+, Sep 16 2016, 07:05:57) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f'\{10}'
DeprecationWarning: invalid escape sequence '\{'
>>> '\c'
DeprecationWarning: invalid escape sequence '\c'
>>> Since this works as I expect it to now, and since it will become an error in the future, I don't think any change is warranted. |
But I admit that dropping the trailing } seems odd. I think this particular usage should be an error: no bare } allowed, similar to: >>> f'10}'
File "<stdin>", line 1
SyntaxError: f-string: single '}' is not allowed |
I see, the original "complaint" about this behavior on stack overflow was made due to the discrepancy between the f-string the the format "equivalent": '\{}'.format(10)'. |
Do you have a link to the SO question? |
Yes, should've attached in my previous message. See http://stackoverflow.com/questions/41330097/why-does-the-symbol-remain-when-f-10-is-evaluated-in-python-3-6 |
The problem is not that the trailing } is dropped, but that the starting { starts an f-string expression. >>> f'\{2*5}'
'\\{10' I expected either '\\10' as in '\{}'.format(2*5), or at least '\\{2*5}'. There is other f-string parsing error: >>> f'\\N{2*5}'
'\\N{2*5}' I expected '\\N10'. '\\N' doesn't start a unicode name escape. This is a legitimate expression. |
Yes: >>> f'\{2*5}'
'\\{10' I agree '\\10' would make sense. |
This problem was no doubt introduced after 3.6a1 when I changed the parsing to disallow backslashes inside {}. |
Proposed patch fixes parsing backslashes in f-strings. P.S. Definitely we should find other name for this term. I hate misleading "f-string". |
The PEP-498 should be updated to define the expected behaviour of f'\{5}': + self.assertEqual(f'\{6*7}', '\\42') I'm not sure that I like this behaviour. f'\\N{AMPERSAND}': reading a local variable looks like a typo or a security vulnerability, rather than a nice feature. IMHO if you want an anti-slash (\) in the output string, it *must* be written "\\" since we *do* interpret other escape sequences:
What is the issue with having to write "\\{10}" to get "\\10" string? It's less error prone. |
This can look as a typo, but how would you write this if this is not a typo? f'\\N{AMPERSAND}' is legitimate syntax, and it would be weird to interpret this expression in any other way than as '\\' + 'N' + format(AMPERSAND).
There is no issue with f'\\{10}'. There is an issue with f'\{10}'. |
Hum, I'm not sure that my previous comment is explicit: I suggest to raise a syntax error on f'\{10}' (or emit a warning, and raise an error in 3.7 if you insist ;-)). |
Updated patch adds a warning. |
Ping. |
Serhiy, since review activity has dropped on b.p.o now that the move to github was made, would you like to make this into a pull request to get it reviewed and merged faster? |
I am not experienced with git and am waiting until new workflow be described in the devguide. |
Serhiy: I plan to find time in the next week or so to look at this. Sorry for the delay. Plus, the new workflow isn't helping me out: I need to get up to speed on it. |
Serhiy: if you want, you can give me your email and I'll submit a PR for you. If you want to do it yourself, just: Download hub: https://hub.github.com/ git clone python/cpython cpython-git |
A PR has been submitted, Ryan. See #490 |
Thank you Ryan. I already created a PR, but I think your recipe can be helpful for me in future. |
Ping again. |
Eric? |
This is on my list of things to review at PyCon, when I should finally have some free time. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: