Skip to content

Commit 58c711f

Browse files
committed
Misc fixes
- Update README - Rename PLU100 -> PLU001 - Fix bug causing two blocks of blank lines with a comment between them to count as a single block - Rename test case files - Properly exclude test case files from linting - Remove unused config files - And more
1 parent a7d6c43 commit 58c711f

21 files changed

+206
-112
lines changed

.flake8

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[flake8]
22
max-line-length=88
33
ignore=
4-
E203, # whitespace before ‘:’
5-
W503, # line break before binary operator
6-
ANN101, # missing type annotation for self in method
7-
ANN102, # missing type annotation for cls in classmethod
8-
E741, # allow variable names I, O and l (but I and O still prevented by pylint)
9-
ANN401, # allow using the Any type hint
10-
E501, # Similar to Pylint line-too-long but Pylint can ignore string literals
4+
E203, # Whitespace before ‘:’
5+
W503, # Line break before binary operator
6+
ANN101, # Missing type annotation for self in method
7+
ANN102, # Missing type annotation for cls in classmethod
8+
E741, # Allow variable names I, O and l (but I and O still prevented by pylint)
9+
ANN401, # Allow using the Any type hint
10+
E501, # Too long lines, handled by Pylint
1111
exclude=
1212
.git,
1313
__pycache__,

.markdownlint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
},
1010
"MD013": {
1111
"line_length": 88
12-
}
12+
},
13+
"MD014": false
1314
}

.pydocstyle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add-ignore=
99
D301 # Use r""" if any backslashes in a docstring
1010

1111
match = .*\.py
12+
match-dir = ^(?!(case_files|build))[^\.].*

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
11
# Flake8-plus
2+
3+
Flake8-plus is a plugin for [Flake8](https://github.com/PyCQA/flake8) that detects
4+
incorrect amounts of vertical whitespace before the first toplevel `import` statement.
5+
By default, the plugin issues a warning if there are blank lines immediately preceding
6+
the first toplevel `import`. The plugin can be configured to expect any number of blank
7+
lines.
8+
9+
## Installation
10+
11+
Flake8-plus can be installed from PyPI using `pip`:
12+
13+
```shell
14+
$ pip install flake8-plus
15+
```
16+
17+
You can verify that it has been installed as follows (the version numbers you see may
18+
vary):
19+
20+
```shell
21+
$ flake8 --version
22+
5.0.4 (flake8-plus: 0.1.0, mccabe: 0.7.0, pycodestyle: 2.9.1, pyflakes: 2.5.0)
23+
```
24+
25+
## Configuration
26+
27+
You can set the required number of blank lines before the first `import`. This can be
28+
done from the command line:
29+
30+
```shell
31+
$ flake8 --blanks-before-imports 1
32+
```
33+
34+
Or from one of the `setup.cfg`, `tox.ini`, or `.flake8` files:
35+
36+
```ini
37+
[flake8]
38+
blanks-before-imports=1
39+
```
40+
41+
## Why no blank lines?
42+
43+
Neither Black, Flake8 nor Pylint enforces a specific number of blank lines preceding the
44+
first `import` and consequently there seems to be no consensus or standard. The table
45+
below shows the frequency of the number of blank lines before the first toplevel
46+
`import` statement in the code bases for [Black](https://github.com/psf/black),
47+
[Flake8](https://github.com/PyCQA/flake8) and [Pylint](https://github.com/PyCQA/pylint)
48+
(as of October 2022).
49+
50+
| Package | Total files | 0 blanks | 1 blank | 2 blanks | Folder |
51+
| ------- | ----------: | -------: | ------: | -------: | ------------- |
52+
| Black | 33 | 21 | 12 | 0 | `src` |
53+
| Flake8 | 32 | 32 | 0 | 0 | `src/flake8/` |
54+
| Pylint | 177 | 3 | 170 | 4 | `pylint` |
55+
56+
Clearly, there is no real consensus. Black seems undecided, Flake8 consistently uses 0
57+
blanks, and Pylint seems to prefer 1 blank line. However, it's worth noting that the
58+
Pylint code does not consistently include module docstrings (thereby breaking
59+
`pylint(missing-module-docstring)`). For that reason, and also because this is a Flake8
60+
plugin, the plugin follows the style of Flake8 as the default.
61+
62+
## Reported problems
63+
64+
| Code |  Description |
65+
| ------ | ------------------------------------------------------- |
66+
| PLU001 | "expected {} blank lines before first import, found {}" |

cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// language - current active spelling language
66
"language": "en",
77
// words - list of words to be always considered correct
8-
"words": ["docstring"],
8+
"words": ["docstring", "docstrings", "toplevel", "Pylint"],
99
// flagWords - list of words to be always considered incorrect
1010
// This is useful for offensive words and common spelling errors.
1111
// For example "hte" should be "the"

flake8_plus/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import defaults
1010
from .config import Config
1111
from .version import VERSION
12-
from .visitors.plu100_visitor import PLU100Visitor
12+
from .visitors.plu001_visitor import PLU001Visitor
1313

1414

1515
class Plugin:
@@ -18,7 +18,7 @@ class Plugin:
1818
name = "flake8-plus"
1919
version = VERSION
2020
visitors = [
21-
PLU100Visitor,
21+
PLU001Visitor,
2222
]
2323

2424
def __init__(self, tree: ast.AST, lines: list[str]):
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
"""The flake8-plus visitor."""
1+
"""Exception classes raised by various operations within pylint."""
22
import ast
33
from typing import Any
44

55
from ..config import Config
66
from ..problem import Problem
77

88

9-
class PLU100Visitor(ast.NodeVisitor):
10-
"""Visitor class for the PLU100 rule."""
9+
class PLU001Visitor(ast.NodeVisitor):
10+
"""Visitor class for the PLU001 rule."""
1111

1212
def __init__(self, lines: list[str], config: Config):
1313
"""
14-
Initialize a PLU100Visitor instance.
14+
Initialize a PLU001Visitor instance.
1515
1616
Args:
1717
lines (list[str]): The physical lines.
@@ -43,7 +43,7 @@ def _process_node(self, node: ast.AST):
4343
actual = self._compute_blank_before(node.lineno)
4444
if actual != self.config.blanks_before_imports:
4545
message = _build_message(actual, self.config.blanks_before_imports)
46-
problem = Problem(node.lineno, node.col_offset, "PLU100", message)
46+
problem = Problem(node.lineno, node.col_offset, "PLU001", message)
4747
self.problems.append(problem)
4848
elif hasattr(node, "end_lineno") and (node.end_lineno is not None):
4949
self._last_end = node.end_lineno
@@ -52,9 +52,13 @@ def _compute_blank_before(self, line_number: int) -> int:
5252
if line_number <= (self._last_end + 1):
5353
return 0
5454

55-
line_numbers = range(self._last_end + 1, line_number)
56-
blanks = [l for l in line_numbers if not self._lines[l - 1].strip()]
57-
return len(blanks)
55+
indices_reversed = reversed(range(self._last_end, line_number - 1))
56+
n_blanks = 0
57+
for index in indices_reversed:
58+
if self._lines[index].strip():
59+
break
60+
n_blanks += 1
61+
return n_blanks
5862

5963

6064
def _build_message(blanks_actual: int, blanks_expected: int) -> str:

pyproject_.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def _validate_version(tag: Optional[str], version: str) -> bool:
104104
entry_points={
105105
"flake8.extension": [
106106
"PLU = flake8_plus:Plugin",
107-
# "PLU = flake8_plus:some_func",
108107
],
109108
},
110109
)

setup_.cfg

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)