Discussion:
[Python-projects] disabling warning for one long line only (potential ppylint bug???)
Klaus Foerster
2010-07-18 17:31:25 UTC
Permalink
Hi,

I have a small example code snippet with two pylint warnings:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name



Now I would like to supress a pylint warning specifically for one line

The solution seems to be to add a trailing comment like for example
# pylint: disable=W0612

So my code would look like:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name



Now the next two problems arise.
- I get a pylint warning because my line is too long
- my line IS too long, and if I masked the line too long warning it
would become even longer.

The next thing I tried was to use a backslash to shorten my line and
move the pylint warning to the next line

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name \
# pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name



However this does not work as expected.
it's now the rest of the function, which will ignore pylint warnings,
but not my single line.


Is there a suggested idiom to ignore warnings for long lines ?


For my given example I found a working but not really elegant solution,
which is:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
( i_want_to_mask_this_warning # pylint: disable=W0612
) = value_with_long_name + value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name



perhaps one solution would be something like:

# pylint: disable_for_next_line=W0612
i_want_to_mask_this_warning = value_with_long_name + value_with_long_name


Thanks in advance for other suggestions / ideas?
Rasener, Dustin M
2010-07-21 13:16:37 UTC
Permalink
Try this:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
# pylint: disable=W0612
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
# pylint: enable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name

The comment to disable has to precede the offending line. Also, notice
the call to "enable". Once you tell pylint to disable a particular
message, it is disabled for the rest of the file, or until the enable
statement.

Hope this helps.

Dustin

-----Original Message-----
From: python-projects-bounces at lists.logilab.org
[mailto:python-projects-bounces at lists.logilab.org] On Behalf Of Klaus
Foerster
Sent: Sunday, July 18, 2010 1:31 PM
To: python-projects at lists.logilab.org
Subject: [Python-projects] disabling warning for one long line only
(potential ppylint bug???)

Hi,

I have a small example code snippet with two pylint warnings:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name



Now I would like to supress a pylint warning specifically for one line

The solution seems to be to add a trailing comment like for example
# pylint: disable=W0612

So my code would look like:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name



Now the next two problems arise.
- I get a pylint warning because my line is too long
- my line IS too long, and if I masked the line too long warning it
would become even longer.

The next thing I tried was to use a backslash to shorten my line and
move the pylint warning to the next line

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name \
# pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name



However this does not work as expected.
it's now the rest of the function, which will ignore pylint warnings,
but not my single line.


Is there a suggested idiom to ignore warnings for long lines ?


For my given example I found a working but not really elegant solution,
which is:

""" simple pylint example """
def afunc(value_with_long_name):
""" a doc string """
value_with_long_name = 3
( i_want_to_mask_this_warning # pylint: disable=W0612
) = value_with_long_name + value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name



perhaps one solution would be something like:

# pylint: disable_for_next_line=W0612
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name


Thanks in advance for other suggestions / ideas?
klausf
2010-07-21 15:44:55 UTC
Permalink
Hi Dustin,
Post by Klaus Foerster
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
# pylint: disable=W0612
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
# pylint: enable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name
Thanks for your answer.
Yes. I know, that this works, but it is a little bit clumsy I think.

Especially as pylint already supports disabling of warnings for only
one line (if the line is short enough to append a trailing pylint
directive)

The reason I want to use the disable statement is for example for
something like this:

timestamp,name,fsize,permission = get_next_entry()
dosomething_with(timestamp,name,permission)

I know, that fsize is not used, but considering the alternative this
is most comprehensible and everything else renders IMHO the code less
readable

entries = get_next_entry()
timestamp,name = entries[:2]
permission = entries[3]
dosomething_with(timestamp,name,permission)

Thus I'm really looking for something, which does not clutter the
source with one disable statement and one enable statement.
Post by Klaus Foerster
The comment to disable has to precede the offending line. Also, notice
the call to "enable". Once you tell pylint to disable a particular
message, it is disabled for the rest of the file, or until the enable
statement.
-----Original Message-----
From: python-projects-bounces at lists.logilab.org
[mailto:python-projects-bounces at lists.logilab.org] On Behalf Of Klaus
Foerster
Sent: Sunday, July 18, 2010 1:31 PM
To: python-projects at lists.logilab.org
Subject: [Python-projects] disabling warning for one long line only
(potential ppylint bug???)
Hi,
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name
Now I would like to supress a pylint warning specifically for one line
The solution seems to be to add a trailing comment like for example
# pylint: disable=W0612
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name
Now the next two problems arise.
- I get a pylint warning because my line is too long
- my line IS too long, and if I masked the line too long warning it
would become even longer.
The next thing I tried was to use a backslash to shorten my line and
move the pylint warning to the next line
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name \
# pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name
However this does not work as expected.
it's now the rest of the function, which will ignore pylint warnings,
but not my single line.
Is there a suggested idiom to ignore warnings for long lines ?
For my given example I found a working but not really elegant solution,
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
( i_want_to_mask_this_warning # pylint: disable=W0612
) = value_with_long_name + value_with_long_name
iwant_to_see_this_warning = 7
print value_with_long_name
# pylint: disable_for_next_line=W0612
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name
Thanks in advance for other suggestions / ideas?
_______________________________________________
Python-Projects mailing list
Python-Projects at lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects
Duncan Gibson
2010-07-21 18:33:22 UTC
Permalink
Post by Klaus Foerster
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name
[...but this then throws up a C0301: line too long message...]

