-
Notifications
You must be signed in to change notification settings - Fork 902
/
non_slot_assignment.rs
242 lines (218 loc) · 8.02 KB
/
non_slot_assignment.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
use rustc_hash::FxHashSet;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Expr, Stmt};
use ruff_text_size::{Ranged, TextRange};
use crate::checkers::ast::Checker;
/// ## What it does
/// Checks for assignments to attributes that are not defined in `__slots__`.
///
/// ## Why is this bad?
/// When using `__slots__`, only the specified attributes are allowed.
/// Attempting to assign to an attribute that is not defined in `__slots__`
/// will result in an `AttributeError` at runtime.
///
/// ## Known problems
/// This rule can't detect `__slots__` implementations in superclasses, and
/// so limits its analysis to classes that inherit from (at most) `object`.
///
/// ## Example
/// ```python
/// class Student:
/// __slots__ = ("name",)
///
/// def __init__(self, name, surname):
/// self.name = name
/// self.surname = surname # [assigning-non-slot]
/// self.setup()
///
/// def setup(self):
/// pass
/// ```
///
/// Use instead:
/// ```python
/// class Student:
/// __slots__ = ("name", "surname")
///
/// def __init__(self, name, surname):
/// self.name = name
/// self.surname = surname
/// self.setup()
///
/// def setup(self):
/// pass
/// ```
#[violation]
pub struct NonSlotAssignment {
name: String,
}
impl Violation for NonSlotAssignment {
#[derive_message_formats]
fn message(&self) -> String {
let NonSlotAssignment { name } = self;
format!("Attribute `{name}` is not defined in class's `__slots__`")
}
}
/// E0237
pub(crate) fn non_slot_assignment(checker: &mut Checker, class_def: &ast::StmtClassDef) {
// If the class inherits from another class (aside from `object`), then it's possible that
// the parent class defines the relevant `__slots__`.
if !class_def.bases().iter().all(|base| {
checker
.semantic()
.resolve_qualified_name(base)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["", "object"]))
}) {
return;
}
for attribute in is_attributes_not_in_slots(&class_def.body) {
checker.diagnostics.push(Diagnostic::new(
NonSlotAssignment {
name: attribute.name.to_string(),
},
attribute.range(),
));
}
}
#[derive(Debug)]
struct AttributeAssignment<'a> {
/// The name of the attribute that is assigned to.
name: &'a str,
/// The range of the attribute that is assigned to.
range: TextRange,
}
impl Ranged for AttributeAssignment<'_> {
fn range(&self) -> TextRange {
self.range
}
}
/// Return a list of attributes that are assigned to but not included in `__slots__`.
fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment> {
// First, collect all the attributes that are assigned to `__slots__`.
let mut slots = FxHashSet::default();
for statement in body {
match statement {
// Ex) `__slots__ = ("name",)`
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
let [Expr::Name(ast::ExprName { id, .. })] = targets.as_slice() else {
continue;
};
if id == "__slots__" {
slots.extend(slots_attributes(value));
}
}
// Ex) `__slots__: Tuple[str, ...] = ("name",)`
Stmt::AnnAssign(ast::StmtAnnAssign {
target,
value: Some(value),
..
}) => {
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
continue;
};
if id == "__slots__" {
slots.extend(slots_attributes(value));
}
}
// Ex) `__slots__ += ("name",)`
Stmt::AugAssign(ast::StmtAugAssign { target, value, .. }) => {
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
continue;
};
if id == "__slots__" {
slots.extend(slots_attributes(value));
}
}
_ => {}
}
}
if slots.is_empty() || slots.contains("__dict__") {
return vec![];
}
// Second, find any assignments that aren't included in `__slots__`.
let mut assignments = vec![];
for statement in body {
let Stmt::FunctionDef(ast::StmtFunctionDef { name, body, .. }) = statement else {
continue;
};
if name == "__init__" {
for statement in body {
match statement {
// Ex) `self.name = name`
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
let [Expr::Attribute(attribute)] = targets.as_slice() else {
continue;
};
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
continue;
};
if id == "self" && !slots.contains(attribute.attr.as_str()) {
assignments.push(AttributeAssignment {
name: &attribute.attr,
range: attribute.range(),
});
}
}
// Ex) `self.name: str = name`
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
let Expr::Attribute(attribute) = target.as_ref() else {
continue;
};
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
continue;
};
if id == "self" && !slots.contains(attribute.attr.as_str()) {
assignments.push(AttributeAssignment {
name: &attribute.attr,
range: attribute.range(),
});
}
}
// Ex) `self.name += name`
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
let Expr::Attribute(attribute) = target.as_ref() else {
continue;
};
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
continue;
};
if id == "self" && !slots.contains(attribute.attr.as_str()) {
assignments.push(AttributeAssignment {
name: &attribute.attr,
range: attribute.range(),
});
}
}
_ => {}
}
}
}
}
assignments
}
/// Return an iterator over the attributes enumerated in the given `__slots__` value.
fn slots_attributes(expr: &Expr) -> impl Iterator<Item = &str> {
// Ex) `__slots__ = ("name",)`
let elts_iter = match expr {
Expr::Tuple(ast::ExprTuple { elts, .. })
| Expr::List(ast::ExprList { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. }) => Some(elts.iter().filter_map(|elt| match elt {
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => Some(value.to_str()),
_ => None,
})),
_ => None,
};
// Ex) `__slots__ = {"name": ...}`
let keys_iter = match expr {
Expr::Dict(ast::ExprDict { keys, .. }) => Some(keys.iter().filter_map(|key| match key {
Some(Expr::StringLiteral(ast::ExprStringLiteral { value, .. })) => Some(value.to_str()),
_ => None,
})),
_ => None,
};
elts_iter
.into_iter()
.flatten()
.chain(keys_iter.into_iter().flatten())
}