diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 8d885176b8..4a6ff2f9e0 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -305,3 +305,5 @@ contributors: * Agustin Toledo: contributor * Nicholas Smith: contributor + +* Andrzej Klajnert: contributor diff --git a/ChangeLog b/ChangeLog index 69d517ea26..2076131af3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -158,6 +158,10 @@ Release date: TBA Close #2963 +* Added ``__post_init__`` to ``defining-attr-methods`` in order to avoid ``attribute-defined-outside-init`` in dataclasses. + + Close #2581 + What's New in Pylint 2.3.0? =========================== diff --git a/examples/pylintrc b/examples/pylintrc index c2a1558ce7..2e35e3b89d 100644 --- a/examples/pylintrc +++ b/examples/pylintrc @@ -460,7 +460,8 @@ redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io # List of method names used to declare (i.e. assign) instance attributes. defining-attr-methods=__init__, __new__, - setUp + setUp, + __post_init__ # List of member names, which should be excluded from the protected access # warning. diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index a62521aba1..8b1da11d6e 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -666,7 +666,7 @@ class ClassChecker(BaseChecker): ( "defining-attr-methods", { - "default": ("__init__", "__new__", "setUp"), + "default": ("__init__", "__new__", "setUp", "__post_init__"), "type": "csv", "metavar": "", "help": "List of method names used to declare (i.e. assign) \ diff --git a/pylintrc b/pylintrc index 3475fb7673..dbd5265526 100644 --- a/pylintrc +++ b/pylintrc @@ -334,7 +334,7 @@ max-public-methods=25 [CLASSES] # List of method names used to declare (i.e. assign) instance attributes. -defining-attr-methods=__init__,__new__,setUp +defining-attr-methods=__init__,__new__,setUp,__post_init__ # List of valid names for the first argument in a class method. valid-classmethod-first-arg=cls diff --git a/tests/functional/attribute_defined_outside_init.py b/tests/functional/attribute_defined_outside_init.py index ff31e955db..1cb319c617 100644 --- a/tests/functional/attribute_defined_outside_init.py +++ b/tests/functional/attribute_defined_outside_init.py @@ -78,3 +78,7 @@ def prop(self): @prop.setter def prop(self, value): self.__prop = value + +class DataClass: + def __post_init__(self): + self.a = 42