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

Question: Does the code format differ between versions of python 3 (3.7, 3.8, 3.9) ? #2383

Closed
freddyaboulton opened this issue Jul 19, 2021 · 3 comments
Labels
T: user support OP looking for assistance or answers to a question

Comments

@freddyaboulton
Copy link

Hello,

Not sure where the best place to ask this question is so I decided to file an issue. In my organization, we've been running black for a couple of months under python versions 3.7 and 3.8 in our CI and I've never noticed that the output is different between the two.

Is there any scenario where the code style between the python 3 versions will be different? I'm wondering if I should suggest that we only run black with python 3.8.

Thank you! I love using your package.

@HassanAbouelela
Copy link
Contributor

Off the top of my head, you will get some differences when it comes to version-specific features, but only in very limited cases. For a minimal reproducible example, have a look at the following script:

if x := False:
    print("what?")

Walrus was introduced 3.8 I believe, which means a 37 vs 38 black will have slightly different behavior (only in certain cases).
The following commands will work (file.py is the code above):

(38) black file.py
(38) black -t py37 file.py
(38) black -t py37 -c "if x := False: print("wot")"

But not the following:

(37) black file.py
(37) black -t py38 file.py
(37) black -t py38 -c "if x := False: print("wot")"

In short: yes, there is some difference in behavior (mostly in AST parsing in this case), but you may not notice it depending on your code. If your codebase is not intended to support 3.7, then it's probably safe to ignore this. If 3.7 is supported though, it may make sense to keep it.

@freddyaboulton
Copy link
Author

Thank you @HassanAbouelela ! This is a very helpful answer.

@ichard26 ichard26 added the T: user support OP looking for assistance or answers to a question label Jul 19, 2021
@cooperlees
Copy link
Collaborator

cooperlees commented Jul 19, 2021

I think the tl;dr here is just run black under the newest cpython runtime possible (or in use at your company) and you should have less issues as newer version ast will have more support.

  • That said, lib2to3 is getting now more enhancements and we'll be hacking them in via blib2to3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: user support OP looking for assistance or answers to a question
Projects
None yet
Development

No branches or pull requests

4 participants