Skip to content

Commit

Permalink
Drop __get__ and __set__ from unnecessary-dunder-call (#9791)
Browse files Browse the repository at this point in the history
These are for descriptors which affects the behavior of the object _as a
property_; I do not think they should be called directly but there is no
alternative when working with the object directly.

Closes #9789
  • Loading branch information
zanieb committed Feb 5, 2024
1 parent b47f85e commit 84aea7f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def __getattribute__(self, item):
def do_thing(self, item):
return object.__getattribute__(self, item) # PLC2801

def use_descriptor(self, item):
item.__get__(self, type(self)) # OK
item.__set__(self, 1) # OK
item.__delete__(self) # OK


blah = lambda: {"a": 1}.__delitem__("a") # OK

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ fn allowed_dunder_constants(dunder_method: &str, target_version: PythonVersion)
| "__await__"
| "__class__"
| "__class_getitem__"
| "__delete__"
| "__dict__"
| "__doc__"
| "__exit__"
| "__get__"
| "__getnewargs__"
| "__getnewargs_ex__"
| "__getstate__"
Expand All @@ -185,6 +187,7 @@ fn allowed_dunder_constants(dunder_method: &str, target_version: PythonVersion)
| "__post_init__"
| "__reduce__"
| "__reduce_ex__"
| "__set__"
| "__set_name__"
| "__setstate__"
| "__sizeof__"
Expand Down Expand Up @@ -285,14 +288,12 @@ impl DunderReplacement {
"__deepcopy__" => Some(Self::MessageOnly("Use `copy.deepcopy()` function")),
"__del__" => Some(Self::MessageOnly("Use `del` statement")),
"__delattr__" => Some(Self::MessageOnly("Use `del` statement")),
"__delete__" => Some(Self::MessageOnly("Use `del` statement")),
"__delitem__" => Some(Self::MessageOnly("Use `del` statement")),
"__divmod__" => Some(Self::MessageOnly("Use `divmod()` builtin")),
"__format__" => Some(Self::MessageOnly(
"Use `format` builtin, format string method, or f-string",
)),
"__fspath__" => Some(Self::MessageOnly("Use `os.fspath` function")),
"__get__" => Some(Self::MessageOnly("Use `get` method")),
"__getattr__" => Some(Self::MessageOnly(
"Access attribute directly or use getattr built-in function",
)),
Expand All @@ -308,7 +309,6 @@ impl DunderReplacement {
"__pow__" => Some(Self::MessageOnly("Use ** operator or `pow()` builtin")),
"__rdivmod__" => Some(Self::MessageOnly("Use `divmod()` builtin")),
"__rpow__" => Some(Self::MessageOnly("Use ** operator or `pow()` builtin")),
"__set__" => Some(Self::MessageOnly("Use subscript assignment")),
"__setattr__" => Some(Self::MessageOnly(
"Mutate attribute directly or use setattr built-in function",
)),
Expand All @@ -331,8 +331,6 @@ fn allow_nested_expression(dunder_name: &str, semantic: &SemanticModel) -> bool
"__init__"
| "__del__"
| "__delattr__"
| "__set__"
| "__delete__"
| "__setitem__"
| "__delitem__"
| "__iadd__"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ unnecessary_dunder_call.py:31:16: PLC2801 Unnecessary dunder call to `__getattri
30 | def do_thing(self, item):
31 | return object.__getattribute__(self, item) # PLC2801
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLC2801
32 |
33 | def use_descriptor(self, item):
|
= help: Access attribute directly or use getattr built-in function

Expand Down

0 comments on commit 84aea7f

Please sign in to comment.