-
Notifications
You must be signed in to change notification settings - Fork 748
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
False positive E202 and E251 inside f-string (Python 3.12) #1201
Comments
I believe the intended syntax for this is |
Hi! In the proposal python/cpython#80998, where discussed syntax was suggested, you can find example of the f-string with spaces around the = symbol. In the section https://docs.python.org/3/reference/lexical_analysis.html#f-strings you can also find example of the f-string with more spaces than in my example:
One of the new features In the Python 3.12 is: "More flexible f-string parsing , allowing many things previously disallowed (PEP 701)." In the PEP701 documentation the special section "Handling of f-string debug expressions" exists. The fragment of this section is below:
As I understand, it means that:
is equivalent to
and only expression itself without surrounding spaces need to be validated against PEP8. Additionally, in the PEP701 documentation you can find examples of "usual" f-strings with spaces after { and before }. Your script generates E201 and E202 for such strings, while real warnings should look like "spaces after { and before } will disappear at the output". And, of course, E251 message is confusing, because meaning of = character is different here. Best regards, |
yes but all of the canonical examples do not have spaces |
By the way, I found one case (agree, very special) where the space after the { is required. Consider the valid Python 3.12 code below, which uses "usual" f-string: a = 999
print(f'{ {a, 2, 3}}')
print(f'{{a, 2, 3}}') which prints:
Do you agree, that it is not PEP8 error/warning? |
use parentheses |
Result of print(f'{({a, 2, 3})=}') looks ugly:
|
it looks ugly either way. nobody would write code like that |
Ok. The last question. Will you accept pull request? P.S. Yes, I understand, that patch will not be trivial, because in the function extraneous_whitespace tokens are unavailable. |
no. I consider the current behavior to be correct. you can use noqa for your strawman cases |
The spaces around foo = 42
print(f"{foo=}")
# foo=42
print(f"{foo =}")
# foo =42
print(f"{foo= }")
# foo= 42
print(f"(foo = }")
# foo = 42 What will be the effective way to produce Generally E251 errors won't affect the behavior of the code, but this one forces to change the behavior. |
Hi!
For the code below:
the pycodestyle.py version 2.11.0 generates 3 false positive errors:
P.S. Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Thank you in advance.
Andrew
The text was updated successfully, but these errors were encountered: