Hello Python Devs, For some time I've been hoping to be able to use pattern matching in Python, so PEP 622 is an exciting proposition. I do have some concerns, mostly about the syntax, because it doesn't currently match the style the rest of Python seems to lay out.
The thing I'd most suggest changing is the syntax for constants within patterns from `.CONSTANT`. Dots can already be in patterns as part of attribute access, so the viewer will need to distinguish between different uses when they see one. Instead I propose an at-symbol, like `@CONSTANT`. The `@` is a symbol used in Python syntax already, so it would not require changes to the tokeniser. However, it can never currently appear in patterns; neither decorators nor the matmul operator would create any ambiguity for a human trying to skim over a pattern, unlike the currently proposed syntax. The `@` does share a downside with the dot, which is lack of precedent. The PEP alludes to `.name` having an entirely different use in imports, and the use of `@name` in decorators does not relate to this new pattern syntax either. All the same, `@` would still make for a clearer symbol because it would be more distinct from attributes. (There is a weak argument that `@CONSTANT` already has a precedent because of its use in decorators, because decorators are names that are used at the top level, and because the `@CONSTANT` syntax in patterns would mostly also refer to top level names. However, users will adapt to whichever syntax is settled on.)
I have two aesthetic suggestions, which have smaller impacts on clarity than the above. Firstly, replacing `|` for delimiting patterns with the more explicit keyword `or`. Python favours keywords where other languages might use punctuation, such as `range(1, 10)` rather than `1..10`, because full words are closer to natural language than punctuation is. Secondly, I prefer the look of `(_, _) as point` over `point := (_, _)` for named sub-patterns. This one is harder to justify because it's a personal preference. All the same, it's worth considering if you haven't already done so.
Finally, I dislike the way the underscore variable name seems like magic. That is, it is a variable name that behaves differently from other names. I recognise that there is a history of Python code treating `_` differently — such as referring to the previous result in the REPL — and I can't offer any better alternatives, just my opinion. I hope you consider these perspectives, especially the change from `.CONSTANT` to `@CONSTANT`. I'm anxious to get my hands on pattern matching syntax, in whatever form it eventually takes.
Best wishes, IFcoltransG