Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Doesn't Python have an autoformatting tool like gofmt or Prettier?


Black (The uncompromising Python code formatter) is all the rage atm.

https://github.com/ambv/black


Yes. Most of the projects now use black https://github.com/ambv/black


The problem that black had last time I saw it on hacker news is that it only inserts whitespace. Unlike braces-and-semicolon languages, that is sometimes not enough to format code well. For example, given the following line:

    x[1][4] = a[2] + b[4]
Black will format like either of these:

    x[1][
        4] = a[2] + b[4]
    
    x[1][4] = a[
        2] + b[4]
But not like either of these, unless you insert the brackets yourself:

    x[1][4] = (
        a[2] + b[4])
    
    x[1][4] = (a[2] 
        + b[4])
Maybe this sounds like just one little problem but I think it's a fundamental flaw. I have seen the result of a Python formatter (not Black but had the same problem) applied to a couple of files and it's a total mess. I'd take inconsistent column widths over that any day. I asked the creator of black about it and he was pretty dismissive.


I use yapf and in such cases backslashes work fine:

    x[1][4] =\ 
        a[2] + b[4]



there's autopep8 and a few other tools but none are considered idiomatic.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: