Avoid PLE0237 for property with setter (#11377)

## Summary

Should this consider the decorator only if the name is actually a
property or is the logic in this PR correct?

fixes: #11358

## Test Plan

Add test case.
This commit is contained in:
Dhruv Manilawala 2024-05-13 05:53:00 +05:30 committed by GitHub
parent d835b3e218
commit 0fc6cf9bee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 0 deletions

View file

@ -66,3 +66,19 @@ class StudentF(object):
def setup(self): def setup(self):
pass pass
# https://github.com/astral-sh/ruff/issues/11358
class Foo:
__slots__ = ("bar",)
def __init__(self):
self.qux = 2
@property
def qux(self):
return self.bar * 2
@qux.setter
def qux(self, value):
self.bar = value / 2

View file

@ -147,6 +147,28 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment> {
return vec![]; return vec![];
} }
// And, collect all the property name with setter.
for statement in body {
let Stmt::FunctionDef(ast::StmtFunctionDef { decorator_list, .. }) = statement else {
continue;
};
for decorator in decorator_list {
let Some(ast::ExprAttribute { value, attr, .. }) =
decorator.expression.as_attribute_expr()
else {
continue;
};
if attr == "setter" {
let Some(ast::ExprName { id, .. }) = value.as_name_expr() else {
continue;
};
slots.insert(id.as_str());
}
}
}
// Second, find any assignments that aren't included in `__slots__`. // Second, find any assignments that aren't included in `__slots__`.
let mut assignments = vec![]; let mut assignments = vec![];
for statement in body { for statement in body {