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

Trailing comma on single-line nested dict #1465

Closed
NakulK48 opened this issue May 28, 2020 · 3 comments
Closed

Trailing comma on single-line nested dict #1465

NakulK48 opened this issue May 28, 2020 · 3 comments
Labels
T: bug Something isn't working

Comments

@NakulK48
Copy link

Describe the bug A clear and concise description of what the bug is.

If a dict is nested within another dict, it doesn't reformat correctly.

To Reproduce Steps to reproduce the behavior:

Take this:

my_dict = {
    "outer": {
        "a": 1,
        "b": 2,
    }
}

Pass it through black:

my_dict = {"outer": {"a": 1, "b": 2,}}

This has a trailing comma, which violates https://www.flake8rules.com/rules/E231.html

Expected behavior A clear and concise description of what you expected to happen.

Either the snippet unchanged (which is what happens when the dictionary isn't nested)

my_dict = {
    "a": 1,
    "b": 2,
}

Or a single-line dict with no trailing comma (black on this returns with no change)

my_dict = {"outer": {"a": 1, "b": 2}}

Environment (please complete the following information):

  • Version: 19.10b0
  • OS and Python version: Ubuntu 18.04, python3.8.2

Does this bug also happen on master?

Yes

Additional context Add any other context about the problem here.

@NakulK48 NakulK48 added the T: bug Something isn't working label May 28, 2020
@NakulK48
Copy link
Author

I'll add as a stylistic note that it's kind of nice to be able to enforce dict fields going on new lines without having to do fmt: off

@ichard26
Copy link
Collaborator

ichard26 commented May 28, 2020

This is part of #1288. The 'Magic Trailing Comma' doesn't work on nested collections yet.

However, there's issues with it so far. It doesn't yet work on nested collections. Worse yet, in those cases it leaves trailing commas behind when a collection ends up compressed into one line. We need to fix those edge cases to make it generally dependable.

@ichard26
Copy link
Collaborator

ichard26 commented Aug 30, 2020

Fixed by #1612 which allows the trailing comma to work on nested collections.

(black-rHKUX7ap) R:\Programming\black>type temp.py
my_dict = {
    "outer": {
        "a": 1,
        "b": 2,
    },
}

(black-rHKUX7ap) R:\Programming\black>black temp.py --diff --color
All done! ✨ 🍰 ✨
1 file would be left unchanged.

Environment:

  • Black version: Black 20.08b1
  • Python version: CPython 3.8.1
  • OS version: Windows 10 Home Edition Build 18363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants