Skip to content

[BUG] return on the 1-2 indent levels levels inside of try/except resets the closes except level #193

@webknjaz

Description

@webknjaz

While checking some code related to #190 I've discovered another bug.

Let me start with a demo:

from pathlib import Path
from redbaron import RedBaron

src = Path('demo.py').read_text()
fst = RedBaron(src)

fimp1, = fst.find_all('from_import')
fimp1.insert_after(fimp1)

Path('out.demo.py').write_text(fst.dumps())

demo.py:

from x import y


def func(x, y, z):
    try:
        return
        c()
    except:
        smth()

class A:
    try:
        return
        c()
    except:
        smth()

try:
    try:
        return
        c()
    except:
        smth()
except:
    pass

try:
    try:
        try:
            return
            c()
        except:
            smth()
        return
    except:
        pass
except:
    pass

out.demo.py:

from x import y
from x import y


def func(x, y, z):
    try:
        return
        c()
except:
        smth()

class A:
    try:
        return
        c()
except:
        smth()

try:
    try:
        return
        c()
except:
        smth()
except:
    pass

try:
    try:
        try:
            return
            c()
        except:
            smth()
        return
except:
        pass
except:
    pass

What's wrong?

If you look closer, you'll notice that some of the except: lines don't have any indentation at all after dumping.
It looks like this only happens when the return statement is present in the try part and from ... import ... statement is inserted into FST (top level?). Also, it doesn't seem to affect try/return/except on the third level of nesting.

N.B. Along with this issue, I also observe #190.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions