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

Formatter: Improve single-with item formatting for Python 3.8 or older #10276

Merged
merged 1 commit into from Mar 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -318,3 +318,13 @@
)
):
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
pass

if True:
with anyio.CancelScope(shield=True) if get_running_loop() else contextlib.nullcontext() as b:
pass
9 changes: 6 additions & 3 deletions crates/ruff_python_formatter/src/other/with_item.rs
Expand Up @@ -7,6 +7,7 @@ use crate::expression::parentheses::{
is_expression_parenthesized, parenthesized, Parentheses, Parenthesize,
};
use crate::prelude::*;
use crate::preview::is_with_single_item_pre_39_enabled;

#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum WithItemLayout {
Expand Down Expand Up @@ -49,7 +50,7 @@ pub enum WithItemLayout {
/// with a, b:
/// ...
/// ```
Python38OrOlder,
Python38OrOlder { single: bool },

/// A with item where the `with` formatting adds parentheses around all context managers if necessary.
///
Expand Down Expand Up @@ -135,8 +136,10 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
)?;
}

WithItemLayout::Python38OrOlder => {
let parenthesize = if is_parenthesized {
WithItemLayout::Python38OrOlder { single } => {
let parenthesize = if (single && is_with_single_item_pre_39_enabled(f.context()))
|| is_parenthesized
{
Parenthesize::IfBreaks
} else {
Parenthesize::IfRequired
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_python_formatter/src/preview.rs
Expand Up @@ -18,3 +18,7 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}

pub(crate) fn is_with_single_item_pre_39_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/statement/stmt_with.rs
Expand Up @@ -104,7 +104,9 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
WithItemsLayout::Python38OrOlder => f
.join_with(format_args![token(","), space()])
.entries(with_stmt.items.iter().map(|item| {
item.format().with_options(WithItemLayout::Python38OrOlder)
item.format().with_options(WithItemLayout::Python38OrOlder {
single: with_stmt.items.len() == 1,
})
}))
.finish(),

Expand Down
Expand Up @@ -324,6 +324,16 @@ with (
)
):
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
pass

if True:
with anyio.CancelScope(shield=True) if get_running_loop() else contextlib.nullcontext() as b:
pass
```

## Outputs
Expand Down Expand Up @@ -688,6 +698,121 @@ with open(
"/etc/hosts" # This is an incredibly long comment that has been replaced for sanitization
):
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
pass

with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
pass

if True:
with anyio.CancelScope(
shield=True
) if get_running_loop() else contextlib.nullcontext() as b:
pass
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -49,7 +49,9 @@
with a: # should remove brackets
pass

-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as c:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as c:
Comment on lines +724 to +727
Copy link
Member Author

Choose a reason for hiding this comment

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

This change might be controversial. I would say it's not ideal but the best we can do with the limitations given by Python 3.8, other than not parenthesizing and exceeding the configured line width.

pass


@@ -214,7 +216,9 @@
pass

# Breaking of with items.
-with test as ( # bar # foo
+with (
+ test # bar
+) as ( # foo
# test
foo
):
@@ -226,7 +230,9 @@
):
pass

-with test as ( # bar # foo # baz
+with (
+ test # bar
+) as ( # foo # baz
# test
foo
):
@@ -279,7 +285,9 @@
) as b:
pass

-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as b:
pass

with (
@@ -322,15 +330,19 @@
pass

if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext():
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ):
pass

if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext() as c:
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ) as c:
pass

with Child(aaaaaaaaa, bbbbbbbbbbbbbbb, cccccc), Document(
@@ -344,14 +356,20 @@
):
pass

-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+):
pass

-with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b:
+with (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+) as b:
pass

if True:
- with anyio.CancelScope(
- shield=True
- ) if get_running_loop() else contextlib.nullcontext() as b:
+ with (
+ anyio.CancelScope(shield=True)
+ if get_running_loop()
+ else contextlib.nullcontext()
+ ) as b:
pass
```


Expand Down Expand Up @@ -1093,4 +1218,23 @@ with open(
"/etc/hosts" # This is an incredibly long comment that has been replaced for sanitization
):
pass

with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
pass

with (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb as b
):
pass

if True:
with (
anyio.CancelScope(shield=True)
if get_running_loop()
else contextlib.nullcontext() as b
):
pass
```