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
Unable to get the else while parsing through AST module #88343
Comments
Hi Team, I was parsing python file using AST module but ran into a situation where the else statement is not found in the parsed data. --------------------- In the parsed data I can see "if" followed by "elif" but the "else" part I am not able to see. Sample code used: sample_data = """
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
"""
---------------------
tree = ast.parse(sample_data ) The above code provide me below data in ast.dump(tree) Module(body=[If(test=Compare(left=Name(id='num', ctx=Load()), ops=[Gt()], comparators=[Constant(value=0, kind=None)]), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Constant(value='Positive number', kind=None)], keywords=[]))], orelse=[If(test=Compare(left=Name(id='num', ctx=Load()), ops=[Eq()], comparators=[Constant(value=0, kind=None)]), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Constant(value='Zero', kind=None)], keywords=[]))], orelse=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Constant(value='Negative number', kind=None)], keywords=[]))])])], type_ignores=[]) While I was traversing through this tree I can't see else in the structure because it is subpart of orelse i.e. inside elif part. Doc referred is : https://docs.python.org/3/library/ast.html Thanks and Best Regards, |
Formatting the result, I get: You'll have to extract the "else" part by walking the tree yourself.
It is in the parsed data, though. Just not directly in the "If" node. |
Hi Eric, Thanks for the quick response here, I have gone through your suggestion but following that I am able to print the else node data i.e. "print("Negative number")" but unable to get the lineno of that else child node. Could you please confirm if there is a way in AST where I can get the lineno of the else child node. Thanks and Best Regards, |
It's in the AST. >>> tree.body[0].orelse[0].orelse[0].lineno
7 To see it with dump, use: ast.dump(tree, include_attributes=True) Which, after some reformatting, gives: Module(body=[If(test=Compare(left=Name(id='num', ctx=Load(), lineno=2, col_offset=3, end_lineno=2, end_col_offset=6), I don't think there's any enhancement to be made here, so I'm going to close this issue. |
Hi Eric, I have tried the action plan suggested by you in your last comment, but below are my logs and still I am unable to get lineno for else statement. ----The respective line no for each line are below ---- 1
Module(body=[If(test=Compare(left=Name(id='num', ctx=Load(), lineno=2, col_offset=3, end_lineno=2, end_col_offset=6),
Here the lineno 7 denotes to the body of else statement not the else lineno. Could you please help me out where you can show the respective line no of else statement i.e. 6 in above example. Thanks and Best Regards, |
There's no actual code associated with the "else" line, so there's no way to get the line number. dis.dis(sample_data) might give you some insight. Since this all works as designed, I'm going to close this issue. You might get more help on StackOverflow or similar. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: