Skip to main content

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 considers the following indentation styles wrong:

def some_function(arg1,
                  *,
                  arg2,
                  arg3):
    print(arg1)
def some_function(argument1,
                  argument2,
                  argument3, argument4, argumen5):
    print(argument1)

This following style above is the style choice of the black formatter. Both this plugin and PEP8 consider it wrong because arguments and function names would be difficult to visually distinghish.

def some_function(
    arg1: int,
    arg2: list,
    arg3: bool = None,
):
    print(arg1)

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.

Notes

  • 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

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.

Specifically, the following style is allowed by PEP8 but this plugin still consider it wrong, because it could lead to messy code diff when refactoring:

- def some_very_very_very_long_func(arg1,
+ def some_very_very_very_very_very_long_func(arg1,
                                    arg2,
                                    arg3,

):
    return None

Interaction with other style checkers and formatters

Metadata

Release files for flake8-indent-in-def 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-indent-in-def 0.1.2
File Size Uploaded
flake8_indent_in_def-0.1.2.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-indent-in-def 0.1.2
File Interpreter ABI Platform
flake8_indent_in_def-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.1 kB

Release files / flake8_indent_in_def-0.1.2.tar.gz

Download URL flake8_indent_in_def-0.1.2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
da9745797813554dabca5ae3bae1606aaf85659f49fadc62bb6e0908dcac7a17
BLAKE2b-256 checksum
How to use checksums
92c121a4135778e4b6f09323ced1fb062a1672c7968e4b66b1c07a6945aa0cbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / flake8_indent_in_def-0.1.2-py2.py3-none-any.whl

Download URL flake8_indent_in_def-0.1.2-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ef56852e377f333d84d9d926287583f174e7f2eb757658e304af1fbfb2df6cb9
BLAKE2b-256 checksum
How to use checksums
3bae59121434f00e74b09e37a2e83275775cd082a5df6fd5979cf15969b77f29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page