Skip to content

Clearer errorr message for "Cannot parse" when targetting multiple version #3294

@plannigan

Description

@plannigan

Is your feature request related to a problem? Please describe.

I was working on a project that is only intended to run on Python 3.10. However, the black configuration incorrectly listed multiple target versions because I copied the file from a different project.

When I tried to format a file with a match statement, the error was

error: cannot format foo.py: Cannot parse: 2:6: match bar:

I was initially confused as I knew that black had added Python 3.10 support. Then I noticed my mistake. By saying that the code should target a multiple Python versions, using a match statement would not be valid.

Describe the solution you'd like

If black encounters a parsing error and it was configured to target multiple Python versions, I think it would be helpful to mention that the syntax "might be valid, but not across all target versions". This would be a hint to the user to think about if they should be using that syntax or that black is misconfigured (as it was in my case).

Describe alternatives you've considered

It would require more work, but the parser could back track and attempt each of the parser for each of the listed target versions. That would allow black provide an even more specific error message. Something like "this is valid for Python 3.10, but targeting Python 3.10 and 3.9", while also being able to identify an invalid syntax.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions