Ast.parse mode='single' doesn't work as expected - Python Help - Discussions on Python.org
Ast.parse mode='single' doesn't work as expected Python Help NickC (Nick) November 6, 2026, 9:46am 1 When parsing code line by line ast.parse ’s mode='single' doesn’t seem to work correctly, or at least not as expected. Couldn’t find any documentation explaining how it is supposed to work though. import ast source_code = \ """print("xyz") x = 1; y = 2 if True: # <- ast fails: IndentationError: expected an indented block after 'if' statement x = 3 """ for line in source_code.splitlines(): ast_tree = ast.parse(line, mode='single') tjreedy (Terry Jan Reedy) November 6, 2026, 10:06am 2 Mode ‘single’ is for single statements, not single lines.
It is intended for an interactive Python console or shell, as in the standard REPL and IDLE. In single statement mode, the value of non-None expression statements get bound to ‘_’ and blank lines signal the end of compound statements. In your example, you should parse multiline source_code as is. DavidCEllis (David Ellis) November 6, 2026, 10:09am 3 @tjreedy covered most of what I was going to say, but I’ll also add that the ‘mode’ explanations are covered under compile in the builtin functions documentation . jonathandung (Jonathan Dung) November 6, 2026, 11:40am 4 Also see ast — Abstract syntax trees — Python 3.15.2 documentation .
NickC (Nick) November 7, 2026, 4:18pm 5 @DavidCEllis Thanks I was looking under ast.parse() didn’t occur to me to look under compile() . NickC (Nick) November 7, 2026, 4:20pm 6 @tjreedy Problem is in this case I need to ast.parse() line by line. So it appears that ast.parse() can’t pass a single line if statement. duncathan (Dunc Graham) November 7, 2026, 4:26pm 7 Why does it need to be line by line? There’s probably a way to achieve what you’re trying to do, but the best solution will depend on why you actually need it. tjreedy (Terry Jan Reedy) November 7, 2026, 6:24pm 8 ast.parse parses legal python code, depending on the mode.
What is does with a line of non-Python code is somewhat undefined, but what it should do is raise an exception, preferably with a comprehensible message. Why do you say such that? It parses single-line ‘if’ statements just fine. You passed just part of one. This is like, and if not caught would result in, calling .__init__ without a required argument. The parser constructs a tree of instances of grammar classes. The ‘If’ constructor requires a ‘body’ instance.
The error message says so. See the entry for ast.If . Rosuav (Chris Angelico) November 7, 2026, 7:21pm 9 That’s correct (assuming you mean that you have just the condition with no body). The REPL does some minor hackery to figure out whether it has a complete statement or one that’s waiting on an indented block, and will take action accordingly; but there’s no way to compile if cond: on its own. tjreedy (Terry Jan Reedy) November 7, 2026, 9:54pm 10 I believe the new REPL uses the hackery in codeop._maybe_compile that is invoked by the public functions in the same module.
One of these is called by code.interactive_interpreter, which IDLE uses. NickC (Nick) November 7, 2026, 10:44pm 11 @duncathan Because it needs to be done line by line so that the code modifications are done at the correctly time. If it is done in a single batch then it will not fit in with other code modifications which are happening line by line. duncathan (Dunc Graham) November 7, 2026, 10:49pm 12 As in, the result of parsing one line may cause the contents of a later line to be changed?
Am I understanding you correctly? NickC (Nick) November 7, 2026, 10:51pm 13 Semi work-around now found but far from perfect because it fails if there is a comment after the if...: line. import ast source_code = \ """x = 1;y = 2 if True: z = 3 """ # <- ast fails: IndentationError: expected an indented block after 'if' statement for line in source_code.splitlines(): print(f"{line=}") pass_fix = False white_space = line[:(len(line) - len(line.lstrip()))] line = line.lstrip() try: tree = ast.parse(line) except IndentationError: print("pass_fix") pass_fix = True line += "pass" print(f"{line=}") tree = ast.parse(line) # print(tree) ast.fix_missing_locations(tree) modi_line = white_space + ast.unparse(tree) print(f"{modi_line=}") if pass_fix: modi_line = modi_line[:-9] modi_line = modi_line.replace("\n", ";") print(f"{modi_line=}") will_f (Will Fatherley) November 8, 2026, 1:31pm 14 Assuming I understand the problem correctly, you might find a reasonable solution by considering the lineno and col_offset attributes of ast.AST instances (there are additional such attributes).
Using ast.walk , you could accumulate instances in a dictionary whose keys are line numbers and perform the specific task against a k-v pair. This is just one approach. On the other hand I have found myself wanting something like a path object that can provide the API of ast , but can “read” an input string character-by-character.