Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect indentation for functions with end paren on a separate line. #126

Closed
deiden26 opened this issue May 10, 2019 · 15 comments · Fixed by #127 or #129
Closed

Incorrect indentation for functions with end paren on a separate line. #126

deiden26 opened this issue May 10, 2019 · 15 comments · Fixed by #127 or #129

Comments

@deiden26
Copy link

Before auto indentation

def do_thing(
    argument_1,
    argument_2
):

After auto indentation

def do_thing(
        argument_1,
        argument_2
):

There is no reason for the extra indentation if the argument list's closing parenthesis is on its own line.

@blueyed
Copy link
Member

blueyed commented May 11, 2019

Thanks for the report. See #127.

Not sure if this won't break other things - at least it was testing for this behavior previously.

JFI: it is only looking upward, so it does not matter if the closing paren is on the first level / not indented - but it will indent like you expect it to.

@deiden26
Copy link
Author

Thanks

@deiden26
Copy link
Author

Just a heads up, this change results in the following auto indentation

def thing(
    stuff):
    return stuff

Which should be

def thing(
        stuff):
    return stuff

per https://www.python.org/dev/peps/pep-0008/#indentation

# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

This is why I mentioned the end parenthesis specifically. In the case where the ending parenthesis is on the same line as the last argument, the double indentation is correct.

@blueyed
Copy link
Member

blueyed commented May 13, 2019

I see.
However, then we cannot really fix this - at least while typing (without the closing parenthesis being there already), no?
It would indent with 8 spaces, and adding the closing one on a new line would not re-indent the line above - you would have to indent it manually again.

@blueyed blueyed reopened this May 13, 2019
@deiden26
Copy link
Author

It would indent with 8 spaces, and adding the closing one on a new line would not re-indent the line above - you would have to indent it manually again.

That would be satisfactory to me. Even if the indentation doesn't work as I type, it would be nice if when I select a block like this and auto-indent it

def do_thing(
        stuff
):

I'd get this

def do_thing(
    stuff
):

By the way, I really appreciate you taking the time to look into this and converse with me.

blueyed added a commit to blueyed/vim-python-pep8-indent that referenced this issue May 13, 2019
@blueyed
Copy link
Member

blueyed commented May 13, 2019

👍
I think it can even be done while typing (when pressing ":" (via indentkeys). See #129.

@blueyed
Copy link
Member

blueyed commented May 13, 2019

With :set indentkeys+=*<Return> it would even handle

def func(
        bar_)

to

def func(
    bar
)

(with the cursor at _ and pressing <CR>)
But I do not think it is worth adding this by default.

@blueyed
Copy link
Member

blueyed commented May 13, 2019

Let me know how it works for you, happy to follow up more on this.. :)

@deiden26
Copy link
Author

This is beautiful. Thanks again for being so responsive.

@blueyed
Copy link
Member

blueyed commented May 14, 2019

You're welcome - thanks for the detailed report in the first place.. :)

Initially I've thought that it should look for the matching closing parenthesis via searchpairpos also (forward, stopping at the next line (or at least fast) then), which might be more robust after all.
I guess currently it might handle trailing whitespace not good enough for example etc.

Let me know any findings, and we can have more tests / fixes here.. ;)

@blueyed
Copy link
Member

blueyed commented May 26, 2019

Hope it's working well still.. :)

Just found that this also should be handled for type annotations in a similar way / just for reference: #130

@zachliu
Copy link

zachliu commented Jul 20, 2019

PEP8 indentation style is funny.

def do_thing(
        argument_1,
        argument_2
):

But I still think the above looks a bit better than

def do_thing(
    argument_1,
    argument_2
):

or

def do_thing(
        argument_1,
        argument_2):

😄 😄 😄

@blueyed
Copy link
Member

blueyed commented Jul 20, 2019

So you prefer 1 over 2?

IIRC this was changed to use 2 instead of 1 here.

@zachliu
Copy link

zachliu commented Jul 20, 2019

Yeah, I know, it's just my preference. I use

def do_thing(
        argument_1,
        argument_2
):

in function/class definitions, and use

do_thing(
    argument_1,
    argument_2
)

when I'm calling them.

@jonathf
Copy link

jonathf commented Jan 21, 2020

There doesn't seem to be a consensus on this topic. The pylint team seams to be against this:
pylint-dev/pylint#1651
I don't care what it should be, but I don't like that my linter and my editor disagreeing.

I don't know if this goes against the spirit of this plugin, but would it be possible to parameterize this feature using a global flag? Like let g:python_pep8_double_indentation = 1, such that we can have our cake and eat it too?

I would be happy to make the PR, if the maintainers don't mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants