@@ -112,6 +112,7 @@ simple_stmts[asdl_stmt_seq*]:
112
112
# will throw a SyntaxError.
113
113
simple_stmt[stmt_ty] (memo):
114
114
| assignment
115
+ | &"type" type_alias
115
116
| e=star_expressions { _PyAST_Expr(e, EXTRA) }
116
117
| &'return' return_stmt
117
118
| &('import' | 'from') import_stmt
@@ -252,8 +253,8 @@ class_def[stmt_ty]:
252
253
253
254
class_def_raw[stmt_ty]:
254
255
| invalid_class_def_raw
255
- | 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
256
- _PyAST_ClassDef(a->v.Name.id,
256
+ | 'class' a=NAME t=[type_params] b=['(' z=[arguments] ')' { z }] ':' c=block {
257
+ _PyAST_ClassDef(a->v.Name.id, t,
257
258
(b) ? ((expr_ty) b)->v.Call.args : NULL,
258
259
(b) ? ((expr_ty) b)->v.Call.keywords : NULL,
259
260
c, NULL, EXTRA) }
@@ -267,16 +268,16 @@ function_def[stmt_ty]:
267
268
268
269
function_def_raw[stmt_ty]:
269
270
| invalid_def_raw
270
- | 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
271
- _PyAST_FunctionDef(n->v.Name.id,
271
+ | 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
272
+ _PyAST_FunctionDef(n->v.Name.id, t,
272
273
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
273
274
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
274
- | ASYNC 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
275
+ | ASYNC 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
275
276
CHECK_VERSION(
276
277
stmt_ty,
277
278
5,
278
279
"Async functions are",
279
- _PyAST_AsyncFunctionDef(n->v.Name.id,
280
+ _PyAST_AsyncFunctionDef(n->v.Name.id, t,
280
281
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
281
282
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
282
283
) }
@@ -628,6 +629,39 @@ keyword_patterns[asdl_seq*]:
628
629
keyword_pattern[KeyPatternPair*]:
629
630
| arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
630
631
632
+ # Type statement
633
+ # ---------------
634
+
635
+ type_alias[stmt_ty]:
636
+ | "type" n=NAME t=[type_params] '=' b=expression {
637
+ CHECK_VERSION(stmt_ty, 12, "Type statement is",
638
+ _PyAST_TypeAlias(CHECK(expr_ty, _PyPegen_set_expr_context(p, n, Store)), t, b, EXTRA)) }
639
+
640
+ # Type parameter declaration
641
+ # --------------------------
642
+
643
+ type_params[asdl_typeparam_seq*]: '[' t=type_param_seq ']' {
644
+ CHECK_VERSION(asdl_typeparam_seq *, 12, "Type parameter lists are", t) }
645
+
646
+ type_param_seq[asdl_typeparam_seq*]: a[asdl_typeparam_seq*]=','.type_param+ [','] { a }
647
+
648
+ type_param[typeparam_ty] (memo):
649
+ | a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
650
+ | '*' a=NAME colon=":" e=expression {
651
+ RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
652
+ ? "cannot use constraints with TypeVarTuple"
653
+ : "cannot use bound with TypeVarTuple")
654
+ }
655
+ | '*' a=NAME { _PyAST_TypeVarTuple(a->v.Name.id, EXTRA) }
656
+ | '**' a=NAME colon=":" e=expression {
657
+ RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
658
+ ? "cannot use constraints with ParamSpec"
659
+ : "cannot use bound with ParamSpec")
660
+ }
661
+ | '**' a=NAME { _PyAST_ParamSpec(a->v.Name.id, EXTRA) }
662
+
663
+ type_param_bound[expr_ty]: ":" e=expression { e }
664
+
631
665
# EXPRESSIONS
632
666
# -----------
633
667
0 commit comments