Compare commits
1 Commits
codex/stab
...
parens_for
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5326ca1873 |
@@ -74,6 +74,7 @@ impl NeedsParentheses for ExprNamed {
|
||||
|| parent.is_stmt_delete()
|
||||
|| parent.is_stmt_for()
|
||||
|| parent.is_stmt_function_def()
|
||||
|| parent.is_match_case()
|
||||
{
|
||||
OptionalParentheses::Always
|
||||
} else {
|
||||
|
||||
@@ -4,8 +4,12 @@ use ruff_python_ast::MatchCase;
|
||||
|
||||
use crate::builders::parenthesize_if_expands;
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::{
|
||||
NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::parens_for_long_if_clauses_in_case_block;
|
||||
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -58,7 +62,18 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||
}
|
||||
|
||||
if let Some(guard) = guard {
|
||||
write!(f, [space(), token("if"), space(), guard.format()])?;
|
||||
write!(f, [space(), token("if"), space(),])?;
|
||||
|
||||
if parens_for_long_if_clauses_in_case_block(f.context()) {
|
||||
maybe_parenthesize_expression(
|
||||
guard,
|
||||
item,
|
||||
Parenthesize::IfBreaksOrIfRequired,
|
||||
)
|
||||
.fmt(f)?;
|
||||
} else {
|
||||
guard.format().fmt(f)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -22,3 +22,8 @@ pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool
|
||||
pub(crate) fn is_with_single_item_pre_39_enabled(context: &PyFormatContext) -> bool {
|
||||
context.is_preview()
|
||||
}
|
||||
|
||||
/// Returns `true` if the [`parens_for_long_if_clauses_in_case_block`](https://github.com/psf/black/pull/4269) preview style is enabled.
|
||||
pub(crate) fn parens_for_long_if_clauses_in_case_block(context: &PyFormatContext) -> bool {
|
||||
context.is_preview()
|
||||
}
|
||||
|
||||
@@ -69,20 +69,7 @@ match 1:
|
||||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,10 +1,10 @@
|
||||
match 1:
|
||||
- case _ if True:
|
||||
+ case _ if (True):
|
||||
pass
|
||||
|
||||
|
||||
match 1:
|
||||
- case _ if True:
|
||||
+ case _ if (True):
|
||||
pass
|
||||
|
||||
|
||||
@@ -25,27 +25,33 @@
|
||||
@@ -25,12 +25,16 @@
|
||||
|
||||
|
||||
match 1:
|
||||
@@ -101,18 +88,7 @@ match 1:
|
||||
pass
|
||||
|
||||
|
||||
match 1:
|
||||
- case _ if True: # this is a comment
|
||||
+ case _ if (True): # this is a comment
|
||||
pass
|
||||
|
||||
|
||||
match 1:
|
||||
- case _ if True: # comment over the line limit unless parens are removed x
|
||||
+ case _ if (
|
||||
+ True
|
||||
+ ): # comment over the line limit unless parens are removed x
|
||||
pass
|
||||
@@ -45,7 +49,7 @@
|
||||
|
||||
|
||||
match 1:
|
||||
@@ -129,12 +105,12 @@ match 1:
|
||||
|
||||
```python
|
||||
match 1:
|
||||
case _ if (True):
|
||||
case _ if True:
|
||||
pass
|
||||
|
||||
|
||||
match 1:
|
||||
case _ if (True):
|
||||
case _ if True:
|
||||
pass
|
||||
|
||||
|
||||
@@ -169,14 +145,12 @@ match 1:
|
||||
|
||||
|
||||
match 1:
|
||||
case _ if (True): # this is a comment
|
||||
case _ if True: # this is a comment
|
||||
pass
|
||||
|
||||
|
||||
match 1:
|
||||
case _ if (
|
||||
True
|
||||
): # comment over the line limit unless parens are removed x
|
||||
case _ if True: # comment over the line limit unless parens are removed x
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@@ -1235,4 +1235,41 @@ match x:
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -82,7 +82,9 @@
|
||||
|
||||
|
||||
match long_lines:
|
||||
- case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
|
||||
+ case "this is a long line for if condition" if (
|
||||
+ aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
|
||||
+ ): # comment
|
||||
pass
|
||||
|
||||
case "this is a long line for if condition with parentheses" if (
|
||||
@@ -90,7 +92,7 @@
|
||||
): # comment
|
||||
pass
|
||||
|
||||
- case "named expressions aren't special" if foo := 1:
|
||||
+ case "named expressions aren't special" if (foo := 1):
|
||||
pass
|
||||
|
||||
case "named expressions aren't that special" if (foo := 1):
|
||||
@@ -101,9 +103,9 @@
|
||||
): # another comment
|
||||
pass
|
||||
|
||||
- case {
|
||||
- "long_long_long_key": str(long_long_long_key)
|
||||
- } if value := "long long long long long long long long long long long value":
|
||||
+ case {"long_long_long_key": str(long_long_long_key)} if (
|
||||
+ value := "long long long long long long long long long long long value"
|
||||
+ ):
|
||||
pass
|
||||
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user