grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
393 stars 21 forks source link

Style Request - More whitespace for json blobs with nested dicts #55

Open phumke opened 3 years ago

phumke commented 3 years ago

I have a black formatted json blob that looks like the following:

    return jsonify(data={
        'id': 123,
        'name': 'A Content Type',
        'type': 'json',
        'field': [
            {
                'name': 'headline',
                'type': 'string'
            },
            {
                'name': 'url',
                'type': 'string'
            }
        ]
    })

After formatting with blue the formatting was changed to the following:

    return jsonify(
        data={
            'id': 123,
            'name': 'A Content Type',
            'type': 'json',
            'field': [{'name': 'headline', 'type': 'string'}, {'name': 'url', 'type': 'string'}],
        }
    )

My own personal preference is towards the original formatting since it's much easier to identify field count and pick out the name fields at a glance.

warsaw commented 3 years ago

I personally think getting formatting like this right 100% of the time is impossible. I suggest adding explicit skip comments to prevent reformatting those lines.

Although, come to think about it, it might be interesting to implement something like # fmt: black and #fmt: blue markers to defer formatting certain sections to black's preferences.

phumke commented 3 years ago

Yeah that's fair - especially given the number of permutations here. The explicit skip helps me in this instance, thank you.

I like the idea of # fmt: black/blue but what about including a config option for json: black/blue - default blue. That way all json blobs in the codebase are formatted consistently without needing to comment them all.

warsaw commented 3 years ago

I guess the problem with that is that blue (or black AFAIK) doesn't really semantically know that this is JSON. It just looks like a function call with an argument set to a dictionary.

phumke commented 3 years ago

Yeah good point - instead of json, maybe a dict: blue/black option?

grantjenks commented 3 years ago

Are you on the most recent version of black? I’m guessing there’s something different about the trailing comma handling. Notice that the “type” field doesn’t have a trailing comma in the first post.

phumke commented 3 years ago

That's a good eye, the code was originally formatted using black v 20.8b1