Skip to content

Improve parser to accept complicated block of code under interactive REPL #111658

Closed
@hsfzxjy

Description

@hsfzxjy

Feature or enhancement

Proposal:

Previously the Python REPL only accepts either a single compound statement or a line of simple statements. For example, we can either do:

>>> def f():
...     pass
... 
>>> 

or do:

>>> a = 1; a *= 3; print(a)
3
>>> 

However, the REPL doesn't accept multiple consecutive compound statements, or any simple statement followed soon after a compound statement:

>>> def f():
...     pass
... def g():
  File "<stdin>", line 3
    def g():
    ^^^
SyntaxError: invalid syntax
>>> def f():
...     pass
... a = 1
  File "<stdin>", line 3
    a = 1
    ^
SyntaxError: invalid syntax
>>> 

This will cause a lot of frustration when, for example, a user pastes snippet directly into the REPL and sees unexpected syntax error.

Hence, I propose to improve the parser, specifically the statement_newline rule to accept the abovementioned cases.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions