berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
793 stars 101 forks source link

Added support for new ast nodes for pattern matching. #195

Open peaceamongworlds opened 3 years ago

peaceamongworlds commented 3 years ago

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.

isidentical commented 3 years ago

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

Kodiologist commented 3 years ago

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.

siikamiika commented 2 years ago

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