Skip to content

Commit

Permalink
bad-dunder-name recognizes properties
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 18, 2022
1 parent 59eede6 commit 6a3011e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
13 changes: 12 additions & 1 deletion pylint/checkers/dunder_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@
"__reduce_ex__",
]

DUNDER_PROPERTIES: list = [
"__class__",
"__dict__",
"__doc__",
"__format__",
"__module__",
"__sizeof__",
"__subclasshook__",
"__weakref__",
]


class DunderChecker(BaseChecker):
"""Checks related to dunder methods.
Expand Down Expand Up @@ -206,7 +217,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
node.name.startswith("__")
and node.name.endswith("_")
and node.name not in self._dunder_methods
and node.name not in EXTRA_DUNDER_METHODS
and node.name not in EXTRA_DUNDER_METHODS + DUNDER_PROPERTIES
):
self.add_message(
"bad-dunder-name",
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/a/arguments_differ.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test that we are emitting arguments-differ when the arguments are different."""
# pylint: disable=missing-docstring, too-few-public-methods, unused-argument,useless-super-delegation, unused-private-member
# pylint: disable=bad-dunder-name
# pylint: disable=missing-docstring, too-few-public-methods, unused-argument,useless-super-delegation
# pylint: disable=unused-private-member, bad-dunder-name

class Parent:

Expand Down
26 changes: 13 additions & 13 deletions tests/functional/a/arguments_differ.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
arguments-differ:12:4:12:12:Child.test:Number of parameters was 1 in 'Parent.test' and is now 2 in overriding 'Child.test' method:UNDEFINED
arguments-differ:23:4:23:12:ChildDefaults.test:Number of parameters was 3 in 'ParentDefaults.test' and is now 2 in overriding 'ChildDefaults.test' method:UNDEFINED
arguments-differ:41:4:41:12:ClassmethodChild.func:Number of parameters was 2 in 'Classmethod.func' and is now 0 in overriding 'ClassmethodChild.func' method:UNDEFINED
arguments-differ:68:4:68:18:VarargsChild.has_kwargs:Variadics removed in overriding 'VarargsChild.has_kwargs' method:UNDEFINED
arguments-renamed:71:4:71:17:VarargsChild.no_kwargs:Parameter 'args' has been renamed to 'arg' in overriding 'VarargsChild.no_kwargs' method:UNDEFINED
arguments-differ:144:4:144:12:StaticmethodChild2.func:Number of parameters was 1 in 'Staticmethod.func' and is now 2 in overriding 'StaticmethodChild2.func' method:UNDEFINED
arguments-differ:180:4:180:12:SecondChangesArgs.test:Number of parameters was 2 in 'FirstHasArgs.test' and is now 4 in overriding 'SecondChangesArgs.test' method:UNDEFINED
arguments-differ:307:4:307:16:Foo.kwonly_1:Number of parameters was 4 in 'AbstractFoo.kwonly_1' and is now 3 in overriding 'Foo.kwonly_1' method:UNDEFINED
arguments-differ:310:4:310:16:Foo.kwonly_2:Number of parameters was 3 in 'AbstractFoo.kwonly_2' and is now 2 in overriding 'Foo.kwonly_2' method:UNDEFINED
arguments-differ:313:4:313:16:Foo.kwonly_3:Number of parameters was 3 in 'AbstractFoo.kwonly_3' and is now 3 in overriding 'Foo.kwonly_3' method:UNDEFINED
arguments-differ:316:4:316:16:Foo.kwonly_4:Number of parameters was 3 in 'AbstractFoo.kwonly_4' and is now 3 in overriding 'Foo.kwonly_4' method:UNDEFINED
arguments-differ:319:4:319:16:Foo.kwonly_5:Variadics removed in overriding 'Foo.kwonly_5' method:UNDEFINED
arguments-differ:359:4:359:14:ClassWithNewNonDefaultKeywordOnly.method:Number of parameters was 2 in 'AClass.method' and is now 3 in overriding 'ClassWithNewNonDefaultKeywordOnly.method' method:UNDEFINED
arguments-differ:13:4:13:12:Child.test:Number of parameters was 1 in 'Parent.test' and is now 2 in overriding 'Child.test' method:UNDEFINED
arguments-differ:24:4:24:12:ChildDefaults.test:Number of parameters was 3 in 'ParentDefaults.test' and is now 2 in overriding 'ChildDefaults.test' method:UNDEFINED
arguments-differ:42:4:42:12:ClassmethodChild.func:Number of parameters was 2 in 'Classmethod.func' and is now 0 in overriding 'ClassmethodChild.func' method:UNDEFINED
arguments-differ:69:4:69:18:VarargsChild.has_kwargs:Variadics removed in overriding 'VarargsChild.has_kwargs' method:UNDEFINED
arguments-renamed:72:4:72:17:VarargsChild.no_kwargs:Parameter 'args' has been renamed to 'arg' in overriding 'VarargsChild.no_kwargs' method:UNDEFINED
arguments-differ:145:4:145:12:StaticmethodChild2.func:Number of parameters was 1 in 'Staticmethod.func' and is now 2 in overriding 'StaticmethodChild2.func' method:UNDEFINED
arguments-differ:181:4:181:12:SecondChangesArgs.test:Number of parameters was 2 in 'FirstHasArgs.test' and is now 4 in overriding 'SecondChangesArgs.test' method:UNDEFINED
arguments-differ:308:4:308:16:Foo.kwonly_1:Number of parameters was 4 in 'AbstractFoo.kwonly_1' and is now 3 in overriding 'Foo.kwonly_1' method:UNDEFINED
arguments-differ:311:4:311:16:Foo.kwonly_2:Number of parameters was 3 in 'AbstractFoo.kwonly_2' and is now 2 in overriding 'Foo.kwonly_2' method:UNDEFINED
arguments-differ:314:4:314:16:Foo.kwonly_3:Number of parameters was 3 in 'AbstractFoo.kwonly_3' and is now 3 in overriding 'Foo.kwonly_3' method:UNDEFINED
arguments-differ:317:4:317:16:Foo.kwonly_4:Number of parameters was 3 in 'AbstractFoo.kwonly_4' and is now 3 in overriding 'Foo.kwonly_4' method:UNDEFINED
arguments-differ:320:4:320:16:Foo.kwonly_5:Variadics removed in overriding 'Foo.kwonly_5' method:UNDEFINED
arguments-differ:360:4:360:14:ClassWithNewNonDefaultKeywordOnly.method:Number of parameters was 2 in 'AClass.method' and is now 3 in overriding 'ClassWithNewNonDefaultKeywordOnly.method' method:UNDEFINED
3 changes: 2 additions & 1 deletion tests/functional/b/bad_dunder_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


