Pylint: False positive "No space allowed around keyword argument assignment" when using non-trivial annotations

Created on 6 Jun 2017  路  7Comments  路  Source: PyCQA/pylint

Steps to reproduce

  1. $ cat pylint_inconsistent_whitespace_issues.py
"""What do you want from me, Pylint?"""

import pathlib

def take_path(arg: pathlib.Path = None):
    print(arg)

def take_str(arg: str=None):
    print(arg)
  1. $ python3.6 -m pylint --docstring-min-length 5 pylint_inconsistent_whitespace_issues.py
No config file found, using default configuration
************* Module pylint_inconsistent_whitespace_issues
C:  5, 0: No space allowed around keyword argument assignment
def take_path(arg: pathlib.Path = None):
                                ^ (bad-whitespace)
C:  8, 0: Exactly one space required around keyword argument assignment
def take_str(arg: str=None):
                     ^ (bad-whitespace)

------------------------------------------------------------------
Your code has been rated at 6.00/10

Current behavior

When using annotated keyword argument assignments sometimes I get No space allowed around keyword argument assignment and sometimes Exactly one space required around keyword argument assignment, which is inconsistent.

Expected behavior

Consistency needed. First error is false positive. Such false positives occur with many non-trivial annotations. So far I haven't found any non-trivial annotation for which it doesn't occur.

pylint --version output

No config file found, using default configuration
pylint 1.7.1, 
astroid 1.5.3
Python 3.6.0 (default, Dec 27 2016, 19:22:03) 
[GCC 5.4.0 20160609]

Most helpful comment

yep, that commit just needs to be cherry picked into the 1.7 branch.

All 7 comments

Ugh... This is most probably duplicate of #1429

Or maybe it isn't, because:

C: 12, 0: No space allowed around keyword argument assignment
def take_tuple(arg: Tuple[int, pathlib.Path] = None):
                                             ^ (bad-whitespace)

This appears to have changed. I raised this as a possible issue in #1507 and after a little digging came came to the conclusion that pylint was correct and closed the ticket however perhaps this is an edge case which I just wasn't hitting for some reason.

TL/DR

PEP-8 actually does specifically mention the use of spaces around default parameters.

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).

Yes:

def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

No:

def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...

For more information see this comment.

The issue is still present in:

No config file found, using default configuration
pylint 1.7.2, 
astroid 1.5.3
Python 3.6.1 (default, May 26 2017, 19:35:58) 
[GCC 5.4.0 20160609]

We're hitting this as well. pylint is in the wrong. The spaces should be allowed. The relevant code is in https://github.com/PyCQA/pylint/blob/master/pylint/checkers/format.py#L617 _has_valid_type_annotation().

yep, that commit just needs to be cherry picked into the 1.7 branch.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PCManticore picture PCManticore  路  3Comments

DGalt picture DGalt  路  3Comments

DevynCJohnson picture DevynCJohnson  路  3Comments

mrginglymus picture mrginglymus  路  3Comments

ethanchewy picture ethanchewy  路  3Comments