PyREPL - recognize one-line suites

Currently, the Python REPL will always give a ... prompt after any block of code - even if the suite is provided on the same line:

>>> if 1: print("Hello")
... 
Hello

Having extra code after that block is invalid in the old REPL; with the new REPL (3.13+), it’s legal, and not part of the block:

Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if 0: print("Hello")
... print("world")
  File "<stdin>", line 2
    print("world")
    ^^^^^
SyntaxError: invalid syntax
>>>
Python 3.15.0a0 (heads/main:e7a3c20b925, Jun 13 2025, 01:15:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if 0: print("Hello")
... print("world")
... 
world
>>>

Is it reasonable for the REPL to detect that the code block is complete, and immediately execute it rather than waiting for further text?

If the “continue writing code after the block” behaviour is desired, the most straight-forward way to do this is to add another layer to the code:

>>> if 1:
...     if 0: print("Hello")
...     print("world")
...     
world
>>>

which works in all versions. Usually that’s not something I’d be looking for when writing code in a single line though.

1 Like

How would you detect that the user doesn’t want to continue?

>>> if 1: print("Hello")
... else: print("Bye")
... 
7 Likes

Ah. Good point. Yep, that’s a killer right there. My first thought was “oh, well maybe this can be used for the ones that don’t have anything else after them”, but that actually isn’t all that many (with, def, class), and the explanation complexity of “these ones don’t prompt for continuation” isn’t worth it.

I should have checked that before posting; my original thought did include with open(...) as f: use f as a use-case, but I went with the simpler if statement for the post, not realising that I’d just revealed the fatal flaw :smiley: Ah well.

1 Like

IMO, some piece of information about it could be added to REPL docs. For the newcomers that don’t understand why command wasn’t executed immediately.

2 Likes