Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse type parameters in class definitions #95

Merged
merged 5 commits into from
Jul 14, 2023

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Jul 12, 2023

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",
            ),
        },
    ),
],

parser/src/parser.rs Outdated Show resolved Hide resolved
parser/src/parser.rs Outdated Show resolved Hide resolved
parser/src/parser.rs Outdated Show resolved Hide resolved
@zanieb zanieb marked this pull request as ready for review July 13, 2023 13:52
@MichaReiser
Copy link
Contributor

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

@MichaReiser MichaReiser merged commit c33fbee into RustPython:main Jul 14, 2023
charliermarsh pushed a commit that referenced this pull request Jul 17, 2023
Extends #95
Closes #82 

Adds parsing of new `type` soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g. 

```python
# A non-generic type alias
type IntOrStr = int | str

# A generic type alias
type ListOrSet[T] = list[T] | set[T]

# A type alias that includes a forward reference
type AnimalOrVegetable = Animal | "Vegetable"

# A generic self-referential type alias
type RecursiveList[T] = T | list[RecursiveList[T]]
```

All type parameter kinds are supported as in #95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519
zanieb added a commit to astral-sh/RustPython-Parser that referenced this pull request Jul 17, 2023
…hon#97)

Extends RustPython#95
Closes RustPython#82

Adds parsing of new `type` soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g.

```python
type IntOrStr = int | str

type ListOrSet[T] = list[T] | set[T]

type AnimalOrVegetable = Animal | "Vegetable"

type RecursiveList[T] = T | list[RecursiveList[T]]
```

All type parameter kinds are supported as in RustPython#95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519
zanieb added a commit to astral-sh/RustPython-Parser that referenced this pull request Jul 17, 2023
…hon#97)

Extends RustPython#95
Closes RustPython#82

Adds parsing of new `type` soft keyword for defining type aliases.

Supports type alias statements as defined in PEP 695 e.g.

```python
type IntOrStr = int | str

type ListOrSet[T] = list[T] | set[T]

type AnimalOrVegetable = Animal | "Vegetable"

type RecursiveList[T] = T | list[RecursiveList[T]]
```

All type parameter kinds are supported as in RustPython#95.

Builds on soft keyword abstractions introduced in RustPython/RustPython#4519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants