Why does pycharm "reformat code" unconditionally strip spaces/indentation from otherwise blank lines?

This is a huge annoyance from 1.0 and this still hasn't been fixed in 1.5, which is bizarre; this surely must annoy the hell out of a lot of other pycharm users than me.

The issue:

It is often nice to have blank lines within a block of code, like a function or a class, both as placeholders as you are coding in the moment, and also to space out complicated segments of code. By blank lines, I mean a line containing enough whitespace to match the scope of the lines neighboring it. Yet the reformat code feature unconditionally strips this indentation out on blank lines, leaving bizarre jagged lines of indentation. The zigzagging indentation in the middle of a class is not only irritating, but also prevent you from copy+pasting your code into an interactive session, as I'm sure many other users do.

I had heard previously that release 1.5 was to include improvements to the formatting functionality, and we should hold tight until that release cleaned up the code style menu, which nonetheless is still a mess of java specific settings and options leftover from the idea codebase. 

The "wrapping and braces" option under settings still has almost entirely greyed options which would only apply to java. Why are they still there?

A heading labeled "Keep when reformatting:" mocks us with entirely greyed out java specific options, yet no choice to preserve indentation when reformatting code.

Can we ever expect a fix on this, or should I figure out how to set-up some external code formatter? On that note, does anyone have any suggestions for nice customizable python code formatters/beautifiers?

5 comments

Hello peter,

The possibility to preserve trailing whitespace is currently not supported

by the formatter core in the IntelliJ Platform. This can be added, but I

don't have any definite time frame at the moment.

Also please don't forget that PyCharm supports formatting not only Python

but also JavaScript code. The options not applicable for Python are in fact

applicable for JavaScript, and are enabled when you select the JavaScript

tab in the preview.

This is a huge annoyance from 1.0 and this still hasn't been fixed in

1.5, which is bizarre; this surely must annoy the hell out of a lot of

other pycharm users than me.

The issue:

It is often nice to have blank lines within a block of code, like a

function or a class, both as placeholders as you are coding in the

moment, and also to space out complicated segments of code. By blank

lines, I mean a line containing enough whitespace to match the scope

of the lines neighboring it. Yet the reformat code feature

unconditionally strips this indentation out on blank lines, leaving

bizarre jagged lines of indentation. The zigzagging indentation in the

middle of a class is not only irritating, but also prevent you from

copy+pasting your code into an interactive session, as I'm sure many

other users do.

I had heard previously that release 1.5 was to include improvements to

the formatting functionality, and we should hold tight until that

release cleaned up the code style menu, which nonetheless is still a

mess of java specific settings and options leftover from the idea

codebase.

The "wrapping and braces" option under settings still has almost

entirely greyed options which would only apply to java. Why are they

still there?

A heading labeled "Keep when reformatting:" mocks us with entirely

greyed out java specific options, yet no choice to preserve

indentation when reformatting code.

Can we ever expect a fix on this, or should I figure out how to set-up

some external code formatter? On that note, does anyone have any

