Skip to content

Commit 3d81290

Browse files
authored
Move wrap_long_dict_values_in_parens to the preview style (#4561)
1 parent 459562c commit 3d81290

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

CHANGES.md

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

4748
### Configuration
4849

docs/the_black_code_style/future_style.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,47 @@ Currently, the following features are included in the preview style:
2222

2323
- `always_one_newline_after_import`: Always force one blank line after import
2424
statements, except when the line after the import is a comment or an import statement
25+
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries
26+
([see below](labels/wrap-long-dict-values))
2527

2628
(labels/unstable-features)=
2729

2830
The unstable style additionally includes the following features:
2931

3032
- `string_processing`: split long string literals and related changes
3133
([see below](labels/string-processing))
32-
- `wrap_long_dict_values_in_parens`: add parentheses to long values in dictionaries
33-
([see below](labels/wrap-long-dict-values))
3434
- `multiline_string_handling`: more compact formatting of expressions involving
3535
multiline strings ([see below](labels/multiline-string-handling))
3636
- `hug_parens_with_braces_and_square_brackets`: more compact formatting of nested
3737
brackets ([see below](labels/hug-parens))
3838

39+
(labels/wrap-long-dict-values)=
40+
41+
### Improved parentheses management in dicts
42+
43+
For dict literals with long values, they are now wrapped in parentheses. Unnecessary
44+
parentheses are now removed. For example:
45+
46+
```python
47+
my_dict = {
48+
"a key in my dict": a_very_long_variable
49+
* and_a_very_long_function_call()
50+
/ 100000.0,
51+
"another key": (short_value),
52+
}
53+
```
54+
55+
will be changed to:
56+
57+
```python
58+
my_dict = {
59+
"a key in my dict": (
60+
a_very_long_variable * and_a_very_long_function_call() / 100000.0
61+
),
62+
"another key": short_value,
63+
}
64+
```
65+
3966
(labels/hug-parens)=
4067

4168
### 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
122149
parenthesized strings. Unnecessary parentheses are stripped. The stability and status of
123150
this feature istracked in [this issue](https://github.com/psf/black/issues/2188).
124151

125-
(labels/wrap-long-dict-values)=
126-
127-
### Improved parentheses management in dicts
128-
129-
For dict literals with long values, they are now wrapped in parentheses. Unnecessary
130-
parentheses are now removed. For example:
131-
132-
```python
133-
my_dict = {
134-
"a key in my dict": a_very_long_variable
135-
* and_a_very_long_function_call()
136-
/ 100000.0,
137-
"another key": (short_value),
138-
}
139-
```
140-
141-
will be changed to:
142-
143-
```python
144-
my_dict = {
145-
"a key in my dict": (
146-
a_very_long_variable * and_a_very_long_function_call() / 100000.0
147-
),
148-
"another key": short_value,
149-
}
150-
```
151-
152152
(labels/multiline-string-handling)=
153153

154154
### Improved multiline string handling

src/black/mode.py

-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ class Preview(Enum):
208208
UNSTABLE_FEATURES: set[Preview] = {
209209
# Many issues, see summary in https://github.com/psf/black/issues/4042
210210
Preview.string_processing,
211-
# See issues #3452 and #4158
212-
Preview.wrap_long_dict_values_in_parens,
213211
# See issue #4159
214212
Preview.multiline_string_handling,
215213
# See issue #4036 (crash), #4098, #4099 (proposed tweaks)

tests/data/cases/preview_long_dict_values.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# flags: --unstable
1+
# flags: --preview
22
x = {
33
"xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
44
"xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
@@ -287,15 +287,17 @@ def bar():
287287

288288
class Random:
289289
def func():
290-
random_service.status.active_states.inactive = make_new_top_level_state_from_dict({
291-
"topLevelBase": {
292-
"secondaryBase": {
293-
"timestamp": 1234,
294-
"latitude": 1,
295-
"longitude": 2,
296-
"actionTimestamp": (
297-
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
298-
),
299-
}
300-
},
301-
})
290+
random_service.status.active_states.inactive = make_new_top_level_state_from_dict(
291+
{
292+
"topLevelBase": {
293+
"secondaryBase": {
294+
"timestamp": 1234,
295+
"latitude": 1,
296+
"longitude": 2,
297+
"actionTimestamp": (
298+
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
299+
),
300+
}
301+
},
302+
}
303+
)

tests/data/cases/walrus_in_dict.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# flags: --unstable
2-
# This is testing an issue that is specific to the unstable style (wrap_long_dict_values_in_parens)
1+
# flags: --preview
2+
# This is testing an issue that is specific to the preview style (wrap_long_dict_values_in_parens)
33
{
44
"is_update": (up := commit.hash in update_hashes)
55
}
66

77
# output
8-
# This is testing an issue that is specific to the unstable style (wrap_long_dict_values_in_parens)
8+
# This is testing an issue that is specific to the preview style (wrap_long_dict_values_in_parens)
99
{"is_update": (up := commit.hash in update_hashes)}

0 commit comments

Comments
 (0)