Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
phumke opened this issue Sep 23, 2021 · 6 comments
Open

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

phumke opened this issue Sep 23, 2021 · 6 comments

Comments

@phumke
Copy link

phumke commented Sep 23, 2021

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
Copy link
Collaborator

warsaw commented Sep 23, 2021

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
Copy link
Author

phumke commented Sep 23, 2021

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
Copy link
Collaborator

warsaw commented Sep 23, 2021

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
Copy link
Author

phumke commented Sep 23, 2021

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

@grantjenks
Copy link
Owner

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
Copy link
Author

phumke commented Sep 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants