From 6a3011eb4e7cc9baa3376c4fb1423012587513f7 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 18 Oct 2022 09:30:19 -0300 Subject: [PATCH] bad-dunder-name recognizes properties --- pylint/checkers/dunder_methods.py | 13 +++++++++- tests/functional/a/arguments_differ.py | 4 +-- tests/functional/a/arguments_differ.txt | 26 +++++++++---------- tests/functional/b/bad_dunder_name.py | 3 ++- tests/functional/b/bad_dunder_name.txt | 8 +++--- .../functional/n/no/no_self_argument_py37.py | 2 +- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/pylint/checkers/dunder_methods.py b/pylint/checkers/dunder_methods.py index 65f10670321..17e1b23bdd0 100644 --- a/pylint/checkers/dunder_methods.py +++ b/pylint/checkers/dunder_methods.py @@ -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. @@ -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", diff --git a/tests/functional/a/arguments_differ.py b/tests/functional/a/arguments_differ.py index 2e4776aa927..392dd08055f 100644 --- a/tests/functional/a/arguments_differ.py +++ b/tests/functional/a/arguments_differ.py @@ -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: diff --git a/tests/functional/a/arguments_differ.txt b/tests/functional/a/arguments_differ.txt index 54a75e84e48..6f2acc160b7 100644 --- a/tests/functional/a/arguments_differ.txt +++ b/tests/functional/a/arguments_differ.txt @@ -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 diff --git a/tests/functional/b/bad_dunder_name.py b/tests/functional/b/bad_dunder_name.py index 7e408bcd783..3d6095564d9 100644 --- a/tests/functional/b/bad_dunder_name.py +++ b/tests/functional/b/bad_dunder_name.py @@ -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") @@ -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" diff --git a/tests/functional/b/bad_dunder_name.txt b/tests/functional/b/bad_dunder_name.txt index 459502c92d6..2a4dc2ae0d2 100644 --- a/tests/functional/b/bad_dunder_name.txt +++ b/tests/functional/b/bad_dunder_name.txt @@ -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 diff --git a/tests/functional/n/no/no_self_argument_py37.py b/tests/functional/n/no/no_self_argument_py37.py index c1b195f0781..6381fb4e60f 100644 --- a/tests/functional/n/no/no_self_argument_py37.py +++ b/tests/functional/n/no/no_self_argument_py37.py @@ -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: