7

How to disable "No space allowed around keyword argument assignment" in Pylint?

I found why it checks for the spaces (PEP 8, why no spaces around '=' in keyword argument or a default parameter value?), but I don't agree because this means I need to spend hours of solving only this message.

I looked for the message so I can disable it in the rcfile here: http://pylint-messages.wikidot.com/all-codes But the message does not appear in the pylint documentation?!

Community
  • 1
  • 1
blpasd
  • 409
  • 7
  • 18
  • PS It used to be that pylint worked on message codes, but newer versions allow you to use the name of the error which is much more user friendly (IMHO) and that name is normally (always?) part of the output from the error. – Jonah Graham Nov 23 '15 at 17:22
  • I agree! Not always however :) – blpasd Nov 23 '15 at 18:04

2 Answers2

6

Disabling in Pylint

This can be disabled in Pylint with bad-whitespace:

$ cat a.py
myfunca(myargb = 3)

$ pylint a.py --reports=n
No config file found, using default configuration
************* Module a
C:  1, 0: No space allowed around keyword argument assignment
myfunca(myargb = 3)
               ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
E:  1, 0: Undefined variable 'myfunca' (undefined-variable)

$ pylint a.py  --disable bad-whitespace --reports=n
No config file found, using default configuration
************* Module a
C:  1, 0: Missing module docstring (missing-docstring)
E:  1, 0: Undefined variable 'myfunca' (undefined-variable)

Disabling in PEP8 Checker

For completeness, you can disable the same for pep8's checker with E251:

$ pep8 a.py 
a.py:1:15: E251 unexpected spaces around keyword / parameter equals
a.py:1:17: E251 unexpected spaces around keyword / parameter equals

$ pep8 a.py --ignore=E251

Update - Info on suppressing just that message

AFAIK you can only disable messages down to the granularity of IDs in pylint, as all the whitespace has the same id bad-whitespace aka C0326 you can therefore either ignore all or none.

This is the code that checks if a message is disabled, as you see it only receives the id to check against:

def is_message_enabled(self, msg_descr, line=None, confidence=None):
    """return true if the message associated to the given message id is
    enabled

    msgid may be either a numeric or symbolic message id.
    """

When the message is added to the lint results, you can see that bad-whitespace is all that is passed in. The around and keyword argument assignment are simply arguments to the message.

warnings = []
if not any(good_space) and policies[0] == policies[1]:
    warnings.append((policies[0], 'around'))
else:
    for ok, policy, position in zip(good_space, policies, ('before', 'after')):
        if not ok:
            warnings.append((policy, position))
for policy, position in warnings:
    construct = _name_construct(token)
    count, state = _policy_string(policy)
    self.add_message('bad-whitespace', line=token[2][0],
                     args=(count, state, position, construct,
                           _underline_token(token)))

The arguments to the message are displayed using the formatting string:

'C0326': ('%s space %s %s %s\n%s',
          'bad-whitespace',
          ('Used when a wrong number of spaces is used around an operator, '
           'bracket or block opener.'),

All that should help if you want to customize pylint to do what you want, and if so, a pull-request back to them would hopefully be well received.

Jonah Graham
  • 7,890
  • 23
  • 55
  • I hope you only use this option on legacy code and not new code :-) – Jonah Graham Nov 23 '15 at 17:23
  • 1
    disable=E251 does not work. disable=bad-whitespace works too well. For example, I only want pylint to ignore whitespace around keyword assignments in function definitions: fun(a = b) should be ok... Now it also ignores a=b, which I really don't like. – blpasd Nov 23 '15 at 17:49
  • @romusters I have provided you the solution, fortunately pylint is written in python so you should find it easy to modify with the pointers I have given in the above answer. You could always spin your own version of pylint which commented out or similarly disabled the more fine grain messages you wanted to suppress. – Jonah Graham Nov 23 '15 at 19:06
  • @romusters "disable=E251 does not work" -- E251 is the message id for pep8 tool https://pypi.python.org/pypi/pep8 not pylint. I have updated the answer to make that clearer – Jonah Graham Nov 23 '15 at 19:09
  • 1
    Thank you very much for this information. I will spend some time on it and hopefully make an addition to Pylint. – blpasd Nov 26 '15 at 16:22
  • @blpasd: The appropriate way to change PyLint to ignore just the particular whitespace you want would be to extend the `no-space-check` option in the configuration file. – cjs Mar 02 '18 at 02:47
0

One way in which we can ignore a specific pylint check is to include the following line at the top of your python code -

# pylint: disable=C0326
# pylint: disable=C0303
... your code goes here

pylint then ignores the mentioned warnings as well as ignores these checks while calculating the pylint score.

For the above code, it will ignore both Trailing whitespaces (C0303) and Space before and after brackets, operators, comma (C0326)

Amit Pathak
  • 1,145
  • 11
  • 26