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

Add Python version to output of black --version #2992

Closed
CharlesB2 opened this issue Apr 4, 2022 · 5 comments · Fixed by #2997
Closed

Add Python version to output of black --version #2992

CharlesB2 opened this issue Apr 4, 2022 · 5 comments · Fixed by #2997
Labels
C: configuration CLI and configuration T: enhancement New feature or request

Comments

@CharlesB2
Copy link

Describe the bug

With structural pattern matching, Black fails with an invalid syntax, even though target version is py310.

To Reproduce

For example, take this code:

from rest_framework import serializers
from audit.models.health_check import ParentModel

class ServiceRelatedField(serializers.Serializer):
    def to_representation(self, value):
        match value:
            case ParentModel():
                data = {"name": value.actor.login}
            case _:
                raise Exception("Unexpected type of service object")
        data["id"] = value.id
        data["class"] = self.context["class"]
        return data

And run it with these arguments:

$ black  --target-version py310 file.py

The resulting error is:

error: cannot format file.py: cannot use --safe with this file; failed to parse source file AST: invalid syntax (, line 6)
This could be caused by running Black with an older Python version that does not support new syntax used in your source file.

Reproduced also with black show

Expected behavior

Environment

  • Black's version: 2.33.0
  • OS and Python version: MacOs 12.3/Python 3.10.2
@CharlesB2 CharlesB2 added the T: bug Something isn't working label Apr 4, 2022
@JelleZijlstra
Copy link
Collaborator

I can't reproduce this. Make sure you're running Black itself under Python 3.10+ too:

% python -m black --target-version=py310 matchsamplebug.py
All done! ✨ 🍰 ✨
1 file left unchanged.
% python -m black --version
python -m black, 22.3.0 (compiled: no)
% python --version 
Python 3.11.0a6+

@ichard26 ichard26 added S: needs repro Needs a functional reproduction C: parser How we parse code. Or fail to parse it. labels Apr 4, 2022
@CharlesB2
Copy link
Author

Thanks, that was the error indeed!

@CharlesB2
Copy link
Author

BTW it would be helpful to know with which is the Python version in the output of black --version

@JelleZijlstra JelleZijlstra changed the title Structural pattern matching fails with invalid syntax Add Python version to output of black --version Apr 4, 2022
@JelleZijlstra
Copy link
Collaborator

BTW it would be helpful to know with which is the Python version in the output of black --version

Good point, reopening this issue to reflect that suggestion

@JelleZijlstra JelleZijlstra reopened this Apr 4, 2022
@JelleZijlstra JelleZijlstra added C: configuration CLI and configuration and removed T: bug Something isn't working S: needs repro Needs a functional reproduction C: parser How we parse code. Or fail to parse it. labels Apr 4, 2022
@CharlesB2
Copy link
Author

Also it may be fair to issue an error or a warning if --target-version is py310 but isn't running with Python 3.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants