Skip to content

A pair of problems with the introductory section to the "8. Compound statements" chapter. #158836

Description

@JonothorDarry

Documentation

The beginning of the "8. Compound statements" chapter of the documentation contains:

A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines.

It has been present since at least Python 3.0. In Python 3.5, async for and async with statements were introduced, invalidating

Each clause header begins with a uniquely identifying keyword

In Python 3.10, match statement was introduced, creating a tension between the aforementioned introductory description and the grammar description that follows, in particular:

compound_stmt ::= (...) | match_stmt (...)
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]

A match statement is a compound statement; thus, it consists of clauses, each of which consists of a header and a suite.

A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’

A case block cannot be a clause header of a match statement, since it's at a different indentation level than the match statement (unless the match subject_expr: is not a header itself):

The clause headers of a particular compound statement are all at the same indentation level.

A case block is neither a simple statement nor a compound statement (and not even a list of simple statements), so it can't really be a part of the suite of a compound statement. Since it's neither a part of the suite nor a clause header, it cannot belong to the compound statement if the introductory description is binding.

One solution to the match problem I can think of is to allow nested clauses in the aforementioned description. The other is describing it as an exception. Similarly, async problem can be solved by removing the "Unique" qualification or describing an exception, or maybe adding something akin to "a prefix of keywords". I don't like either of them, as the beginning of this chapter is the part that elucidates some of the main reasons for the clarity of the syntax of the language. Though I guess this quality is a relic of some long-gone past.

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions