Skip to content

Commit 5cfd581

Browse files
committed
Clean-up
1 parent c8d3858 commit 5cfd581

12 files changed

Lines changed: 54 additions & 33 deletions

File tree

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ class DefaultRunner:
2424
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
2525

2626

27-
# Regression test: Preserve parentheses around invalid type expressions.
27+
# Preserve parentheses around invalid type expressions.
2828
def preserve_invalid_type_expressions_in_annotations():
2929
named: (value := int) = 1
3030
yielded_with_value: (yield 1) = 1
3131
yielded: (yield 1)
32+
yielded_from: (yield from iter) = 1
3233

3334

3435
async def preserve_invalid_type_expressions_in_async_annotations():

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/function.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,16 @@ def args_with_type_annotations_no_after_colon_comment(
563563
int # trailing type
564564
# after type
565565
): pass
566+
567+
568+
# Preserve parentheses around invalid type expressions in parameter annotations.
569+
def preserve_named_param_annotation(x: (value := int)):
570+
pass
571+
572+
573+
def preserve_yield_param_annotation(x: (yield 1)):
574+
pass
575+
576+
577+
async def preserve_await_param_annotation(x: (await g())):
578+
pass

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/return_annotation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,6 @@ def process_board_action(
194194
pass
195195

196196

197-
# Regression test: Preserve parentheses around invalid type expressions.
197+
# Preserve parentheses around invalid type expressions.
198198
async def preserve_await_return_annotation() -> (await g()):
199199
pass

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
type long_bound_short_default[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)=a]=int
152152
type short_bound_long_default[T:a= (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)]=int
153153

154-
# Regression test: Preserve parentheses around invalid type expressions.
154+
# Preserve parentheses around invalid type expressions.
155155
type NamedExprValue = (value := int)
156156

157157
async def preserve_await_type_alias_value():

crates/ruff_python_formatter/src/expression/expr_await.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use ruff_formatter::write;
22
use ruff_python_ast::AnyNodeRef;
33
use ruff_python_ast::ExprAwait;
44

