Ruff 2024.2 style (#9639) · astral-sh/ruff@a6f32dd (original) (raw)

`@@ -2,12 +2,8 @@ use ruff_formatter::write;

`

2

2

`use ruff_python_ast::StmtAnnAssign;

`

3

3

``

4

4

`use crate::comments::SourceComment;

`

``

5

`+

use crate::expression::is_splittable_expression;

`

5

6

`use crate::expression::parentheses::Parentheses;

`

6

``

`-

use crate::expression::{has_parentheses, is_splittable_expression};

`

7

``

`-

use crate::preview::{

`

8

``

`-

is_parenthesize_long_type_hints_enabled,

`

9

``

`-

is_prefer_splitting_right_hand_side_of_assignments_enabled,

`

10

``

`-

};

`

11

7

`use crate::statement::stmt_assign::{

`

12

8

`AnyAssignmentOperator, AnyBeforeOperator, FormatStatementsLastExpression,

`

13

9

`};

`

`@@ -30,13 +26,7 @@ impl FormatNodeRule for FormatStmtAnnAssign {

`

30

26

`write!(f, [target.format(), token(":"), space()])?;

`

31

27

``

32

28

`if let Some(value) = value {

`

33

``

`-

if is_prefer_splitting_right_hand_side_of_assignments_enabled(f.context())

`

34

``

`` -

// The has_parentheses check can be removed when stabilizing is_parenthesize_long_type_hints.

``

35

``

`` -

// because is_splittable_expression covers both.

``

36

``

`-

&& (has_parentheses(annotation, f.context()).is_some()

`

37

``

`-

|| (is_parenthesize_long_type_hints_enabled(f.context())

`

38

``

`-

&& is_splittable_expression(annotation, f.context())))

`

39

``

`-

{

`

``

29

`+

if is_splittable_expression(annotation, f.context()) {

`

40

30

`FormatStatementsLastExpression::RightToLeft {

`

41

31

`before_operator: AnyBeforeOperator::Expression(annotation),

`

42

32

`operator: AnyAssignmentOperator::Assign,

`

`@@ -47,23 +37,20 @@ impl FormatNodeRule for FormatStmtAnnAssign {

`

47

37

`} else {

`

48

38

`// Remove unnecessary parentheses around the annotation if the parenthesize long type hints preview style is enabled.

`

49

39

`// Ensure we keep the parentheses if the annotation has any comments.

`

50

``

`-

if is_parenthesize_long_type_hints_enabled(f.context()) {

`

51

``

`-

if f.context().comments().has_leading(annotation.as_ref())

`

52

``

`-

|| f.context().comments().has_trailing(annotation.as_ref())

`

53

``

`-

{

`

54

``

`-

annotation

`

55

``

`-

.format()

`

56

``

`-

.with_options(Parentheses::Always)

`

57

``

`-

.fmt(f)?;

`

58

``

`-

} else {

`

59

``

`-

annotation

`

60

``

`-

.format()

`

61

``

`-

.with_options(Parentheses::Never)

`

62

``

`-

.fmt(f)?;

`

63

``

`-

}

`

``

40

`+

if f.context().comments().has_leading(annotation.as_ref())

`

``

41

`+

|| f.context().comments().has_trailing(annotation.as_ref())

`

``

42

`+

{

`

``

43

`+

annotation

`

``

44

`+

.format()

`

``

45

`+

.with_options(Parentheses::Always)

`

``

46

`+

.fmt(f)?;

`

64

47

`} else {

`

65

``

`-

annotation.format().fmt(f)?;

`

``

48

`+

annotation

`

``

49

`+

.format()

`

``

50

`+

.with_options(Parentheses::Never)

`

``

51

`+

.fmt(f)?;

`

66

52

`}

`

``

53

+

67

54

`write!(

`

68

55

` f,

`

69

56

`[

`

`@@ -83,11 +70,7 @@ impl FormatNodeRule for FormatStmtAnnAssign {

`

83

70

`// Decimal # the user's age, used to determine if it's safe for them to use ruff

`

84

71

`// )

`

85

72

```` // ```

````

86

``

`-

if is_parenthesize_long_type_hints_enabled(f.context()) {

`

87

``

`-

FormatStatementsLastExpression::left_to_right(annotation, item).fmt(f)?;

`

88

``

`-

} else {

`

89

``

`-

annotation.format().fmt(f)?;

`

90

``

`-

}

`

``

73

`+

FormatStatementsLastExpression::left_to_right(annotation, item).fmt(f)?;

`

91

74

`}

`

92

75

``

93

76

`if f.options().source_type().is_ipynb()

`