Skip to content

can not parse dict-comprehension with f-string in condition #4600

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

Closed
15r10nk opened this issue Mar 2, 2025 · 1 comment
Closed

can not parse dict-comprehension with f-string in condition #4600

15r10nk opened this issue Mar 2, 2025 · 1 comment

Comments

@15r10nk
Copy link

15r10nk commented Mar 2, 2025

Describe the bug

The following code can not be parsed/formatted by black:

{name_4: name_1 for name_2 in something if f'\\' if {}}
❯ .venv/bin/black -t py312 scripts/min_code.py
error: cannot format scripts/min_code.py: Cannot parse for target version Python 3.12: 1:53: {name_4: name_1 for name_2 in something if f'\\' if {}}

Oh no! 💥 💔 💥
1 file failed to reformat.

but it can be parsed by cpython.

❯ python3.12 scripts/min_code.py
Traceback (most recent call last):
  File "/home/frank/projects/black/scripts/min_code.py", line 1, in <module>
    {name_4: name_1 for name_2 in something if f'\\' if {}}
                                  ^^^^^^^^^
NameError: name 'something' is not defined

(this is not a parsing but an runtime error, which is ok)

Environment

  • Black's version: current main (00c0d6d)
  • OS and Python version: Linux/Python 3.12

Additional context

The bug was found by pysource-codegen (see #3908)

@MeGaGiGaGon
Copy link
Collaborator

I'm surprised that's valid python, that double if is certainly something. Looks to be the same issue as #4588, it parses if you make the f-string a normal string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants