-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
When implementing __new__, you usually create an instance of the class in which the __new__ is defined. pylint currently does not allow setting protected and private attributes inside __new__.
Steps to reproduce
Lint the follwing code
# pylint: disable=blacklisted-name, missing-docstring
class MyObj:
def __new__(cls, foo, bar):
obj = object.__new__(cls)
obj._foo = foo # <-- warning reported here
obj.__bar = bar # <-- warning reported here
return obj
@property
def foo(self):
return self._foo
@property
def bar(self):
return self.__bar
def main():
instance = MyObj(1, 2)
print(instance.foo)
print(instance.bar)
main()Current behavior
This currently returns the following:
************* Module example
W: 7, 8: Access to a protected member _foo of a client class (protected-access)
W: 8, 8: Access to a protected member __bar of a client class (protected-access)
------------------------------------------------------------------
Your code has been rated at 8.67/10 (previous run: 7.69/10, +0.97)
Expected behavior
No warning should be reported.
This may be controversial, but I wonder if it would make sense to silence the warnings in this case. just like they are inside of __init__. While __new__ is used really rarely, it does not strike me as wrong to access private/protected members in there unless I am missing something. Would that really be a bad practice?
pylint --version output
pylint 1.8.1,
astroid 1.6.0
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
This is also somewhat related to #1801 as I noticed this error with that enum, but it turns out that the warning is also emitted for normal objects.