File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed
resources/test/fixtures/flake8_return Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff 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
142150def 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+
241250def global_assignment ():
242251 global X
243252 X = 1
244253 return X
245254
255+
246256def nonlocal_assignment ():
247257 X = 1
258+
248259 def inner ():
249260 nonlocal X
250261 X = 1
Original file line number Diff line number Diff 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) ;
You can’t perform that action at this time.
0 commit comments