5-
use crate::expression::is_type_expression_parent;
65
use crate::expression::maybe_parenthesize_expression;
76
use crate::expression::parentheses::{
87
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
@@ -37,7 +36,7 @@ impl NeedsParentheses for ExprAwait {
3736
parent: AnyNodeRef,
3837
context: &PyFormatContext,
3938
) -> OptionalParentheses {
40-
if parent.is_expr_await() || is_type_expression_parent(parent) {
39+
if parent.is_expr_await() {
4140
OptionalParentheses::Always
4241
} else if is_expression_parenthesized(
4342
self.value.as_ref().into(),

crates/ruff_python_formatter/src/expression/expr_named.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ruff_python_ast::AnyNodeRef;
33
use ruff_python_ast::ExprNamed;
44

55
use crate::comments::dangling_comments;
6-
use crate::expression::is_type_expression_parent;
76
use crate::expression::parentheses::{
87
NeedsParentheses, OptionalParentheses, in_parentheses_only_soft_line_break_or_space,
98
};
@@ -54,8 +53,7 @@ impl NeedsParentheses for ExprNamed {
5453
) -> OptionalParentheses {
5554
// Unlike tuples, named expression parentheses are not part of the range even when
5655
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
57-
if is_type_expression_parent(parent)
58-
|| parent.is_stmt_ann_assign()
56+
if parent.is_stmt_ann_assign()
5957
|| parent.is_stmt_assign()
6058
|| parent.is_stmt_aug_assign()
6159
|| parent.is_stmt_assert()
@@ -67,6 +65,7 @@ impl NeedsParentheses for ExprNamed {
6765
|| parent.is_expr_await()
6866
|| parent.is_stmt_delete()
6967
|| parent.is_stmt_for()
68+
|| parent.is_stmt_function_def()
7069
|| parent.is_expr_lambda()
7170
{
7271
OptionalParentheses::Always

crates/ruff_python_formatter/src/expression/expr_yield.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ruff_python_ast::AnyNodeRef;
33
use ruff_python_ast::{Expr, ExprYield, ExprYieldFrom};
44
use ruff_text_size::{Ranged, TextRange};
55

6-
use crate::expression::is_type_expression_parent;
76
use crate::expression::maybe_parenthesize_expression;
87
use crate::expression::parentheses::{
98
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
@@ -43,10 +42,6 @@ impl NeedsParentheses for AnyExpressionYield<'_> {
4342
parent: AnyNodeRef,
4443
context: &PyFormatContext,
4544
) -> OptionalParentheses {
46-
if is_type_expression_parent(parent) {
47-
return OptionalParentheses::Always;
48-
}
49-
5045
// According to https://docs.python.org/3/reference/grammar.html There are two situations
5146
// where we do not want to always parenthesize a yield expression:
5247
// 1. Right hand side of an assignment, e.g. `x = yield y`

crates/ruff_python_formatter/src/expression/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,19 +1452,6 @@ pub(crate) const fn is_invalid_type_expression(expr: &Expr) -> bool {
14521452
)
14531453
}
14541454

1455-
/// Returns `true` if `parent` represents a type-expression position.
1456-
pub(crate) const fn is_type_expression_parent(parent: AnyNodeRef) -> bool {
1457-
matches!(
1458-
parent,
1459-
AnyNodeRef::StmtFunctionDef(_)
1460-
| AnyNodeRef::StmtTypeAlias(_)
1461-
| AnyNodeRef::Parameter(_)
1462-
| AnyNodeRef::TypeParamTypeVar(_)
1463-
| AnyNodeRef::TypeParamTypeVarTuple(_)
1464-
| AnyNodeRef::TypeParamParamSpec(_)
1465-
)
1466-
}
1467-
14681455
/// Returns the sub-expression to which the left-most character in expression belongs.
14691456
///
14701457
/// For example, in the expression `a + b * c`, the left-most subexpression is `a`. But for

crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ class DefaultRunner:
2929
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
3030
3131
32-
# Regression test: Preserve parentheses around invalid type expressions.
32+
# Preserve parentheses around invalid type expressions.
3333
def preserve_invalid_type_expressions_in_annotations():
3434
named: (value := int) = 1
3535
yielded_with_value: (yield 1) = 1
3636
yielded: (yield 1)
37+
yielded_from: (yield from iter) = 1
3738
3839
3940
async def preserve_invalid_type_expressions_in_async_annotations():
@@ -81,11 +82,12 @@ class DefaultRunner:
8182
)
8283
8384
84-
# Regression test: Preserve parentheses around invalid type expressions.
85+
# Preserve parentheses around invalid type expressions.
8586
def preserve_invalid_type_expressions_in_annotations():
8687
named: (value := int) = 1
8788
yielded_with_value: (yield 1) = 1
8889
yielded: (yield 1)
90+
yielded_from: (yield from iter) = 1
8991
9092
9193
async def preserve_invalid_type_expressions_in_async_annotations():

crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/ruff_python_formatter/tests/fixtures.rs
3-
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/function.py
43
---
54
## Input
65
```python
@@ -569,6 +568,19 @@ def args_with_type_annotations_no_after_colon_comment(
569568
int # trailing type
570569
# after type
571570
): pass
571+
572+
573+
# Preserve parentheses around invalid type expressions in parameter annotations.
574+
def preserve_named_param_annotation(x: (value := int)):
575+
pass
576+
577+
578+
def preserve_yield_param_annotation(x: (yield 1)):
579+
pass
580+
581+
582+
async def preserve_await_param_annotation(x: (await g())):
583+
pass
572584
```
573585

574586
## Output
@@ -1318,6 +1330,19 @@ def args_with_type_annotations_no_after_colon_comment(
13181330
# after type
13191331
):
13201332
pass
1333+
1334+
1335+
# Preserve parentheses around invalid type expressions in parameter annotations.
1336+
def preserve_named_param_annotation(x: (value := int)):
1337+
pass
1338+
1339+
1340+
def preserve_yield_param_annotation(x: (yield 1)):
1341+
pass
1342+
1343+
1344+
async def preserve_await_param_annotation(x: (await g())):
1345+
pass
13211346
```
13221347

13231348

0 commit comments

Comments
 (0)