Skip to content

Commit 605c606

Browse files
Ignore property assignments in RET504 (#1520)
1 parent 92c2981 commit 605c606

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

resources/test/fixtures/flake8_return/RET504.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ def x():
137137
return a
138138

139139

140+
# Considered OK, since attribute assignments can have side effects.
141+
class X:
142+
def x(self):
143+
a = self.property
144+
self.property = None
145+
return a
146+
147+
140148
# Test cases for using value for assignment then returning it
141149
# See:https://github.com/afonasev/flake8-return/issues/47
142150
def resolve_from_url(self, url: str) -> dict:
@@ -238,13 +246,16 @@ def close(self):
238246
report(traceback.format_exc())
239247
return any_failed
240248

249+
241250
def global_assignment():
242251
global X
243252
X = 1
244253
return X
245254

255+
246256
def nonlocal_assignment():
247257
X = 1
258+
248259
def inner():
249260
nonlocal X
250261
X = 1

src/flake8_return/visitor.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ impl<'a> ReturnVisitor<'a> {
3838
.push(expr.location);
3939
return;
4040
}
41+
ExprKind::Attribute { .. } => {
42+
// Attribute assignments are often side-effects (e.g., `self.property = value`),
43+
// so we conservatively treat them as references to every known
44+
// variable.
45+
for name in self.stack.assigns.keys() {
46+
self.stack
47+
.refs
48+
.entry(name)
49+
.or_insert_with(Vec::new)
50+
.push(expr.location);
51+
}
52+
}
4153
_ => {}
4254
}
4355
visitor::walk_expr(self, expr);

0 commit comments

Comments
 (0)