Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore property assignments in RET504 #1520

Merged
merged 1 commit into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions resources/test/fixtures/flake8_return/RET504.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ def x():
return a


# Considered OK, since attribute assignments can have side effects.
class X:
def x(self):
a = self.property
self.property = None
return a


# Test cases for using value for assignment then returning it
# See:https://github.com/afonasev/flake8-return/issues/47
def resolve_from_url(self, url: str) -> dict:
Expand Down Expand Up @@ -238,13 +246,16 @@ def close(self):
report(traceback.format_exc())
return any_failed


def global_assignment():
global X
X = 1
return X


def nonlocal_assignment():
X = 1

def inner():
nonlocal X
X = 1
Expand Down
12 changes: 12 additions & 0 deletions src/flake8_return/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl<'a> ReturnVisitor<'a> {
.push(expr.location);
return;
}
ExprKind::Attribute { .. } => {
// Attribute assignments are often side-effects (e.g., `self.property = value`),
// so we conservatively treat them as references to every known
// variable.
for name in self.stack.assigns.keys() {
self.stack
.refs
.entry(name)
.or_insert_with(Vec::new)
.push(expr.location);
}
}
_ => {}
}
visitor::walk_expr(self, expr);
Expand Down