Skip to content

Commit

Permalink
Move wrap_long_dict_values_in_parens to the preview style (#4561)
Browse files Browse the repository at this point in the history
  • Loading branch information
cobaltt7 authored Jan 28, 2025
1 parent 459562c commit 3d81290
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The following changes were not in any previous release:
- Collapse multiple empty lines after an import into one (#4489)
- Prevent `string_processing` and `wrap_long_dict_values_in_parens` from removing
parentheses around long dictionary values (#4377)
- Move `wrap_long_dict_values_in_parens` from the unstable to preview style (#4561)

### Configuration

Expand Down
58 changes: 29 additions & 29 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,47 @@ Currently, the following features are included in the preview style:

- `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
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries
([see below](labels/wrap-long-dict-values))

(labels/unstable-features)=

The unstable style additionally includes the following features:

- `string_processing`: split long string literals and related changes
([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 strings ([see below](labels/multiline-string-handling))
- `hug_parens_with_braces_and_square_brackets`: more compact formatting of nested
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)=

### Improved multiline dictionary and list indentation for sole function parameter
Expand Down Expand Up @@ -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
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)=

### Improved multiline string handling
Expand Down
2 changes: 0 additions & 2 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ class Preview(Enum):
UNSTABLE_FEATURES: set[Preview] = {
# Many issues, see summary in https://github.com/psf/black/issues/4042
Preview.string_processing,
# See issues #3452 and #4158
Preview.wrap_long_dict_values_in_parens,
# See issue #4159
Preview.multiline_string_handling,
# See issue #4036 (crash), #4098, #4099 (proposed tweaks)
Expand Down
28 changes: 15 additions & 13 deletions tests/data/cases/preview_long_dict_values.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flags: --unstable
# flags: --preview
x = {
"xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
"xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
Expand Down Expand Up @@ -287,15 +287,17 @@ def bar():

class Random:
def func():
random_service.status.active_states.inactive = make_new_top_level_state_from_dict({
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": (
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
),
}
},
})
random_service.status.active_states.inactive = make_new_top_level_state_from_dict(
{
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": (
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
),
}
},
}
)
6 changes: 3 additions & 3 deletions tests/data/cases/walrus_in_dict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# flags: --unstable
# This is testing an issue that is specific to the unstable style (wrap_long_dict_values_in_parens)
# flags: --preview
# 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)
}

# 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)}

0 comments on commit 3d81290

Please sign in to comment.