Skip to content
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

Parsing the with statement with tuple argument failed #4632

Open
15r10nk opened this issue Mar 20, 2025 · 4 comments · May be fixed by #4634
Open

Parsing the with statement with tuple argument failed #4632

15r10nk opened this issue Mar 20, 2025 · 4 comments · May be fixed by #4634
Labels
T: bug Something isn't working

Comments

@15r10nk
Copy link

15r10nk commented Mar 20, 2025

Describe the bug

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

with (name_2, name_2) as name_2:
    pass

black reported the following error:

> black -l 100 -t py312 bug.py
error: cannot format bug.py: INTERNAL ERROR: Black 25.1.1.dev21+g944a38e.d20250317 on Python (CPython) 3
.12.6 produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf
/black/issues.  This diff might be helpful: /tmp/blk_umh51eiz.log

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

the reported diff in /tmp/blk_umh51eiz.log is:

--- src
+++ dst
@@ -5,30 +5,29 @@
         Pass(
         )  # /Pass
         items=
         withitem(
             context_expr=
-            Tuple(
+            Name(
                 ctx=
                 Load(
                 )  # /Load
-                elts=
-                Name(
-                    ctx=
-                    Load(
-                    )  # /Load
-                    id=
-                    'name_2',  # str
-                )  # /Name
-                Name(
-                    ctx=
-                    Load(
-                    )  # /Load
-                    id=
-                    'name_2',  # str
-                )  # /Name
-            )  # /Tuple
+                id=
+                'name_2',  # str
+            )  # /Name
+            optional_vars=
+            None,  # NoneType
+        )  # /withitem
+        withitem(
+            context_expr=
+            Name(
+                ctx=
+                Load(
+                )  # /Load
+                id=
+                'name_2',  # str
+            )  # /Name
             optional_vars=
             Name(
                 ctx=
                 Store(
                 )  # /Store

but it can be parsed by cpython:

from ast import parse
parse(
    'with (name_2, name_2) as name_2:\n'
    '    pass\n'
)

result:


returncode: 0

Environment

  • Black's version: current main (6144c46)
  • OS and Python version: Linux/Python 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ]

Additional context

The bug was found by pysource-codegen (see #3908)
The above problem description was created from a script, let me know if you think it can be improved.

@15r10nk 15r10nk added the T: bug Something isn't working label Mar 20, 2025
@tusharsadhwani
Copy link
Collaborator

What I'm surprised by is that this didn't get caught by primer. Fix should be easy though, thanks again for doing your part, the fuzzer is going to help so much going forward.

@JelleZijlstra
Copy link
Collaborator

This is purely theoretical because it's always a runtime error (tuples don't support the context manager protocol). Still good to fix of course.

@15r10nk
Copy link
Author

15r10nk commented Mar 20, 2025

This is purely theoretical

Yes, but I have no choice. pysource-codegen generates its own test cases, which means it would find it if I try to create an exception here. Generating these cases is the best I can do here.

@JelleZijlstra
Copy link
Collaborator

Yes, that makes sense and I'm grateful that you're doing this; I was replying to Tushar's comment about primer not finding it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants