Pycodestyle: Relax most extra-spacing errors when extra spacing is being used for horizontal alignment

Created on 7 Apr 2016  路  6Comments  路  Source: PyCQA/pycodestyle

When functions have lots and lots of keyword arguments, code is IMNSHO much more readable if you line up the equals signs, e.g.

    exits = get_exits(args.data_dir,
                      country_code = args.countrycode,
                      bad_exit     = args.badexit,
                      good_exit    = args.goodexit,
                      version      = args.version,
                      nickname     = args.nickname,
                      address      = args.address)

instead of

    exits = get_exits(args.data_dir,
                      country_code=args.countrycode,
                      bad_exit=args.badexit,
                      good_exit=args.goodexit,
                      version=args.version,
                      nickname=args.nickname,
                      address=args.address)

This also comes up when a function (often, but not always, __init__) needs to initialize a whole bunch of variables in a row:

def __init__(self, queue, circ_id, socks_port, socks_addr="127.0.0.1"):
    self._queue           = queue
    self._circ_id         = circ_id
    self._socks_addr      = socks_addr
    self._socks_port      = socks_port

    self._orig_queue      = None
    self._orig_circ_id    = None
    self._orig_proxy_addr = None
    self._orig_proxy_port = None
    self._orig_socket     = None

or in the middle of complex conditionals:

if address or nickname or version or requested:
    candidates = [
        desc for desc in candidates
        if ((not address   or address  in desc.address) and
            (not nickname  or nickname in desc.nickname) and
            (not version   or version  == str(desc.version)) and
            (not requested or desc.fingerprint in requested))
    ]

So I request the addition of the following heuristic: Do not issue any of the "extra spaces after/before X" errors if the first non-whitespace character after the extra spaces is aligned with a first non-whitespace character on either the preceding or following line. Additionally, disable the "there should be _no_ whitespace around equals signs in parameter lists" warning in this circumstance.

Most helpful comment

@zackw the easy way to handle this is to ignore that error code altogether.

All 6 comments

Yeah, this one kind of bugs me too :smile:

FWIW, if such a heuristic is implemented, I'd really like to be able to turn it off :wink:

From the last bullet of PEP-8: Pet Peeves, while not specifically addressing function keyword args, makes me lean towards this not being something to add...

Avoid extraneous whitespace in the following situations:
...

  • More than one space around an assignment (or other) operator to align it with another.

Well, whoever wrote _that_ was clearly out of their gourd. ;-)

(But seriously ... this being something about which reasonable people may disagree, is to me a strong argument for _including_ the heuristic as an option. That way people who do like lining up their equals signs can use the tool just as efficiently as those who don't.)

@zackw the easy way to handle this is to ignore that error code altogether.

I believe the consensus here is to ignore those specific codes with --ignore / --extend-ignore

Was this page helpful?
0 / 5 - 0 ratings