$ 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)
$ python3.6 -m pylint --docstring-min-length 5 pylint_inconsistent_whitespace_issues.pyNo 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
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.
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.
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]
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().
although I believe https://github.com/PyCQA/pylint/commit/2561f539d60a3563d6507e7a22e226fb10b58210 should have fixed this?
yep, that commit just needs to be cherry picked into the 1.7 branch.
Most helpful comment
yep, that commit just needs to be cherry picked into the 1.7 branch.