Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to ignore attribute-defined-outside-init on "private" attributes. #9555

Closed
collinfunk opened this issue Apr 18, 2024 · 2 comments

Comments

@collinfunk
Copy link

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:

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

@collinfunk collinfunk added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 18, 2024
@DanielNoord
Copy link
Collaborator

This seems pretty opinionated and not something I would expect to checker to do. Personally I'm -1 on this proposal.

@Pierre-Sassoulas Pierre-Sassoulas added Won't fix/not planned Needs decision 🔒 Needs a decision before implemention or rejection and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling Needs decision 🔒 Needs a decision before implemention or rejection labels Apr 22, 2024
@Pierre-Sassoulas
Copy link
Member

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).

@Pierre-Sassoulas Pierre-Sassoulas closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants