Code Formatter Questions

Cameron Simpson cs at cskk.id.au
Sun Mar 28 23:45:45 EDT 2021


On 28Mar2021 15:42, Travis Griggs <travisgriggs at gmail.com> wrote:
>I've been looking into using a code formatter as a code base size has grown as well as contributing developers. I've found and played with autopep, black, and yapf. As well as whatever pycharm has (which may just be gui preferences around one of those 3).
>2) I use more and type annotations (at least at the trivial level). But 
>I like to have variable annotations tightly bound to the identifier, 
>kind of like a subscript. So I want to see
>
>  def foo_bar(baz:int) -> bool:
>    yak:str = 'howdy mates'
>
>And NOT
>
>  def foo_bar(baz: int) -> bool:
>    yak: str = 'howdy mates'
>
>In other cases though (dictionaries for example), I'm fine with (and prefer) the spacing.
>
>Is there anyway to make any of these formatters do this?

yapf has many tunings. Worth a look. It is my preferred formatter. By 
comparison, black is both opinionated and has basicly no tuning, 
something I greatly dislike.

>We write a lot of Swift and Kotlin as well as which uses the same 
>general syntax (identifier:Type) for type annotation, and we'd like to 
>have some consistency across the styles (we pack the couplets in those 
>two).

Then you probably want a more tunable formatter in each of these cases, 
raising the odds of formatting in your preferred style.

Finally, you could consider post processing formatted code if some 
specific tuning isn't what you want. (Or patch yapf to add the tuning, 
maybe.)

For example, if some formatter gets you 95% if the way there, maybe you 
can apply your type annotation special case with a simpler tool (eg, for 
me, sed) since you can have more confidence in the physical code layout 
and therefore less need to have a deep understanding of the syntax in 
the post process step.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list