Skip to content

Commit

Permalink
PEP 695 - Type Parameter Syntax
Browse files Browse the repository at this point in the history
This PR adds support for parsing and representing Type Parameters and Type Aliases as specified by PEP 695. What's missing are the scope rules, to be implemented in a future PR.

Notable (user visible) changes:

- new `TypeAlias` CST node, which is a `SmallStatement`
- new CST nodes to represent TypeVarLikes: `TypeVar`, `TypeVarTuple`, `ParamSpec`
- new helper CST nodes:  `TypeParameters` to serve as a container for multiple TypeVarLikes, and `TypeParam` which is a single item in a `TypeParameters` (owning the separating comma)
- extended `FunctionDef` and `ClassDef` with an optional `type_parameters` field, as well as `whitespace_after_type_parameters` to own the extra whitespace between type parameters and the following token
  - these new fields are added after all others to avoid breaking callers passing in fields as positional arguments
- in `FunctionDef` and `ClassDef`, `whitespace_after_name` now owns the whitespace before the type parameters if they exist
  • Loading branch information
zsol committed Aug 28, 2023
1 parent 2064e20 commit 87461db
Show file tree
Hide file tree
Showing 13 changed files with 1,792 additions and 32 deletions.
12 changes: 12 additions & 0 deletions libcst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,19 @@
MatchValue,
NameItem,
Nonlocal,
ParamSpec,
Pass,
Raise,
Return,
SimpleStatementLine,
SimpleStatementSuite,
Try,
TryStar,
TypeAlias,
TypeParam,
TypeParameters,
TypeVar,
TypeVarTuple,
While,
With,
WithItem,
Expand Down Expand Up @@ -438,4 +444,10 @@
"VisitorMetadataProvider",
"MetadataDependent",
"MetadataWrapper",
"TypeVar",
"TypeVarTuple",
"ParamSpec",
"TypeParam",
"TypeParameters",
"TypeAlias",
]
Loading

0 comments on commit 87461db

Please sign in to comment.