blue
blue copied to clipboard
Style Request - More whitespace for json blobs with nested dicts
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.
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.
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.
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.
Yeah good point - instead of json, maybe a dict: blue/black
option?
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.
That's a good eye, the code was originally formatted using black v 20.8b1