Pylint: Inconsistent whitespace demands wrt annotated and non-annotated keyword arguments

Created on 30 Jan 2018  路  4Comments  路  Source: PyCQA/pylint

pylint 1.8.2 and current git master both have inconsistent demands wrt annotated and non-annotated keyword arguments. For annotated ones, exactly one whitespace is required, and for non-annotated ones, no space is allowed. I think the treatment of annotated ones is incorrect, it should not allow spaces either (but hey, as long as it's consistent...). pylintrc in all below invocations is the unmodified one from pylint master.

Steps to reproduce

$ cat t.py

```python
def case1(some: int=0, thing=1):
pass

def case2(some: int = 0, thing=1):
pass

def case3(some: int=0, thing = 1):
pass

def case4(some: int = 0, thing = 1):
pass

```shell
$ bin/pylint t.py

Current behavior

Using config file .../pylintrc
************* Module t
C:  3,20: Exactly one space required around keyword argument assignment
def case1(some: int=0, thing=1):
                   ^ (bad-whitespace)
C:  9,20: Exactly one space required around keyword argument assignment
def case3(some: int=0, thing = 1):
                   ^ (bad-whitespace)
C:  9,30: No space allowed around keyword argument assignment
def case3(some: int=0, thing = 1):
                             ^ (bad-whitespace)
C: 12,32: No space allowed around keyword argument assignment
def case4(some: int = 0, thing = 1):
                               ^ (bad-whitespace)

Expected behavior

case2 and case3 should always cause warnings, because of the whitespace. Additionally, exactly one of case1 or case4 should cause a warning (not both; and my personal preference would be to warn about case4).

pylint --version output

(Current git master)

$ bin/pylint --version
Using config file .../pylintrc
pylint 2.0.0
astroid 1.6.1
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0]

Most helpful comment

Actually PEP484 doesn't say anything about the spaces as far as I can see (just uses the default values in some examples), but PEP8 does: https://www.python.org/dev/peps/pep-0008/#other-recommendations

When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).

Wow, I do still disagree with this recommendation (quite unexpected and inconsistent), but agree that pylint is correct in following the standard, so closing here.

All 4 comments

It looks like this is intentional by the writer of this particular checker. (see: https://github.com/PyCQA/pylint/blob/master/pylint/checkers/format.py#L657 from https://github.com/PyCQA/pylint/pull/1229)

To be honest I think having context aware bad-whitespace would make the checker more complex for not a lot of benefit, because I think it's better to just annotate all your function parameters at once if you are ready to get started with type annotation.

CCing @petrpulc (he who wrote the annotate checker) & @shlomme (he who refactored the bad-whitespace checker)

@scop

For annotated ones, exactly one whitespace is required, and for non-annotated ones, no space is allowed.

This is exactly how it's supposed to be. That is what current Python coding standards recommend, and IMHO Pylint should be following standards.

Take a look at https://pypi.python.org/pypi/pycodestyle - it will (or at least should) give you same results.

I 100% agree with @brycepg with regard to annotating.

And def case4(some: int = 0, thing: t.Any = 1): while having import typing as t might be one way to go if you're not sure what to put as type of thing.

Yes, this behavior is intended and follows PEP484: https://www.python.org/dev/peps/pep-0484/

And if you don't need to create a type hint for any of the arguments, just follow PEP8 and omit spaces around equal sign. The case2 definition is therefore correct and produces no warning...

Actually PEP484 doesn't say anything about the spaces as far as I can see (just uses the default values in some examples), but PEP8 does: https://www.python.org/dev/peps/pep-0008/#other-recommendations

When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).

Wow, I do still disagree with this recommendation (quite unexpected and inconsistent), but agree that pylint is correct in following the standard, so closing here.

Was this page helpful?
0 / 5 - 0 ratings