Mentioned briefly on twitter
Given a 2.7 codebase with print statements in it, such as this bad.py:
def plain_print(arg):
"""simple function to print things plainly"""
print arg
black reports the following:
$ black bad.py
error: cannot format bad.py: cannot use --safe with this file; failed to parse source file with Python 3.6's builtin AST. Re-run with --fast or stop using deprecated Python 2 syntax. AST error message: Missing parentheses in call to 'print'. Did you mean print(arg)? (<unknown>, line 7)
Everything it says is correct. I could stop using deprecated Python 2 syntax (by importing print_function from __future__, or worse by pretending the statement is a function by wrapping the arg in parens). I could also elect to use --fast, which also seems to work.
Still, it would be nice if this _just worked_ for 2.7 sources.
Python version: 3.6
Black version: 18.4a0
Let me know how I can improve the error message since I think it says exactly what went wrong here.
You're using a piece of syntax that doesn't parse with Python 3's AST. I'm not using the builtin AST for formatting (I use lib2to3's CST instead) but I am using it later to confirm that code "before" and "after" still looks the same to Python. I can't use lib2to3's CST for this because "before" and "after" in this case are obviously different (we reformatted the file after all).
In other words, Black is able to reformat your file just fine, it just cannot confirm for you that it didn't make any errors while doing it. Yes, it would be nice if that worked for deprecated pieces of grammar, too, but it would require a lot of work for something that can be trivially rectified on the user's end by using print functions.
I hoped the error message explained this in a bit more succinct language. Let me know what was confusing there.
Hm. How would you feel if Black reformatted print statements to be print functions instead?
I actually think that'd be grand (if possible). I can see that getting tricky for cases where folks are using print with non-standard file object via >>, but if it's possible then I'd say go for it.
Is this the only breaking piece of syntax between 2 and 3?
Alright, so we'll do that. There is also exec which is unfortunately harder to fix (requires six or future) so I'm not going to touch that.
On 4 Apr, 2018, at 6:30 PM, Owen Nelson notifications@github.com wrote:
I actually think that'd be grand (if possible). I can see that getting tricky for cases where folks are using print with non-standard file object via >>, but if it's possible then I'd say go for it.
Is this the only breaking piece of syntax between 2 and 3?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/ambv/black/issues/108#issuecomment-378794468, or mute the thread https://github.com/notifications/unsubscribe-auth/AADX8X_Px4TPwB_URu_LmkGxUDT5xRsSks5tlXOagaJpZM4THqQS.
This is fixed by #840.
Most helpful comment
Let me know how I can improve the error message since I think it says exactly what went wrong here.
You're using a piece of syntax that doesn't parse with Python 3's AST. I'm not using the builtin AST for formatting (I use lib2to3's CST instead) but I am using it later to confirm that code "before" and "after" still looks the same to Python. I can't use lib2to3's CST for this because "before" and "after" in this case are obviously different (we reformatted the file after all).
In other words, Black is able to reformat your file just fine, it just cannot confirm for you that it didn't make any errors while doing it. Yes, it would be nice if that worked for deprecated pieces of grammar, too, but it would require a lot of work for something that can be trivially rectified on the user's end by using print functions.
I hoped the error message explained this in a bit more succinct language. Let me know what was confusing there.