Flake8 - check for blank lines around statements
PEP 8 recommends to use blank lines only to separate logical sections:
Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).
Use blank lines in functions, sparingly, to indicate logical sections.
However, adding blank lines before and after compound statements (e.g. if/else block) as well as some simple
statements (e.g. return) might improve code readability which is otherwise hindered despite syntax highlighting
that modern code editors provide, as demonstrated in the following example:
if 1 == 1:
print(1)
for n in [2, 3]:
print(n)
else:
print(4)
...where it might not be immediately apparent that this is not one if/else statement but an if statement followed by
a for/else statement.
This Flake8 plugin therefore checks for a blank line before/after each statement as long as it's not the first/last line of code within a module and not the first/last statement within a compound statement.
Requirements
- Python >= 3.8.1
- Flake8 >= 3.8.0
Installation
Pip
pip install flake8-bas
Poetry
poetry add flake8-bas
Statements and their error codes
The statements are split into different categories based on whether they are
simple statements or
compound statements, and whether an error occurs between
two statements of the same type or not. This allows you to filter entire groups using BAS and the first digit,
e.g. BAS2.
Error types:
- Before Error - missing line before a statement as long as the preceding element is not a statement of the same type.
- After Error - missing line after a statement as long as the element that follows is not a statement of the same type.
- Sibling Error - missing line between two or more consecutive statements of the same type.
BAS1xx/BAS2xx/BAS3xx: Simple statements
Simple statements, excluding expressions and assignments, which are technically statements as well.
| Statement | Before Error | After Error | Sibling Error |
|---|---|---|---|
assert |
BAS101 | BAS201 | BAS301 |
break |
BAS102 | BAS202 | BAS302 |
continue |
BAS103 | BAS203 | BAS303 |
del |
BAS104 | BAS204 | BAS304 |
global |
BAS105 | BAS205 | BAS305 |
import |
BAS106 | BAS206 | BAS306 |
from import |
BAS107 | BAS207 | BAS307 |
nonlocal |
BAS108 | BAS208 | BAS308 |
pass |
BAS109 | BAS209 | BAS309 |
raise |
BAS110 | BAS210 | BAS310 |
return |
BAS111 | BAS211 | BAS311 |
yield |
BAS112 | BAS212 | BAS312 |
yield from |
BAS113 | BAS213 | BAS313 |
Note: Some of these errors shouldn't occur (e.g. return followed by another return) because having
consecutive siblings of those types does not make sense, but the plugin would raise these errors anyway.
BAS5xx/BAS6xx/BAS7xx: Compound statements
| Statement | Before Error | After Error | Sibling Error |
|---|---|---|---|
class |
BAS501 | BAS601 | BAS701 |
def |
BAS502 | BAS602 | BAS702 |
async def |
BAS503 | BAS603 | BAS703 |
for |
BAS504 | BAS604 | BAS704 |
async for |
BAS505 | BAS605 | BAS705 |
if |
BAS506 | BAS606 | BAS706 |
match |
BAS507 | BAS607 | BAS707 |
try |
BAS508 | BAS608 | BAS708 |
while |
BAS509 | BAS609 | BAS709 |
with |
BAS510 | BAS610 | BAS710 |
async with |
BAS511 | BAS611 | BAS711 |
Overlapping errors
The extension produces overlapping errors, that is two statements of different types following each other would produce one "before" error and one "after" error pointing to the same line of code:
a = 1
global a
del a
This would result in two errors for line 4:
./file.py:4:1: BAS205 missing blank line after "global" statement
./file.py:4:1: BAS104 missing blank line before "del" statement
However, two statements of the same type would produce only one "sibling" error.
Configuration
The plugin checks for blank lines around every statement. There are no custom configuration options. Instead, you could simply ignore some errors. This system has benefits as well as drawbacks.
The benefit is that you could take advantage of Flake8's ignore and per-file-ignores (flake8 >= 3.7.0) config
options and have a different behaviour applied to each set of files:
[flake8]
ignore = BAS3
per-file-ignores =
app/*: BAS10, BAS110, BAS20, BAS210
tests/*: BAS1, BAS2
The drawback is that there are no sane defaults and you would inevitably need to exclude some errors, either because they are undesirable, make little sense, or the same/conflicting checks might already be applied by another plugin (e.g. checks by flake8-import-order) or should be handled by other (formatting) tools (e.g. black).
Recommended exclusions
Only compound statements plus return and yield would raise errors:
[flake8]
ignore = BAS10, BAS110, BAS20, BAS210, BAS30, BAS310
All simple statements excluded
Only compound statements would raise errors:
[flake8]
ignore = BAS1, BAS2, BAS3
Metadata
Release files for flake8-bas 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flake8_bas-1.1.0.tar.gz | 7.8 kB | Without provenance | Details for flake8_bas-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flake8_bas-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for flake8_bas-1.1.0-py3-none-any.whl |
Total release size: 16.1 kB
Release files / flake8_bas-1.1.0.tar.gz
| Download URL | flake8_bas-1.1.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
29e25966cfab09d3741ebbc300cf421ea5eb8f488522f359b1c4a8b41521bdfa
|
|
BLAKE2b-256 checksum How to use checksums |
ff80d07792a4e54415ad5c413ede790beb2a4c2736122062d3e56aa943724348
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-44-generic
|
Release files / flake8_bas-1.1.0-py3-none-any.whl
| Download URL | flake8_bas-1.1.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
460b9cd1acdecb51fcbc719207eb0bc7ce6d7053aaed1ef2aaaf122207198228
|
|
BLAKE2b-256 checksum How to use checksums |
a1e9c9998a814b75621710be7dec0faf733bdae99a7b04f21527354c230500d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-44-generic
|
Security for flake8-bas 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.