class Apples:
__slots__ = ("a", "b")

def __hello__(self): # [bad-dunder-name]
# not one of the explicitly defined dunder name methods
print("hello")
Expand Down Expand Up @@ -33,7 +35,6 @@ def __inv__(self): # [bad-dunder-name]
# author likely meant to call the invert dunder method
pass

#todo: can I rule properties out?
@property
def __doc__(self):
return "Docstring"
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/b/bad_dunder_name.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bad-dunder-name:6:4:6:17:Apples.__hello__:Bad or misspelled dunder method name __hello__.:HIGH
bad-dunder-name:20:4:20:15:Apples.__init_:Bad or misspelled dunder method name __init_.:HIGH
bad-dunder-name:28:4:28:16:Apples.___neg__:Bad or misspelled dunder method name ___neg__.:HIGH
bad-dunder-name:32:4:32:15:Apples.__inv__:Bad or misspelled dunder method name __inv__.:HIGH
bad-dunder-name:8:4:8:17:Apples.__hello__:Bad or misspelled dunder method name __hello__.:HIGH
bad-dunder-name:22:4:22:15:Apples.__init_:Bad or misspelled dunder method name __init_.:HIGH
bad-dunder-name:30:4:30:16:Apples.___neg__:Bad or misspelled dunder method name ___neg__.:HIGH
bad-dunder-name:34:4:34:15:Apples.__inv__:Bad or misspelled dunder method name __inv__.:HIGH
2 changes: 1 addition & 1 deletion tests/functional/n/no/no_self_argument_py37.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test detection of self as argument of first method in Python 3.7 and above."""

# pylint: disable=missing-docstring,too-few-public-methods
# pylint: disable=missing-docstring,too-few-public-methods,bad-dunder-name


class Toto:
Expand Down

0 comments on commit 6a3011e

Please sign in to comment.