bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnulib-tool.py: Fix pylint 'attribute-defined-outside-init' warnings


From: Collin Funk
Subject: Re: gnulib-tool.py: Fix pylint 'attribute-defined-outside-init' warnings.
Date: Wed, 17 Apr 2024 16:28:58 -0700
User-agent: Mozilla Thunderbird

Hi Bruno,

On 4/17/24 3:18 PM, Bruno Haible wrote:
> In this example, the property 'attribute' is used outside of the class,
> that is, it is public. (In the OO world, the concept of "class" and of
> "private"/"public" are tightly related, because one of the main purposes
> of a class is to hide implementation details.)

True, but Python is different than the rest of the OO world because it
does not directly support "private"/"public". :(

There are ways to sort of emulate it but a single underscore is
simpler and gets the point across, as the "prior knowledge
summarization engine" suggests.

> Therefore, can you please see if after renaming message to _message the
> warning in GLError persists? If yes, please report a bug to the tool
> that produced this warning. The rationale why the warning is bogus is:
>   - The '_message' attribute is clearly private.
>   - Only one method uses it.
>   - There is no other convention for state that is used by one method only.

A single underscore doesn't silence it. I can submit a feature request
but it isn't really a bug since it is still an attribute outside of
__init__. IIRC pylint is pretty opinionated and they have many open
issues so who knows if it will even be addressed.

I'm not opposed to defining instance variables outside of __init__,
but I think that it should be more clear they are private. I'm happy
with the solution of a single underscore for private variables. The
functions I added in GLEmiter '_eliminate_NMD', etc. follow a similar
idea.

As far as silencing the warnings go, you can use a comment like so [1]:

   self.message = '[Errno %d] %s' % (errno, message)  # pylint: 
disable=attribute-defined-outside-init

but I can see that quickly becoming annoying if we make more use of
private variables. Since it would have to be placed in every block or
line one is defined.

How about we just silence this warning? I've attached a patch that
should work. I think the '_' prefix for private instance
variables/functions is a standard convention, but maybe we should add
a note in main.py?

Collin

[1] 
https://pylint.readthedocs.io/en/stable/user_guide/messages/message_control.html#block-disables

Attachment: 0001-gnulib-tool.py-Ignore-attribute-defined-outside-init.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]