RustPython / Parser

MIT License
66 stars 24 forks source link

Parse type parameters in class definitions #95

Closed zanieb closed 11 months ago

zanieb commented 11 months ago

Extends #93 Part of #82

Adds parsing of generic type variable definitions for classes.

Supports all type variable definitions e.g.

# TypeVar
class Test[T]:
   pass

# TypeVar with bound
class Test[T: str]:
   pass

# TypeVarTuple
class Test[*Ts]:
   pass

# ParamSpec
class Test[**P]:
   pass

Example output

class Foo[X, Y: str, *U, **P]:
   pass
...,
type_params: [
    TypeVar(
        TypeParamTypeVar {
            range: 10..11,
            name: Identifier(
                "X",
            ),
            bound: None,
        },
    ),
    TypeVar(
        TypeParamTypeVar {
            range: 13..19,
            name: Identifier(
                "Y",
            ),
            bound: Some(
                Name(
                    ExprName {
                        range: 16..19,
                        id: Identifier(
                            "str",
                        ),
                        ctx: Load,
                    },
                ),
            ),
        },
    ),
    TypeVarTuple(
        TypeParamTypeVarTuple {
            range: 21..23,
            name: Identifier(
                "U",
            ),
        },
    ),
    ParamSpec(
        TypeParamParamSpec {
            range: 25..28,
            name: Identifier(
                "P",
            ),
        },
    ),
],
MichaReiser commented 11 months ago

LGTM. Let's give @youknowone a chance to chime in as well.