PyCQA / PyCQA/pycodestyle

E306: False positive when preceded by 2 blank lines and a comment

Open
#1,011 0 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

$ pycodestyle --version
2.7.0

If you have two blank lines, a comment, and then a nested function, E306 is raised erroneously.
(Since this false positive requires a genuine violation of E303, I don't think it's a big deal. But I noticed it, so I'm reporting it.)

The following functions both trigger false postives for E306.

def bad():
    _ = None


    # arbitrary comment

    def inner():  # E306 not expected (but happening! 1/2)
        pass
def bad():
    _ = None


    # arbitrary comment
    def inner():  # E306 not expected (but happening! 2/2)
        pass

For context, this snippet contains the false positives as well as cases that don't trigger E306:

def bad():
    _ = None


    # arbitrary comment

    def inner():  # E306 not expected (but happening! 1/2)
        pass


def bad():
    _ = None


    # arbitrary comment
    def inner():  # E306 not expected (but happening! 2/2)
        pass


def good():
    _ = None
    # arbitrary comment


    def inner():  # E306 not expected
        pass


def good():
    _ = None

    # arbitrary comment
    def inner():  # E306 not expected
        pass


def good():
    _ = None

    # arbitrary comment

    def inner():  # E306 not expected
        pass

If you dump that to a file (e.g., bug.py) and run pycodestyle on it, you'll get E306 inside the two bad() functions:

$ pycodestyle --select=E306 --show-source bug.py
bug.py:7:5: E306 expected 1 blank line before a nested definition, found 0
    def inner():  # E306 not expected (but happening! 1/2)
    ^
bug.py:16:5: E306 expected 1 blank line before a nested definition, found 0
    def inner():  # E306 not expected (but happening! 2/2)
    ^

Let me know if there's anything I can clarify/provide, thanks!

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

Save the supplied examples as bug.py and run pycodestyle --select=E306 --show-source bug.py to reproduce the two false positives. Read the E306 checker entry point in the project's single Python file, then verify the nested definitions preceded by comments and blank lines no longer report E306 while the existing cases retain their expected behavior.

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
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.