Comments (14)
No no, thank YOU! We love your tool :) (Well, I love it. My team grumbles when they forget to run it and our linting job snarks at them.)
from black.
I got something that appears to work: #4332.
from black.
Thanks! cc @tusharsadhwani.
from black.
Using the same quotes is a syntax error in Python itself. You terminate the string early.
>>> f'{1:{f'{2}'}}'
File "<stdin>", line 1
f'{1:{f'{2}'}}'
^
SyntaxError: f-string: expecting '}'
from black.
@tarper24 it works fine on Python 3.12 onwards.
from black.
@tarper24 not in Python 3.12 any more. That's actually why we made this change; we had to revamp the parser around f-strings to support the new syntax. Unfortunately that caused us to start failing on some f-strings that were already valid. We found a few such cases before release by running Black on various codebases, but unfortunately we missed your case.
from black.
This seems like the minimal reproduction:
f"{1:{f'{2}'}}"
from black.
actually, using same or different quotes gives us two different crash scenarios:
f'{1:{f'{2}'}}'
If the quotes of the outer and inner fstring are the same, we get a different crash.
from black.
I spent some time on this but couldn't figure out a solution yet.
The reproducer gets tokenized like this:
% python -m blib2to3.pgen2.tokenize 4329.py
1,0-1,2: FSTRING_START 'f"'
1,2-1,2: FSTRING_MIDDLE ''
1,2-1,3: LBRACE '{'
1,3-1,4: NUMBER '1'
1,4-1,5: OP ':'
1,5-1,5: FSTRING_MIDDLE ''
1,5-1,6: OP '{'
1,6-1,8: FSTRING_START "f'"
1,8-1,8: FSTRING_MIDDLE ''
1,8-1,9: LBRACE '{'
1,9-1,10: NUMBER '2'
1,10-1,11: OP '}'
1,11-1,12: FSTRING_MIDDLE "'"
1,12-1,13: RBRACE '}'
Traceback (most recent call last):
The FSTRING_MIDDLE "'"
near the end is wrong; it should be an FSTRING_END
, closing the inner f-string.
My current thinking is that the issue is that the inside_fstring_colon
in the tokenizer gets set to True for the outer f-string and then applied incorrectly while we're parsing the inner f-string. To address that, I tried turning inside_fstring_colon
into a stack with an entry for each nested f-string, but that so far doesn't work.
from black.
Commenting out and bracelev == 0
in the part that yields RBRACE fixes this case. But it breaks other cases. That's how far I got yesterday night
from black.
Also it's not the FSTRING_MIDDLE that's incorrect, it's the OP just above it, which should be an RBRACE to match the LBRACE.
from black.
The minimised case that breaks when making the bracelev change is:
f'{1:{2}d}'
from black.
What is the difference between OP and LBRACE/RBRACE here? I noticed the variation but it wasn't clear to me which one is correct.
from black.
In the original impl it's very blurry what to use, but I went with yielding LBRACE whenever we go from collecting FSTRING_MIDDLE tokens to parsing python expressions again
from black.
Related Issues (20)
- Cannot format f-strings where the contents re-use the same quotes as the enclosing f-string HOT 1
- 24.4.2: pytest fails in tests/test_format.py with `AssertionError` HOT 3
- Cannot parse multiline f-string containing multiline string HOT 1
- Is there any way to have a permalink for a specific version in the changelog doc? HOT 5
- Unnecessary parentheses added to expression in indexed assignment
- "EOF in multi-line string" on string containing same quote nested in f-string HOT 2
- Failure to format when double-quotes are in an f-string's code block HOT 8
- Long lines that concatenate list comprehensions HOT 3
- Black Formatter not working for this code. HOT 2
- Python 3.12 `def f[T: Generics](...)` HOT 2
- Strings not being formatted by Black HOT 3
- Black fails to format string which has '='s etc HOT 2
- Black v24.4.1 & v24.4.2 fails to format f-strings containing multi-line strings HOT 1
- Advice (UFMT) command-failed Failed due to ASTSafetyError HOT 2
- fmt: skip is required at the line which follows the one incorrectly formatted
- Error in github actions when using `use_pyproject: true` HOT 2
- Error: Invalid value for 'SRC ...': Path '...' does not exist. HOT 2
- Technical doc for smoother contribution
- import asyncio HOT 4
- no info about setting by default HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from black.