Jatin Kumar
Jatin Kumar

Reputation: 2785

Disable pylint execution on a section of code or function

I am using pylint 0.27 with python 2.7.3. Pylint has a known bug which hits when it analyses code having a .next() call. As given in http://www.logilab.org/122793 link, it fails with the given traceback.

I cannot change my python and pylint versions but I would like to workaround this problem by disabling pylint on the piece of code that has .next() call by adding a #pylint: MAGIC comment in my code.

I could find support for disabling pylint checking on a file using #pylint: skip-file but I am interested at doing this at function level or rather at line level.

Any other workarounds are also welcomed!

Upvotes: 19

Views: 17041

Answers (4)

Alnitak
Alnitak

Reputation: 2489

To disable pylint entirely for a function without needing to enumerate each pylint violation code, specify all to the disable, eg:

def foo():
    # pylint: disable=all
    """You put the disable right under the function signature 
    to disable pylint for the entire function scope.
    """
    pass

Upvotes: 3

Leo
Leo

Reputation: 3116

When I refactor code, I wrap the portion I have yet to touch in a class ToDo and start that class with a directive to ignore all messages. At the end of the class the directive goes out of scope. MWE:

def unused_variable(foo=''):
    "generate W0613"

class ToDo:
    #pylint: disable = E, W, R, C
    #disables W0613 and E0213

    def unused_variable(foo=''):
        "generate W0613 and E0213"

def main(foo=''):
    "generate W0613"

Upvotes: 2

Garrett Kadillak
Garrett Kadillak

Reputation: 1074

You can accomplish this by adding the pylint comment as the first line of the function body like so:

def wont_raise_pylint():
  # pylint: disable=W0212
  some_module._protected_member()
  some_module._protected_member()

def will_still_raise_pylint():
  some_module._protected_member()

Upvotes: 24

sthenault
sthenault

Reputation: 15125

Unfortunatly you won't be able to locally avoid the error you encounter.

One could expect to locate in the source code the offending piece of code, and then locally disable the involved message(s), but this won't work because it will still be run. This is because when locally disabling a message, the code detecting this message is run, only the message isn't be emitted in the end.

However, it may work if you globally disable this message (depending on the way it's implemented). In your case it seems unfortunatly that you would have to skip the whole 'logging' checker.

To sum up, to avoid your traceback you may either:

  • locally use pylint: skip-file, in which case every pylint's features will be activated but the whole offending file will be skipped

  • globally disable the 'logging' checker (--disable=logging), in which case the whole code (including the offending file) will be checked but without the 'logging' checker messages

Upvotes: 1

Related Questions