astor icon indicating copy to clipboard operation
astor copied to clipboard

Added support for new ast nodes for pattern matching.

Open peaceamongworlds opened this issue 3 years ago • 3 comments

Pattern matching has been added to Python 3.10, which introduces four new ast nodes. This adds support for those nodes.

This seems pretty straightforward, but tell me if I need to change anything else.

peaceamongworlds avatar Mar 14 '21 17:03 peaceamongworlds

Be aware though Pattern matching AST is unstable right now: https://bugs.python.org/issue42128#msg388583

isidentical avatar Mar 14 '21 17:03 isidentical

You'll want tests and a changelog entry. My PRs can serve as examples. But it does seem like this addition may be premature, anyway.

Kodiologist avatar Mar 15 '21 17:03 Kodiologist

What I could gather from https://docs.python.org/3/library/ast.html#abstract-grammar

 | Match(expr subject, match_case* cases)
...
    match_case = (pattern pattern, expr? guard, stmt* body)

    pattern = MatchValue(expr value)
            | MatchSingleton(constant value)
            | MatchSequence(pattern* patterns)
            | MatchMapping(expr* keys, pattern* patterns, identifier? rest)
            | MatchClass(expr cls, pattern* patterns, identifier* kwd_attrs, pattern* kwd_patterns)

            | MatchStar(identifier? name)
            -- The optional "rest" MatchMapping parameter handles capturing extra mapping keys

            | MatchAs(pattern? pattern, identifier? name)
            | MatchOr(pattern* patterns)

Of which MatchValue, MatchSingleton, MatchSequence, MatchMapping and MatchClass seem to be missing

siikamiika avatar Dec 17 '21 22:12 siikamiika