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

"dumps" creates wrong indentation inside a for loop #184

Open
vsjha18 opened this issue Apr 9, 2019 · 0 comments
Open

"dumps" creates wrong indentation inside a for loop #184

vsjha18 opened this issue Apr 9, 2019 · 0 comments

Comments

@vsjha18
Copy link

vsjha18 commented Apr 9, 2019

If you iterate on a function node and issue "dumps" on each line, then any if/else block is incorrectly indented.

Use this code to reproduce the problem

In [1]: import redbaron

In [2]: code = """
   ...: def foo():
   ...:     simple_code()
   ...:     # a comment
   ...:     if x is True:
   ...:         add()
   ...:     else:
   ...:         sub()
   ...: """

In [3]: tree = redbaron.RedBaron(code)

In [4]: defnode = tree[1]

In [5]: type(defnode)
Out[5]: redbaron.nodes.DefNode

In [6]: for line in defnode:
   ...:     print(line.dumps())
   ...:
simple_code()
# a comment
if x is True:
        add()
    else:
        sub()

As you can see above if and else are not in the same vertical alignment.

As per my use case I am extracting all the metadata from the attributes of the defnode and I need to unparse the function body line by line. I am able to do it easily with ast by I am loosing all the code comments, which I want to preserve. If somehow this indentation issue is resolved, then I am all done !

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

1 participant