Move wrap_long_dict_values_in_parens to the preview style (#4561)

This commit is contained in:
cobalt 2025-01-27 19:46:13 -06:00 committed by GitHub
parent 459562c71a
commit 3d8129001f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 47 deletions

View File

@ -43,6 +43,7 @@ The following changes were not in any previous release:
- Collapse multiple empty lines after an import into one (#4489) - Collapse multiple empty lines after an import into one (#4489)
- Prevent `string_processing` and `wrap_long_dict_values_in_parens` from removing - Prevent `string_processing` and `wrap_long_dict_values_in_parens` from removing
parentheses around long dictionary values (#4377) parentheses around long dictionary values (#4377)
- Move `wrap_long_dict_values_in_parens` from the unstable to preview style (#4561)
### Configuration ### Configuration

View File

@ -22,6 +22,8 @@ Currently, the following features are included in the preview style:
- `always_one_newline_after_import`: Always force one blank line after import - `always_one_newline_after_import`: Always force one blank line after import
statements, except when the line after the import is a comment or an import statement statements, except when the line after the import is a comment or an import statement
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries
([see below](labels/wrap-long-dict-values))
(labels/unstable-features)= (labels/unstable-features)=
@ -29,13 +31,38 @@ The unstable style additionally includes the following features:
- `string_processing`: split long string literals and related changes - `string_processing`: split long string literals and related changes
([see below](labels/string-processing)) ([see below](labels/string-processing))
- `wrap_long_dict_values_in_parens`: add parentheses to long values in dictionaries
([see below](labels/wrap-long-dict-values))
- `multiline_string_handling`: more compact formatting of expressions involving - `multiline_string_handling`: more compact formatting of expressions involving
multiline strings ([see below](labels/multiline-string-handling)) multiline strings ([see below](labels/multiline-string-handling))
- `hug_parens_with_braces_and_square_brackets`: more compact formatting of nested - `hug_parens_with_braces_and_square_brackets`: more compact formatting of nested
brackets ([see below](labels/hug-parens)) brackets ([see below](labels/hug-parens))
(labels/wrap-long-dict-values)=
### Improved parentheses management in dicts
For dict literals with long values, they are now wrapped in parentheses. Unnecessary
parentheses are now removed. For example:
```python
my_dict = {
"a key in my dict": a_very_long_variable
* and_a_very_long_function_call()
/ 100000.0,
"another key": (short_value),
}
```
will be changed to:
```python
my_dict = {
"a key in my dict": (
a_very_long_variable * and_a_very_long_function_call() / 100000.0
),
"another key": short_value,
}
```
(labels/hug-parens)= (labels/hug-parens)=
### Improved multiline dictionary and list indentation for sole function parameter ### Improved multiline dictionary and list indentation for sole function parameter
@ -122,33 +149,6 @@ exceed the line length limit. Line continuation backslashes are converted into
parenthesized strings. Unnecessary parentheses are stripped. The stability and status of parenthesized strings. Unnecessary parentheses are stripped. The stability and status of
this feature istracked in [this issue](https://github.com/psf/black/issues/2188). this feature istracked in [this issue](https://github.com/psf/black/issues/2188).
(labels/wrap-long-dict-values)=
### Improved parentheses management in dicts
For dict literals with long values, they are now wrapped in parentheses. Unnecessary
parentheses are now removed. For example:
```python
my_dict = {
"a key in my dict": a_very_long_variable
* and_a_very_long_function_call()
/ 100000.0,
"another key": (short_value),
}
```
will be changed to:
```python
my_dict = {
"a key in my dict": (
a_very_long_variable * and_a_very_long_function_call() / 100000.0
),
"another key": short_value,
}
```
(labels/multiline-string-handling)= (labels/multiline-string-handling)=
### Improved multiline string handling ### Improved multiline string handling

View File

@ -208,8 +208,6 @@ class Preview(Enum):
UNSTABLE_FEATURES: set[Preview] = { UNSTABLE_FEATURES: set[Preview] = {
# Many issues, see summary in https://github.com/psf/black/issues/4042 # Many issues, see summary in https://github.com/psf/black/issues/4042
Preview.string_processing, Preview.string_processing,
# See issues #3452 and #4158
Preview.wrap_long_dict_values_in_parens,
# See issue #4159 # See issue #4159
Preview.multiline_string_handling, Preview.multiline_string_handling,
# See issue #4036 (crash), #4098, #4099 (proposed tweaks) # See issue #4036 (crash), #4098, #4099 (proposed tweaks)

View File

@ -1,4 +1,4 @@
# flags: --unstable # flags: --preview
x = { x = {
"xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": ( "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
"xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx" "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
@ -287,7 +287,8 @@ def bar():
class Random: class Random:
def func(): def func():
random_service.status.active_states.inactive = make_new_top_level_state_from_dict({ random_service.status.active_states.inactive = make_new_top_level_state_from_dict(
{
"topLevelBase": { "topLevelBase": {
"secondaryBase": { "secondaryBase": {
"timestamp": 1234, "timestamp": 1234,
@ -298,4 +299,5 @@ def func():
), ),
} }
}, },
}) }
)

View File

@ -1,9 +1,9 @@
# flags: --unstable # flags: --preview
# This is testing an issue that is specific to the unstable style (wrap_long_dict_values_in_parens) # This is testing an issue that is specific to the preview style (wrap_long_dict_values_in_parens)
{ {
"is_update": (up := commit.hash in update_hashes) "is_update": (up := commit.hash in update_hashes)
} }
# output # output
# This is testing an issue that is specific to the unstable style (wrap_long_dict_values_in_parens) # This is testing an issue that is specific to the preview style (wrap_long_dict_values_in_parens)
{"is_update": (up := commit.hash in update_hashes)} {"is_update": (up := commit.hash in update_hashes)}