-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[925] Improve multiline dictionary and list indentation for sole function parameter #3964
Conversation
diff-shades results comparing this PR (c066e69) to main (ef1048d). The full diff is available in the logs under the "Generate HTML diff report" step.
|
@henriholopainen seems like there's a crash on some code in Hypothesis (from the diff-shades failure). Can you take a look? |
Located the issue in Hypothesis (and other places); opening and closing brackets/braces don't necessary belong to the same entity. For example:
becomes currently:
which is clearly very wrong. I'll add some test cases and look for a sophisticated approach for a fix tomorrow. |
Test cases added and logic fixed |
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Thanks for the PR! While I like the style, it's a fairly substantive change for black and goes against the philosophy of diff reduction (in the case you add or remove arguments). I wonder therefore if it makes sense to have magic trailing commas after the single arg prevent this reformatting. |
Do you mean that we should instead format
as it would be formatted without the trailing comma:
? |
Track the preview style of #8279 / psf/black#3964
How does (or should) this handle unpacking? I don't see a test to confirm intended behaviour there. foo(*[
pretend,
many,
and,
long,
arguments,
# ... ..................................................................
*another_list
]) |
Currently it doesn't, but adding support for that would make sense IMO. |
It looks like Black already does what I suggested, I went ahead and added a test case and docs for it here: #3991 |
Maybe I was too hasty in merging this. I think this makes things generally look better, but we should definitely think more about whether we want to go through before we move this change to stable. |
One question, as both a user and a Ruff maintainer: is it intended that this also applies to parenthesized lists, sets, etc., outside of function calls? For example, with this commit: # Input
([1, 2, 3,])
# HEAD^
(
[
1,
2,
3,
]
)
# HEAD
([
1,
2,
3,
]) |
Not intentionally, but I don't mind that we remove the extra newlines there. Ideally we should also remove the parentheses. |
This makes me so very happy, thanks @JelleZijlstra :) |
) ## Summary This PR implement's Black's new single-argument hugging for lists, sets, and dictionaries under preview style. For example, this: ```python foo( [ 1, 2, 3, ] ) ``` Would instead now be formatted as: ```python foo([ 1, 2, 3, ]) ``` A couple notes: - This doesn't apply when the argument has a magic trailing comma. - This _does_ apply when the argument is starred or double-starred. - We don't apply this when there are comments before or after the argument, though Black does in some cases (and moves the comments outside the call parentheses). It doesn't say it in the originating PR (psf/black#3964), but I think this also applies to parenthesized expressions? At least, it does in my testing of preview vs. stable, though it's possible that behavior predated the linked PR. See: #8279. ## Test Plan Before: | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75804 | 1799 | 1648 | | django | 0.99984 | 2772 | 34 | | home-assistant | 0.99963 | 10596 | 146 | | poetry | 0.99925 | 317 | 12 | | transformers | 0.99967 | 2657 | 322 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99980 | 3669 | 18 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 21 | After: | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75804 | 1799 | 1648 | | django | 0.99984 | 2772 | 34 | | home-assistant | 0.99963 | 10596 | 146 | | poetry | 0.96215 | 317 | 34 | | transformers | 0.99967 | 2657 | 322 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99980 | 3669 | 18 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 21 |
Description
Making multiline lists and dictionaries less indented when passed as a function parameter and pairing parens with brackets/braces was discussed at in #925. This PR introduces a conservative first step, where a multiline list or dict as the sole parameter to a function call will have special treatment for better readability. The feature is behind
hug_parens_with_braces_and_square_brackets
preview toggle.Checklist - did you ...
CHANGES.md
if necessary?