You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently attribute-defined-outside-init will be emitted for all attributes. I think the warning would be more helpful if it was disallowed on "private" variables.
It is my understanding that this warning is to stop situations like this:
var = MyClass() # Creates a class with __init__.
print(var.messages) # The messages attribute is defined outside of __init__, this is invalid.
var.do_something() # "messages" is defined here in do_something.
print(var.messages) # Now this is valid.
Which I generally agree with.
However, for private variables, prefixed with an underscore for example, I find this less helpful. A class may find it helpful to define things outside of init to manage its internal state. And since they are clearly private, they should not be used like the example above.
Desired solution
I am aware you can use a # pylint: disable=attribute-defined-outside-init to silence these. But those can become difficult to maintain.
Would it be possible to add a .pylintrc option to only emit this warning on variables that are not prefixed with an underscore?
Additional context
No response
The text was updated successfully, but these errors were encountered:
Closing as won't do because this would subvert the spirit of the check, I don't see how the variable being private change the fact that the class is harder to read and understand and would be in a wrong/unexpected state post-initialization if the function defining the private variable isn't called properly (from the class itself or from outside).
Current problem
Currently attribute-defined-outside-init will be emitted for all attributes. I think the warning would be more helpful if it was disallowed on "private" variables.
It is my understanding that this warning is to stop situations like this:
Which I generally agree with.
However, for private variables, prefixed with an underscore for example, I find this less helpful. A class may find it helpful to define things outside of init to manage its internal state. And since they are clearly private, they should not be used like the example above.
Desired solution
I am aware you can use a
# pylint: disable=attribute-defined-outside-init
to silence these. But those can become difficult to maintain.Would it be possible to add a
.pylintrc
option to only emit this warning on variables that are not prefixed with an underscore?Additional context
No response
The text was updated successfully, but these errors were encountered: