Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:
In general case the AST does not preserve detailed information about all syntactic elements. For example in the AST for `a + b` does not include lineno and col_offset for "+". >>> ast.dump(ast.parse('a + b'), include_attributes=True) "Module(body=[Expr(value=BinOp(left=Name(id='a', ctx=Load(), lineno=1, col_offset=0, end_lineno=1, end_col_offset=1), op=Add(), right=Name(id='b', ctx=Load(), lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), lineno=1, col_offset=0, end_lineno=1, end_col_offset=5)], type_ignores=[])" But in most cases you can determine it from the position of the surrounded nodes. "+" lies between the end of BinOp.left and the beginning of BinOp.right, i.e. between columns 1 and 4 at line 1. What is left is to count the number of whitespace character before and after "+" to determine its exact position. The same method you can use to find the position of the "else" keyword. It is easier in 3.8 since the AST contains now also the position of the end of the node (end_lineno and end_col_offset). ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue21992> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com