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

Move wrap_long_dict_values_in_parens to the preview style #4561

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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)=
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be removed. We happened to have these longer descriptions only for unstable features, but I think that was just by chance.


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