suggestions for nice customizable python code formatters/beautifiers?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Just to balance with the other side of the argument, the current behavior is what I expect as a default.  Stripping all trailing whitespace is a standard way to reduce the amount of noise in our line-and-character oriented diff tools (as with Python, you don't want to turn on the diff option that says "ignore whitespace.")  Many coding standards or version control hooks will enforce this.

While you make some good points, a language-aware IDE like pycharm should be able to put your cursor in the right space in a block whether or not there are spaces on a blank line.  Copy/pasting code into an interactive session, at least with the default python interface, is always such a pain that I rarely do it, and prefer alternatives like using "import" from the console, or using "execute selection in console" built-in from the IDE.  The fact that "execute selection in console" is behaving poorly for me at the moment is the bug I'd be concerned with instead.

0

Thanks for the timely response Dmitry.

So what I'm hearing from you are the following

  • The formatter core is incapable of NOT wiping out indentation on blank lines
  • There are currently no plans for adding this functionality,
    • even if there were plans, theres no telling when this would actually be implemented (I can tell you for free right now, that the answer is going to be sometime after my license expires)
    • The UI for wrapping and braces being dominated by javascript specific options is intentional.

Fair enough; the functionality isn't there yet. I'd very much like it to be there though. Please let me know what I have to do to get the ball rolling on that.

To me this is an incredibly important usability issue; if I can't depend on pycharm to format my code for me without forcing me to spend all my time reindenting individual lines, I probably won't be renewing my license.

Kevin:

Thanks for making it clear what your preferences are with respect to copy/pasting into an interactive session vs GUI context menus within the IDE.

Nonetheless, my workflow differs from yours, and I'm sure I'm not alone. In the course of working on some python code, I'll often want to test snippets of code as I'm writing them. As you point out, the "execute selection in console" option exists (although it doesn't behave nicely), but even if it did I would infinitely prefer to copy+paste the code into a terminal with an interactive python session.

You mention that since this is such a pain to do, you've given up and tried alternatives; what this says to me is that you've been bit by the same annoyance! You should be arguing on my side :-). The import module workaround you suggest is not at all the same thing as copy+pasting a code snippet, so I don't see that as a viable workaround either.

I also find it funny that you believe the current behaviour to be expected as default. I'm guessing you are not primarily a python programmer. Stripping trailing whitespace might be a common practice from the C and Java world where whitespace is insignificant, but it is definitely NOT a standard in any python IDE in existence (except for pycharm). You say so right in your post that you don't want to set your diff tools to ignore whitespace when feeding them python code, so I'm not sure how you came to the seemingly contradictory conclusion that treating whitespace on blank lines as insignificant is standard operating procedure for python.

To sum up:

The interactive interpreter treats whitespace (even whitespace on lines with no code!) as significant.

Why doesn't pycharm?

0
You mention that since this is such a pain to do, you've given up and tried alternatives; what this says to me is that you've been bit by the same annoyance!

Probably!  But I feel like that's because the normal python console is fundamentally broken for multiline input; even if the lines to copy had the whitespace needed, there's still no way to say "re-run that block", and since readline treats all the lines as independent, trying to re-run a chunk of code changing just the second line but not the first or third is a major pain.  And if you're copy/pasting from a block like this:

def foo(a):

    # v- cut from here -v

    for i in range(6):

        a = a + i

    print a

    # ^- to here -^

Then you either have to strip off a consistent amount of whitespace from the left or make a container for it...  the line-oriented interface just isn't made for this, the problem you bring up is only one of many.  pycharm has a console that doesn't need to be line-oriented, and it should Just Work.

I do consider Python my primary language these days, but you're right, I did come from C.  To Twisted, which has "no trailing whitespace" in their coding standard.  And Tim Peters' classic reindent.py strips trailing whitespace.  So it's not unprecedented, by any means.

0

I'm not talking about being able to execute just one line from an indented block. That doesn't even make sense.

I don't know why everyone is having so much difficulty understanding what I am saying. Maybe I am not making myself clear enough.

I have code like so:

def foo():

    pass

    #NOTICE THE INDENTATION ON THIS LINE!!

    pass

    return True

You copy and paste that into a python interpreter just fine, in both interactive and non-interactive modes. Everyone with me so far?

You hit "format code" in pycharm and suddenly your code only works in non-interactive mode. You can't paste it into an interactive session because obviously the indentation is all fucked up. The non-interactive interpreter is forgiving in this case, but that doesn't mean that pycharm should just go ahead and assume that no one will ever use an interactive session as they develop.

This is a problem.

This is not a matter of interactive python sessions being broken on multiline input- the documentation is quite clear that whenever the interpreter expects a continuation (with ... instead of >>>), you give it the expected indent.

I'm not the only one who's annoyed by this unexpected and bizarre behaviour, yet I still haven't heard a single reasonable argument in favor it.

"Because it makes it the code more readable to command line diff tools, and Java and C do it" is off base for reasons already explained.

"Twisted says no trailing whitespace" is also a profoundly underwhelming argument. First of all, Twisted is about as unpythonic and java-like as you can get for a popular python project- its the last place you should be looking for advice on making your IDE pythonic or following python coding standards. Secondly, this isn't a matter of trimming trailing whitespace per se; I would be fine and dandy trimming away all the whitespace on lines which have code. It's not surprising that twisted doesn't make a distinction on that, given how java-like it is.

I reiterate: Trimming whitespace on otherwise blank lines breaks compatibility with interactive mode.

This is a problem. Please fix it as soon as possible.

0

Please sign in to leave a comment.