surely Occam's Razor suggests that the simplest solution
is just to have multiple disables at the end of the line:

i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612,C0301

OK, so it's not elegant, but easier than rewriting pylint

D
Klaus Foerster
2010-07-21 19:38:20 UTC
Permalink
Hi Duncam
Post by Duncan Gibson
Post by Klaus Foerster
""" simple pylint example """
""" a doc string """
value_with_long_name = 3
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612
iwant_to_see_this_warning = 7
print value_with_long_name
[...but this then throws up a C0301: line too long message...]
surely Occam's Razor suggests that the simplest solution
i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612,C0301
I considered this already.
It works if I really create a long line. (not sure, whether my user
agent will break it or not.

i_want_to_mask_this_warning = value_with_long_name +
value_with_long_name # pylint: disable=W0612,C0301

What I wanted however is to have lines which are not too long.
They look ugly depending on the source code editor being used.
Not knowing who'll review my code I'd like to have code not
exceeding 80 characters per line in length

If I break the line with a backslash like:
i_want_to_mask_this_warning = value_with_long_name + \
value_with_long_name # pylint: disable=W0612,C0301

then the W0612 will NOT be ignored.


So the solutions I see are.
- the rather noisy #disable / #enable solution. reducing readbility
- leave a long line in the source code and disable two warnings
- rewrite the code in order to not have any warnings to mask
- rewrite the code such, that the line will be short enough even with
the pylint pragma
- accept, that more warnings will be ignored than realy intended (skip
the enable)


Is there anybody from Logilab listening who could
comment how difficult it would be to?

- ignore one line warnings for source code lines separated with a '\'
- introduce a new pragma, which will ignore the next statement

I love pylint (much better than pychecker), but still try to find ways
to NOT make my code less readble in order to satisfy pylint.

The danger in keeping (an visually ignoring) pylint warnings is, that one
might overlook a real warning in between all the false warning.


bye


Klaus
Sylvain Thénault
2010-07-26 06:45:45 UTC
Permalink
Hi all,
Post by Klaus Foerster
What I wanted however is to have lines which are not too long.
They look ugly depending on the source code editor being used.
Not knowing who'll review my code I'd like to have code not
exceeding 80 characters per line in length
i_want_to_mask_this_warning = value_with_long_name + \
value_with_long_name # pylint: disable=W0612,C0301
then the W0612 will NOT be ignored.
Is there anybody from Logilab listening who could
comment how difficult it would be to?
- ignore one line warnings for source code lines separated with a '\'
- introduce a new pragma, which will ignore the next statement
The pb is that multi-lines statements are somewhat tricky to detect,
since in most case that information get lost by the python parser.
Or is not easy to detect. Could you create a ticket on pylint's tracker
for your case ? IMO we should first try to fix message control directive
with multi-lines statement, and if we don't reach an acceptable solution,
then introduce a new directive or a similar alternative (which I would
like to avoid).
Post by Klaus Foerster
I love pylint (much better than pychecker), but still try to find ways
to NOT make my code less readble in order to satisfy pylint.
You're definitly right here.
--
Sylvain Th?nault LOGILAB, Paris (France)
Formations Python, Debian, M?th. Agiles: http://www.logilab.fr/formations
D?veloppement logiciel sur mesure: http://www.logilab.fr/services
CubicWeb, the semantic web framework: http://www.cubicweb.org
Emile Anclin
2010-07-22 07:53:27 UTC
Permalink
Especially as pylint already supports disabling of warnings for only ?
one line (if the line is short enough to append a trailing pylint ?
directive)
The reason I want to use the disable statement is for example for
timestamp,name,fsize,permission = get_next_entry()
dosomething_with(timestamp,name,permission)
I know, that fsize is not used, but considering the alternative this ?
is most comprehensible and everything else renders IMHO the code less ?
readable
If it is only about not using "fsize", you can call it _fsize, and it will
be considered as a dummy variable (everything starting with "_" or
with "dummy"; this dummy variable regex can be configured...).
--
Emile Anclin <emile.anclin at logilab.fr>
http://www.logilab.fr/ http://www.logilab.org/
Informatique scientifique & et gestion de connaissances
Klaus Foerster
2010-07-22 07:58:35 UTC
Permalink
Thanks a lot Emile,
Post by Emile Anclin
Post by klausf
Especially as pylint already supports disabling of warnings for only
one line (if the line is short enough to append a trailing pylint
directive)
The reason I want to use the disable statement is for example for
timestamp,name,fsize,permission = get_next_entry()
dosomething_with(timestamp,name,permission)
I know, that fsize is not used, but considering the alternative this
is most comprehensible and everything else renders IMHO the code less
readable
If it is only about not using "fsize", you can call it _fsize, and it will
be considered as a dummy variable (everything starting with "_" or
with "dummy"; this dummy variable regex can be configured...).
Yes this is true.

for most of my warnings this should work out well.

I still think it would be a good idea to be able to disable warnings for
a multi line statement, which was split with '\' .

For the most common cases in my source code though,
your solution is even better than explicit disabling.



bye

Klaus
Jean-Michel Pichavant
2010-07-23 07:55:30 UTC
Permalink
Post by Klaus Foerster
Thanks a lot Emile,
Post by Emile Anclin
Post by klausf
Especially as pylint already supports disabling of warnings for only
one line (if the line is short enough to append a trailing pylint
directive)
The reason I want to use the disable statement is for example for
timestamp,name,fsize,permission = get_next_entry()
dosomething_with(timestamp,name,permission)
I know, that fsize is not used, but considering the alternative this
is most comprehensible and everything else renders IMHO the code less
readable
If it is only about not using "fsize", you can call it _fsize, and it will
be considered as a dummy variable (everything starting with "_" or
with "dummy"; this dummy variable regex can be configured...).
Yes this is true.
for most of my warnings this should work out well.
I still think it would be a good idea to be able to disable warnings for
a multi line statement, which was split with '\' .
For the most common cases in my source code though,
your solution is even better than explicit disabling.
bye
Klaus
_______________________________________________
Python-Projects mailing list
Python-Projects at lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects
I've seen people writting

timestamp, name, _, permission = get_next_entry()


The drawback is that your reader loose the information that you're
expecting fsize as 3rd element. It may be worth it though.
You just have to make sure that '_' is identified as a dummy variable
in your pylint rules.

JM
Duncan Gibson
2010-07-23 08:46:14 UTC
Permalink
Post by Jean-Michel Pichavant
I've seen people writting
timestamp, name, _, permission = get_next_entry()
The drawback is that your reader loose the information that you're
expecting fsize as 3rd element. It may be worth it though.
You just have to make sure that '_' is identified as a dummy variable
in your pylint rules.
I've modified my .pylintrc file to have the following:

dummy-variables-rgx=(_|dummy|(unused.*))

which would allow me to have:

timestamp, name, dummy, permission = get_next_entry()

or the self-documenting:

timestamp, name, unused_fsize, permission = get_next_entry()

D.
Maarten ter Huurne
2010-07-23 13:30:28 UTC
Permalink
Post by Duncan Gibson
Post by Jean-Michel Pichavant
I've seen people writting
timestamp, name, _, permission = get_next_entry()
The drawback is that your reader loose the information that you're
expecting fsize as 3rd element. It may be worth it though.
You just have to make sure that '_' is identified as a dummy variable
in your pylint rules.
dummy-variables-rgx=(_|dummy|(unused.*))
timestamp, name, dummy, permission = get_next_entry()
timestamp, name, unused_fsize, permission = get_next_entry()
I have done something similar:

In pylintrc:
dummy-variables-rgx=[^_]*_

In the source code:
timestamp, name, fsize_, permission = get_next_entry()

It really helps code readability to give names to things you untuple, even
if you don't use all of them.

Bye,
Maarten
Emile Anclin
2010-07-26 08:33:49 UTC
Permalink
Post by Maarten ter Huurne
Post by Duncan Gibson
Post by Jean-Michel Pichavant
I've seen people writting
timestamp, name, _, permission = get_next_entry()
The drawback is that your reader loose the information that you're
expecting fsize as 3rd element. It may be worth it though.
You just have to make sure that '_' is identified as a dummy
variable in your pylint rules.
dummy-variables-rgx=(_|dummy|(unused.*))
timestamp, name, dummy, permission = get_next_entry()
timestamp, name, unused_fsize, permission = get_next_entry()
dummy-variables-rgx=[^_]*_
timestamp, name, fsize_, permission = get_next_entry()
It really helps code readability to give names to things you untuple,
even if you don't use all of them.
ok, I guess I should have mentionned what I had to check myself:

Pylint uses the dummy-variable-rgx with the "match" method, and this
method "match" *if and only if* the variable name starts with the regex;

example :

dummy-variables-rgx=_|dummy|unused

will match '_' ,'_hello', '_fsize', 'dummy_sth', 'unused_var', etc,

but not 'not_used', another_dummy', 'mydummyunusedvar' etc.

Maybe we should explain that in the pylintrc file more explicitly ...
--
Emile Anclin <emile.anclin at logilab.fr>
http://www.logilab.fr/ http://www.logilab.org/
Informatique scientifique & et gestion de connaissances
Loading...