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

Black incorrectly preserves trailing comma on one-line collection literals and function declarations #1202

Closed
alatar224 opened this issue Dec 12, 2019 · 5 comments
Labels
F: trailing comma Full of magic T: bug Something isn't working

Comments

@alatar224
Copy link

The trailing comma is unnecessarily preserved when collapsing an inline dict that is a function argument, though not when the dict is in an assignment. Furthermore, a dict with a trailing comma isn't being collapsed when it could fit onto a single line (I'm not familiar enough to know if this part is a real bug, but it seemed weird). See MWE below.

$ python --version
Python 3.6.0
$ black --version
black, version 19.10b0
$ black --code $'run({\n    1: 2,\n})'
run(
    {1: 2,}
)
$ black --code $'a = {\n    1:2,\n}'
a = {
    1: 2,
}
$ black --code $'a = {\n    1:2\n}'
a = {1: 2}

I couldn't find this anywhere else, apologies if this is a duplicate. Possibly related #925 -- at least, the behavior seemed OK then, anyway.

@zsol zsol added the T: bug Something isn't working label Dec 16, 2019
@zsol
Copy link
Collaborator

zsol commented Dec 16, 2019

This is a bug unfortunately. There are two acceptable behaviors for black:

  1. expand all collection literals with trailing commas, not just top level ones (this would make the first example format to
run(
    {
        1: 2,
    }
)
  1. If a collection literal is not expanded, remove the trailing comma. This would make the first example format to
run(
    {1: 2}
)

@zsol zsol added the F: trailing comma Full of magic label Dec 16, 2019
@zsol zsol changed the title Collapsed inline dict function argument preserves trailing comma Black incorrectly preserves trailing comma on one-line collection literals and function declarations Jan 16, 2020
@SpotlightKid
Copy link

As mentioned in #1010, this triggers the E231 flake8 error.

callahad added a commit to callahad/kuma that referenced this issue Feb 10, 2020
Should be removed by Black, but currently buggy.

See: psf/black#1202
callahad added a commit to callahad/kuma that referenced this issue Feb 10, 2020
Should be removed by Black, but currently buggy.

See: psf/black#1202
peterbe pushed a commit to mdn/kuma that referenced this issue Feb 10, 2020
* Format Python code with Black

* Update .flake8 for Black

* Address most flake8 issues

* Fix flake8 E231: missing whitespace after ','

Should be removed by Black, but currently buggy.

See: psf/black#1202

* Re-run Black

* One final black/flake8 fix

* Fail CI if Black does not pass
HyukjinKwon added a commit to databricks/koalas that referenced this issue Feb 22, 2020
This PR proposes to add [Black](https://github.com/psf/black) into Koalas.

- `dev/linter-python` detects if codes are formatted or not by Black
- `dev/reformat` script that reformats the codes by using black.
- It still keeps 100 line length style, and ignores `E231` and `E203` at pycodestyle. See also psf/black#429 and psf/black#1202. Black automatically formats and violates several rules.
- Update contribution guide.

Resolve #755

Co-authored-by: Deepyaman Datta <[email protected]>
@zsol
Copy link
Collaborator

zsol commented Mar 11, 2020

#1288 will address this issue, it's going to get fixed as part of the next (stable) release

@zsol zsol closed this as completed Mar 11, 2020
rising-star92 added a commit to rising-star92/databricks-koalas that referenced this issue Jan 27, 2023
This PR proposes to add [Black](https://github.com/psf/black) into Koalas.

- `dev/linter-python` detects if codes are formatted or not by Black
- `dev/reformat` script that reformats the codes by using black.
- It still keeps 100 line length style, and ignores `E231` and `E203` at pycodestyle. See also psf/black#429 and psf/black#1202. Black automatically formats and violates several rules.
- Update contribution guide.

Resolve #755

Co-authored-by: Deepyaman Datta <[email protected]>
@Borda
Copy link

Borda commented Jun 29, 2023

You can still get it if you run black with the -C option 🐿️

@hauntsaninja
Copy link
Collaborator

That's not true with latest black. Please open a new issue with a repro if you still see something like this with latest black.

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

No branches or pull requests

5 participants