Pylint: Questionable "bad-whitespace" for function annotations with default values

Created on 7 May 2014  路  7Comments  路  Source: PyCQA/pylint

Originally reported by: Antony Lee (BitBucket: anntzer, GitHub: @anntzer?)


Both http://legacy.python.org/dev/peps/pep-3107/ and https://docs.python.org/3/tutorial/controlflow.html#function-annotations put spaces around the = for default values when an argument has an annotation:

#!python
def f(x: int = 1): pass
# compare with
def f(x: int=1): pass

I also think that the second option looks weird as I tend to parse it as x: (int=1), not (x: int) = 1. Well, perhaps the syntax could have been better but that's what we have now.

But Pylint currently triggers a bad-whitespace warning for the first option.


bug

All 7 comments

_Original comment by_ Antony Lee (BitBucket: anntzer, GitHub: @anntzer?):


By the way, the official PEP8 has been updated to mandate the version with spaces.

+1

https://www.python.org/dev/peps/pep-0008/#other-recommendations
Updated PEP8 recommends whitespace around equal sign in this situation.

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

Adressed in #1229

Thanks @petrpulc

Hello again.
It works but is far from perfect as I see now...
Take for example:

import datetime
def foo(date: datetime.date = None)

which will now still result in bad-whitespace.

While the "regular" type hints work now (yet are not checked against imported names), this is virtually uncheckable. Should I resign and add the period as an ignored token; or would it be even better to not check the contents of the annotation at all?

@petrpulc There is already a PR for that: https://github.com/PyCQA/pylint/pull/1430.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DevynCJohnson picture DevynCJohnson  路  3Comments

thanatos picture thanatos  路  3Comments

pylint-bot picture pylint-bot  路  3Comments

glmdgrielson picture glmdgrielson  路  3Comments

adamtheturtle picture adamtheturtle  路  3Comments