Allow `pass` inside `match` statements

Currently, using pass in the body of a match statement raises a SyntaxError:

>>> match 2:
...    pass
  File "<stdin>", line 2
        pass
            ^
SyntaxError: invalid syntax

This is somewhat surprising.

A quick look at the Python grammar reveals why it doesn’t work: a match_stmt consists of one or more case_blocks. As the documentation points out, pass can be used “when a statement is required syntactically,” and case_blocks aren’t considered statements.

I propose we change the grammar to accept pass in the body of a match statement. I can look into the required AST changes if there is support for this idea.

The current alternative is quite clunky:

>>> match 2:
...     case _:
...         pass
... 

What use is there for a match with no cases and just a pass statement?

6 Likes

What use is there for a match with no cases and just a pass statement?

Same as the use for an if statement or function definition with just a pass statement

From the same documentation: “Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level.”

I often use pass as a placeholder when I’m writing code and was surprised when it didn’t work in a match statement.

2 Likes

Put a TODO comment in instead of the empty match (for others or future you to know what you were going to implement)

A match line on its own isn’t a complete block. Similarly, you can’t write try: pass on its own, the block requires an except or finally to be valid syntax. Once you have valid syntax, then you can use pass to keep those blocks empty, but it requires the valid syntax first.

12 Likes

After considering the try: pass example, I no longer think the current behavior is surprising. Thank you all for the feedback!

13 Likes