New submission from Terry J. Reedy <>: says "A 
logical line that contains only spaces, tabs, formfeeds and possibly a comment, 
is ignored" but notes that REPLs might not ignore them during interactive 
input.  The same is true of editors as lines are typed.  In particular, even a 
no-code comment line can suggest where a smart indenter should indent.  In any 
case, Python does not care what follows '#', and IDLE should not either (except 
when uncommenting-out lines). 

Suppose one types the following:

if 1:
    if 2:
    # x

Currently, IDLE indents 4 columns after '#' and '# x' and 8 columns after '#x'. 
If one moves the comments to the margin, the indents are 0 and 8 columns.  This 
is because pyparse.Parser._study2 ignores lines that match _junkre and _junkre 
only  matches comments with '#' followed by a non-space (/B) character.

I think that IDLE should generally assume that the comment starts on a legal 
column and that the comment will apply to the next line typed, which will have 
the same indent, and that the lack of space after '#' (there have been many 
such in idlelib) is preference, indifference, or error.

The only exception relevant to IDLE is '##' inserted at the beginning of code 
lines  to (temporarily) make them ignored.  If one places the cursor at the end 
of such a line and hits return to insert new lines, some indent is likely 
wanted if the line above is indented.  Matching '##.*\n' is easy enough.

Note that smart indent always uses the line above, if there is one, because 
there typically is not one below, and if there is, either choice is arbitrary 
and being smarter would cost time.  (This should be in revised doc.)

messages: 312613
nosy: csabella, terry.reedy
priority: normal
severity: normal
stage: test needed
status: open
title: IDLE: make smart indent after comment line consistent
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

Python tracker <>
Python-bugs-list mailing list

Reply via email to