1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use ruff_formatter::FormatRuleWithOptions;
use ruff_formatter::write;
use ruff_python_ast::{ExceptHandlerExceptHandler, Expr, PythonVersion};
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{ClauseHeader, clause};
use crate::statement::suite::SuiteKind;
#[derive(Copy, Clone, Default)]
pub(crate) enum ExceptHandlerKind {
#[default]
Regular,
Starred,
}
#[derive(Default)]
pub struct FormatExceptHandlerExceptHandler {
pub(crate) except_handler_kind: ExceptHandlerKind,
pub(crate) last_suite_in_statement: bool,
}
impl FormatRuleWithOptions<ExceptHandlerExceptHandler, PyFormatContext<'_>>
for FormatExceptHandlerExceptHandler
{
type Options = FormatExceptHandlerExceptHandler;
fn with_options(mut self, options: Self::Options) -> Self {
self.except_handler_kind = options.except_handler_kind;
self.last_suite_in_statement = options.last_suite_in_statement;
self
}
}
impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHandler {
fn fmt_fields(
&self,
item: &ExceptHandlerExceptHandler,
f: &mut PyFormatter,
) -> FormatResult<()> {
let except_handler_kind = self.except_handler_kind;
let ExceptHandlerExceptHandler {
range: _,
node_index: _,
type_,
name,
body,
} = item;
let comments_info = f.context().comments().clone();
let dangling_comments = comments_info.dangling(item);
write!(
f,
[clause(
ClauseHeader::ExceptHandler(item),
&format_with(|f: &mut PyFormatter| {
write!(
f,
[
token("except"),
match except_handler_kind {
ExceptHandlerKind::Regular => None,
ExceptHandlerKind::Starred => Some(token("*")),
}
]
)?;
match type_.as_deref() {
// For tuples of exception types without an `as` name and on 3.14+, the
// parentheses are optional.
//
// ```py
// try:
// ...
// except BaseException, Exception: # Ok
// ...
// ```
//
// Unless any component of the tuple is starred. This case is actually valid
// syntax on its own but is parsed as `except*`, not a tuple with a starred
// element:
//
// ```py
// try:
// ...
// except *exceptions, BaseException:
// ...
// ```
//
// And this case is an outright `SyntaxError`:
//
// ```py
// try:
// ...
// except BaseException, *exceptions: # SyntaxError
// ...
// ```
Some(Expr::Tuple(tuple))
if f.options().target_version() >= PythonVersion::PY314
&& name.is_none()
&& !tuple.iter().any(Expr::is_starred_expr) =>
{
write!(
f,
[
space(),
tuple.format().with_options(TupleParentheses::NeverPreserve)
]
)?;
}
Some(type_) => {
write!(
f,
[
space(),
maybe_parenthesize_expression(
type_,
item,
Parenthesize::IfBreaks
)
]
)?;
if let Some(name) = name {
write!(f, [space(), token("as"), space(), name.format()])?;
}
}
_ => {}
}
Ok(())
}),
dangling_comments,
body,
SuiteKind::other(self.last_suite_in_statement),
)]
)
}
}