Author Topic: try: except: indentation error (Python)  (Read 8250 times)

PouncingPanda

  • Community Member
  • Posts: 74
  • Hero Points: 2
try: except: indentation error (Python)
« on: April 20, 2008, 01:01:06 AM »
I am using SE v13.0.0 on Vista.  When I type in the following:

Code: [Select]
try:
    self.startState = attrs['start']
    except:

when I hit 'return' at the end of the line that says 'except:' what happens is it goes to the next line but does not deindent except which I believe it should.  I get this:

Code: [Select]
try:
    self.startState = attrs['start']
    except:
        print 'something here'

when I should get this:

Code: [Select]
try:
    self.startState = attrs['start']
except:
    print 'something here'

I understand that with whitespace-significant indentation for Python it is a black art to determine when to deindent sometimes, but things like 'except', 'elseif' and 'else' should come with a deindent after the ':' IMHO as 100% of the time we want a deindent and a lack of deindent for these keywords constitutes a syntax error.

PouncingPanda

  • Community Member
  • Posts: 74
  • Hero Points: 2
Re: try: except: indentation error (Python)
« Reply #1 on: May 02, 2008, 06:56:28 AM »
Is anybody else seeing this?  I'm not 100% sure but I seem to recall that hitting ':' after 'else', 'elif' or 'except' causes a line to de-indent which would make this a regression from SE 11.  If not can I beg or plead for this feature?  I know that hitting '}' in C++ causes a de-indent, but nothing in Python?  We already have the ':' indenting at return but after 'else', 'elif' and 'except' we would want to de-indent that line 100% of the time.  After all current behavior gives us this:

Code: [Select]
try:
    self.startState = attrs['start']
    except:
        print 'something here'

and

Code: [Select]
if re.match('data\\.', val):
    line += '4 '
    elif val[0] == '"':
        line += '8 '
        else:
            line += '0 '

which is a syntax error 100% of the time.

edit: spelling error
« Last Edit: May 03, 2008, 12:59:10 AM by PouncingPanda »

kfank

  • Community Member
  • Posts: 12
  • Hero Points: 0
Re: try: except: indentation error (Python)
« Reply #2 on: October 04, 2012, 10:54:24 PM »
I'm using SE 2012 for linux and the indentation issue mentioned in this thread appears to be fixed.  However, I have a related issue with nested try blocks.  In the following code '|' represents the cursor position.

I start with the following and 'except' is indented within the second try block like any other text until the trailing colon is added:
Code: [Select]
try:
    try:
        dofirst()
        except|
Now I type the colon:
Code: [Select]
try:
    try:
        dofirst()
    except:|
So far, so good.  Now I complete this except block.
Code: [Select]
try:
    try:
        dofirst()
    except:
        reportError()
        |
At this point I add the except for the outer try block.  I backspace twice over the two indent levels and type...
Code: [Select]
try:
    try:
        dofirst()
    except:
        reportError()
except|
but when I type the trailing colon it aligns the except with the inner try block:
Code: [Select]
try:
    try:
        dofirst()
    except:
        reportError()
    except:|
I think it makes sense to automatically outdent, but not to automatically indent.  How does the editor know which block it should be associated with?  It doesn't and makes the wrong assumption that it is the inner one.  That's a good assumption if you begin typing already indented within the inner block.  But I gave the editor a hint by backspacing first before typing except: and it should not have overridden this.

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1818
  • Hero Points: 151
Re: try: except: indentation error (Python)
« Reply #3 on: October 05, 2012, 02:17:37 PM »
Good point.  It will be fixed in the next 17.0.2 hotfix.