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

Generator comprehension in call with trailing comma spuriously fails #1038

Closed
msullivan opened this issue Oct 1, 2019 · 2 comments
Closed
Labels
C: crash Black is crashing C: parser How we parse code. Or fail to parse it.

Comments

@msullivan
Copy link
Contributor

Trying to reformat list(i for i in range(10), ) fails with "Black produced code that is not equivalent to the source."

Passing a generator comprehension to a function as its sole argument with a trailing comma is valid in Python 2.7 but not in Python 3.7. (It is valid in Python 3.6 as well but typed_ast rejects it in 3.6 mode because it is based on the 3.7 parser... I expected the Python 3.8 parser rejects it as well.)

Black fails on this because the original code parses only in 2.7, so it gets a 2.7 AST. Once the trailing comma is removed, it parses in 3.7 as well, so gets a 3.7 AST, which will never match the 2.7 one.

I think the solution is probably to ensure that the same parser is used for before and after?

@JelleZijlstra JelleZijlstra added the C: crash Black is crashing label May 30, 2021
@JelleZijlstra
Copy link
Collaborator

This still reproduces, but only with --skip-magic-trailing-comma.

@JelleZijlstra JelleZijlstra added the C: parser How we parse code. Or fail to parse it. label May 30, 2021
@JelleZijlstra
Copy link
Collaborator

This no longer crashes because we dropped support for 2.7.

Technically there's a bug here as this syntax works at runtime in 3.6 and Black rejects it even though we still support formatting Python 3.6, but I don't care.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: crash Black is crashing C: parser How we parse code. Or fail to parse it.
Projects
None yet
Development

No branches or pull requests

2 participants