Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
webknjaz opened this issue Nov 19, 2019 · 5 comments

Comments

@webknjaz
Copy link
Contributor

webknjaz commented Nov 19, 2019

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.

@wyattanderson
Copy link

wyattanderson commented Dec 13, 2019

Can confirm! Also seeing this under similar circumstances.

@webknjaz

This comment has been minimized.

@DorConnecteam
Copy link

hi, Is there any update or workaround for this issue?

@tanaymeh
Copy link

tanaymeh commented Sep 9, 2023

Has this issue been solved or is there a reliable workaround?

@hoorelbeke-jimmy
Copy link

I have a similar issue that may have common causes: #222 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants