Code Monkey home page Code Monkey logo

flake8-indent-in-def's Introduction

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

flake8-indent-in-def's People

Contributors

cyyc1 avatar

Watchers

 avatar

Forkers

jsh9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.