A flake8 plugin that enforces 8-space indentation in function/class definitions
Project description
flake8-indent-in-def
This is a flake8 plugin enforces 8-space indentation in function/class definitions in Python code.
Installation
pip install flake8-indent-in-def
Violation codes
There is one violation code that this plugin reports:
Code | Description |
---|---|
IND101 | hanging indentation in function definition must be 8 spaces |
IND102 | if the 1st argument is on the same line as the function name, all other arguments must be on the same line |
IND201 | hanging indentation in class definition must be 8 spaces |
IND202 | if the 1st base class is on the same line as the class name, all other base classes must be on the same line |
Style examples
Wrong
This plugin, as well as PEP8, considers the following indentation styles wrong:
def some_function(argument1,
argument2,
argument3, argument4, argumen5):
print(argument1)
def some_function(arg1,
arg2,
arg3):
print(arg1)
def some_function(
arg1: int,
arg2: list,
arg3: bool = None,
):
print(arg1)
Note: this style above is the style choice of the black
formatter. This style is wrong because arguments and function names would be difficult to visually distinghish.
Correct
Correspondingly, here are the correct indentation styles:
def some_function(
arg1: int,
arg2: list,
arg3: bool = None,
):
print(arg1)
def some_function(
arg1: int, arg2: list, arg3: bool = None
) -> None:
print(arg1)
def some_function(arg1: int, arg2: list, arg3: bool = None) -> None:
print(arg1)
def some_function(
arg1: int, arg2: list,
arg3: bool = None, arg4: float = 2.0,
) -> None:
print(arg1)
Additionally, this plugin by default enforces the same indentation styles on class inheritence:
class MyClass(
BaseClassA,
BaseClassB,
BaseClassC,
):
def __init__(self):
pass
You can opt out of class inheritence checks by ignoring rules IND201
and IND202
.
Rationale
When we only indent by 4 spaces in function definitions, it is difficult to visually distinguish function arguments with the function name and the function body. This reduces readability.
It is similar for base classes in class definitions, but it's less of an issue than function definitions.
Interaction with other style checkers and formatters
black
-formatted code will cause a style violation here, becauseblack
authors explicitly opted for the 4-space indentation and do not plan to change it- The style enforced in this plugin contradicts with rule WPS318 enforced by wemake-python-styleguide ("WPS")
- But WPS is configurable so you can always opt out of WPS318
- This plugin does not check trailing commas, because flake8-commas already does it
- This plugin does not forbid grouping arguments (see example below), because WPS317 can enforce it
def some_func(
arg1, arg2, arg3,
arg4, arg5,
):
pass
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flake8_indent_in_def-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a379ad32cee3e77b116a6ccdce415926ccd7877e328a54be0123e6a71264af81 |
|
MD5 | ff31208201b6b435ef5211ced271a36f |
|
BLAKE2b-256 | 92c2da9de35767b0128c54065b0f19b0b47fb2396533b779add2828c713abc78 |
Hashes for flake8_indent_in_def-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6566c6e1331b54c0a224041089203d55968a4e856e87786b80906700d744220b |
|
MD5 | ebe87bbdc6a6c7571c480d718ae98325 |
|
BLAKE2b-256 | d5651cdc9552b323d47c15d45135299231ee86e6d8a147b29215f899e23dd831 |