-
Notifications
You must be signed in to change notification settings - Fork 902
/
exit_annotations.rs
345 lines (306 loc) · 10.8 KB
/
exit_annotations.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::fmt::{Display, Formatter};
use ruff_python_ast::{
Expr, ExprBinOp, ExprSubscript, ExprTuple, Identifier, Operator, ParameterWithDefault,
Parameters,
};
use smallvec::SmallVec;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::is_const_none;
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;
/// ## What it does
/// Checks for incorrect function signatures on `__exit__` and `__aexit__`
/// methods.
///
/// ## Why is this bad?
/// Improperly-annotated `__exit__` and `__aexit__` methods can cause
/// unexpected behavior when interacting with type checkers.
///
/// ## Example
/// ```python
/// class Foo:
/// def __exit__(self, typ, exc, tb, extra_arg) -> None:
/// ...
/// ```
///
/// Use instead:
/// ```python
/// class Foo:
/// def __exit__(
/// self,
/// typ: type[BaseException] | None,
/// exc: BaseException | None,
/// tb: TracebackType | None,
/// extra_arg: int = 0,
/// ) -> None:
/// ...
/// ```
#[violation]
pub struct BadExitAnnotation {
func_kind: FuncKind,
error_kind: ErrorKind,
}
impl Violation for BadExitAnnotation {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
let method_name = self.func_kind.to_string();
match self.error_kind {
ErrorKind::StarArgsNotAnnotated => format!("Star-args in `{method_name}` should be annotated with `object`"),
ErrorKind::MissingArgs => format!("If there are no star-args, `{method_name}` should have at least 3 non-keyword-only args (excluding `self`)"),
ErrorKind::ArgsAfterFirstFourMustHaveDefault => format!("All arguments after the first four in `{method_name}` must have a default value"),
ErrorKind::AllKwargsMustHaveDefault => format!("All keyword-only arguments in `{method_name}` must have a default value"),
ErrorKind::FirstArgBadAnnotation => format!("The first argument in `{method_name}` should be annotated with `object` or `type[BaseException] | None`"),
ErrorKind::SecondArgBadAnnotation => format!("The second argument in `{method_name}` should be annotated with `object` or `BaseException | None`"),
ErrorKind::ThirdArgBadAnnotation => format!("The third argument in `{method_name}` should be annotated with `object` or `types.TracebackType | None`"),
}
}
fn fix_title(&self) -> Option<String> {
if matches!(self.error_kind, ErrorKind::StarArgsNotAnnotated) {
Some("Annotate star-args with `object`".to_string())
} else {
None
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum FuncKind {
Sync,
Async,
}
impl Display for FuncKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
FuncKind::Sync => write!(f, "__exit__"),
FuncKind::Async => write!(f, "__aexit__"),
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum ErrorKind {
StarArgsNotAnnotated,
MissingArgs,
FirstArgBadAnnotation,
SecondArgBadAnnotation,
ThirdArgBadAnnotation,
ArgsAfterFirstFourMustHaveDefault,
AllKwargsMustHaveDefault,
}
/// PYI036
pub(crate) fn bad_exit_annotation(
checker: &mut Checker,
is_async: bool,
name: &Identifier,
parameters: &Parameters,
) {
let func_kind = match name.as_str() {
"__exit__" if !is_async => FuncKind::Sync,
"__aexit__" if is_async => FuncKind::Async,
_ => return,
};
let positional_args = parameters
.args
.iter()
.chain(parameters.posonlyargs.iter())
.collect::<SmallVec<[&ParameterWithDefault; 4]>>();
// If there are less than three positional arguments, at least one of them must be a star-arg,
// and it must be annotated with `object`.
if positional_args.len() < 4 {
check_short_args_list(checker, parameters, func_kind);
}
// Every positional argument (beyond the first four) must have a default.
for parameter in positional_args
.iter()
.skip(4)
.filter(|parameter| parameter.default.is_none())
{
checker.diagnostics.push(Diagnostic::new(
BadExitAnnotation {
func_kind,
error_kind: ErrorKind::ArgsAfterFirstFourMustHaveDefault,
},
parameter.range(),
));
}
// ...as should all keyword-only arguments.
for parameter in parameters
.kwonlyargs
.iter()
.filter(|arg| arg.default.is_none())
{
checker.diagnostics.push(Diagnostic::new(
BadExitAnnotation {
func_kind,
error_kind: ErrorKind::AllKwargsMustHaveDefault,
},
parameter.range(),
));
}
check_positional_args(checker, &positional_args, func_kind);
}
/// Determine whether a "short" argument list (i.e., an argument list with less than four elements)
/// contains a star-args argument annotated with `object`. If not, report an error.
fn check_short_args_list(checker: &mut Checker, parameters: &Parameters, func_kind: FuncKind) {
if let Some(varargs) = ¶meters.vararg {
if let Some(annotation) = varargs
.annotation
.as_ref()
.filter(|ann| !is_object_or_unused(ann, checker.semantic()))
{
let mut diagnostic = Diagnostic::new(
BadExitAnnotation {
func_kind,
error_kind: ErrorKind::StarArgsNotAnnotated,
},
annotation.range(),
);
if checker.patch(diagnostic.kind.rule()) {
if checker.semantic().is_builtin("object") {
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
"object".to_string(),
annotation.range(),
)));
}
}
checker.diagnostics.push(diagnostic);
}
} else {
checker.diagnostics.push(Diagnostic::new(
BadExitAnnotation {
func_kind,
error_kind: ErrorKind::MissingArgs,
},
parameters.range(),
));
}
}
/// Determines whether the positional arguments of an `__exit__` or `__aexit__` method are
/// annotated correctly.
fn check_positional_args(
checker: &mut Checker,
positional_args: &[&ParameterWithDefault],
kind: FuncKind,
) {
// For each argument, define the predicate against which to check the annotation.
type AnnotationValidator = fn(&Expr, &SemanticModel) -> bool;
let validations: [(ErrorKind, AnnotationValidator); 3] = [
(ErrorKind::FirstArgBadAnnotation, is_base_exception_type),
(ErrorKind::SecondArgBadAnnotation, is_base_exception),
(ErrorKind::ThirdArgBadAnnotation, is_traceback_type),
];
for (arg, (error_info, predicate)) in positional_args.iter().skip(1).take(3).zip(validations) {
let Some(annotation) = arg.parameter.annotation.as_ref() else {
continue;
};
if is_object_or_unused(annotation, checker.semantic()) {
continue;
}
// If there's an annotation that's not `object` or `Unused`, check that the annotated type
// matches the predicate.
if non_none_annotation_element(annotation, checker.semantic())
.is_some_and(|elem| predicate(elem, checker.semantic()))
{
continue;
}
checker.diagnostics.push(Diagnostic::new(
BadExitAnnotation {
func_kind: kind,
error_kind: error_info,
},
annotation.range(),
));
}
}
/// Return the non-`None` annotation element of a PEP 604-style union or `Optional` annotation.
fn non_none_annotation_element<'a>(
annotation: &'a Expr,
semantic: &SemanticModel,
) -> Option<&'a Expr> {
// E.g., `typing.Union` or `typing.Optional`
if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation {
if semantic.match_typing_expr(value, "Optional") {
return if is_const_none(slice) {
None
} else {
Some(slice)
};
}
if !semantic.match_typing_expr(value, "Union") {
return None;
}
let Expr::Tuple(ExprTuple { elts, .. }) = slice.as_ref() else {
return None;
};
let [left, right] = elts.as_slice() else {
return None;
};
return match (is_const_none(left), is_const_none(right)) {
(false, true) => Some(left),
(true, false) => Some(right),
(true, true) => None,
(false, false) => None,
};
}
// PEP 604-style union (e.g., `int | None`)
if let Expr::BinOp(ExprBinOp {
op: Operator::BitOr,
left,
right,
..
}) = annotation
{
if !is_const_none(left) {
return Some(left);
}
if !is_const_none(right) {
return Some(right);
}
return None;
}
None
}
/// Return `true` if the [`Expr`] is the `object` builtin or the `_typeshed.Unused` type.
fn is_object_or_unused(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.as_ref()
.is_some_and(|call_path| {
matches!(
call_path.as_slice(),
["" | "builtins", "object"] | ["_typeshed", "Unused"]
)
})
}
/// Return `true` if the [`Expr`] is `BaseException`.
fn is_base_exception(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "BaseException"]))
}
/// Return `true` if the [`Expr`] is the `types.TracebackType` type.
fn is_traceback_type(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_call_path(expr)
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["types", "TracebackType"]))
}
/// Return `true` if the [`Expr`] is, e.g., `Type[BaseException]`.
fn is_base_exception_type(expr: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Subscript(ExprSubscript { value, slice, .. }) = expr else {
return false;
};
if semantic.match_typing_expr(value, "Type")
|| semantic
.resolve_call_path(value)
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["" | "builtins", "type"]))
{
is_base_exception(slice, semantic)
} else {
false
}
}