@@ -112,6 +112,7 @@ simple_stmts[asdl_stmt_seq*]:
112112# will throw a SyntaxError.
113113simple_stmt[stmt_ty] (memo):
114114 | assignment
115+ | &"type" type_alias
115116 | e=star_expressions { _PyAST_Expr(e, EXTRA) }
116117 | &'return' return_stmt
117118 | &('import' | 'from') import_stmt
@@ -252,8 +253,8 @@ class_def[stmt_ty]:
252253
253254class_def_raw[stmt_ty]:
254255 | 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,
257258 (b) ? ((expr_ty) b)->v.Call.args : NULL,
258259 (b) ? ((expr_ty) b)->v.Call.keywords : NULL,
259260 c, NULL, EXTRA) }
@@ -267,16 +268,16 @@ function_def[stmt_ty]:
267268
268269function_def_raw[stmt_ty]:
269270 | 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,
272273 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
273274 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 {
275276 CHECK_VERSION(
276277 stmt_ty,
277278 5,
278279 "Async functions are",
279- _PyAST_AsyncFunctionDef(n->v.Name.id,
280+ _PyAST_AsyncFunctionDef(n->v.Name.id, t,
280281 (params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
281282 b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
282283 ) }
@@ -628,6 +629,39 @@ keyword_patterns[asdl_seq*]:
628629keyword_pattern[KeyPatternPair*]:
629630 | arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
630631
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+
631665# EXPRESSIONS
632666# -----------
633667
0 commit comments