Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce closing bracket lint rules in python

We're using flake8 and pylint to do our code style checks on a project.

But the problem is, none of those do any checks for how lines are split. Since consistency is nice in a project it looks really weird that we find

foo = long_function_name(var_one, var_two,
                         var_three, var_four)

foo = long_function_name(
    var_one,
    var_two,
    var_three,
    var_four)

foo = long_function_name(
    var_one,
    var_two,
    var_three,
    var_four
)

foo = {
    a,
    b}

foo = {
    a,
    b,
}

in our codebase. Sometimes different ways of closing a thing is right next to each other like in the example above.

Now is there a checker or a rule for pylint or flake or special checker that would just ensure:

  • if you have a hanging indent, that the closing bracket, curly brackets or parentheses are always in a new line, and that we have a trailing comma above them.

Consistency is key in a clean codebase, and we can't rely on devs to stick to rules if those aren't automatically checked. So I need a checker for the cases mentioned.

like image 875
zidarsk8 Avatar asked Mar 17 '18 14:03

zidarsk8


1 Answers

You can use wemake-python-styleguide. It is a set of flake8 plugins with lots of new rules.

To install it run: pip install wemake-python-styleguide

Running: flake8 your_module.py

This is still just a flake8 plugin!

The specific rule you are interested in is consistency.ParametersIndentationViolation.

Correct code samples:

# Correct:
def my_function(arg1, arg2, arg3) -> None:
    return None

print(1, 2, 3, 4, 5, 6)

def my_function(
    arg1, arg2, arg3,
) -> None:
    return None

print(
    1, 2, 3, 4, 5, 6,
)

def my_function(
    arg1,
    arg2,
    arg3,
) -> None:
    return None

print(
    first_variable,
    2,
    third_value,
    4,
    5,
    last_item,
)

# Special case:

print('some text', 'description', [
    first_variable,
    second_variable,
    third_variable,
    last_item,
], end='')

Incorrect code samples:

call(1,
     2,
     3)

print(
    1, 2,
    3,
)

Docs: https://wemake-python-stylegui.de

like image 124
sobolevn Avatar answered Nov 03 '22 05:11

sobolevn