PyCQA / PyCQA/pycodestyle

E226: pycodestyle incorrectly warns about missing whitespace around operators

Open
#658 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.2k
Forks
754
PR merge metrics
No merged PRs in 30d

Description

Given the following example.py,

my_list = [1, 2, 3]
a = 2+2
b = my_list[1:1+1]

pycodestyle incorrectly warns about the missing whitespace for the + operator inside the list slice:

» pycodestyle --select E226 ./example.py
./example.py:2:6: E226 missing whitespace around arithmetic operator  # OK
./example.py:3:16: E226 missing whitespace around arithmetic operator  # incorrect

The first warning (line 2) is correct (line 2 should read a = 2 + 2); the second warning (line 3) is incorrect according to PEP-8:

Yes:

ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

No:

ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]

Edit: okay, I'm not so sure I've got it 100%. The style I think is PEP-8 doesn't really seem to get listed anywhere, but even if I add spaces around the : (which is listed), I still get lints.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the example.py snippet and pycodestyle --select E226. Start by locating pycodestyle's E226 implementation and checking how it handles operators inside list slices. Done means ordinary unspaced arithmetic such as 2+2 still warns, while the reported + inside a slice does not.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.