mcpyrate icon indicating copy to clipboard operation
mcpyrate copied to clipboard

Python 3.10: match/case

Open Technologicat opened this issue 1 year ago • 1 comments
trafficstars

https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching

I almost missed the AST implications of this.

Check if we need to change something - sounds like at least the unparser will need some new cases.

Technologicat avatar Sep 26 '24 16:09 Technologicat

https://docs.python.org/3/library/ast.html

    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)

Additionally, but unrelatedly, the unparser doesn't support these yet (3.12+):

    type_ignore = TypeIgnore(int lineno, string tag)

    type_param = TypeVar(identifier name, expr? bound)
               | ParamSpec(identifier name)
               | TypeVarTuple(identifier name)

Even though mcpyrate already works on Pythons up to 3.12, it seems there's a bit of work for full proper support of 3.10+.

Technologicat avatar Sep 26 '24 20:09 Technologicat

Also this (3.11+):

https://docs.python.org/3/library/ast.html#ast.TryStar

Technologicat avatar Sep 27 '24 08:09 Technologicat

All these done as of b82af24.

That should be it for AST changes for 3.10, 3.11 and 3.12.

Technologicat avatar Sep 27 '24 10:09 Technologicat