pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Auto-indent too large when creating `def` after incomplete `for` loop

Open debonte opened this issue 1 year ago • 2 comments

When pressing Enter after def isDigitOrComma(ch: str) -> bool: below, the automatic indentation is 16 spaces rather than 4.

If you delete the incomplete for loop then auto-indent works as expected.

I can understand that the "Unexpected indentation" errors after that for loop in the reduce function could throw things off, but it looks like the def for isDigitOrComma actually got the parser back on track. The only error on that line is that there's no return statement. So why is the indentation logic confused?

I ran into this while doing some recreational coding over the weekend. I think we should be able to handle this. Stopping in the midst of implementing one function to write another one (presumably to be used in the first) seems like a common scenario to me.

def reduce(input: str) -> str:
    depth = 0
    for i in range(0, len(input)):
        ch = input[i]

        if ch == "[":
            depth += 1

            if depth >= 4 and isRegularNumberPair(input[i:]):
                (left, right) = parseRegularNumberPair(input[i:])

                for leftSearch in range(i, 0, -1):
                    leftCh = input[leftSearch]
                    if leftCh != "," and leftCh != "[" and leftCh != "]":


        elif ch == "]":
            depth -= 1
        elif ch == ",":
            pass
        else:
            pass


def isDigitOrComma(ch: str) -> bool:

debonte avatar Sep 12 '22 17:09 debonte

Is this a parser recovery issue or a bug in the indentation logic? Does the parser believe that the isDigitOrComma def is a child of the for loop?

debonte avatar Sep 12 '22 18:09 debonte

Confirmed that the parser decides that isDigitOrComma is a child of a suite within the reduce function.

We'll first try to improve the parser recovery logic to handle this better.

debonte avatar Sep 14 '22 17:09 debonte

This issue has been fixed in prerelease version 2022.10.11, which we've just released. You can find the changelog here: CHANGELOG.md

heejaechang avatar Oct 05 '22 22:10 heejaechang