Skip to content

Commit 618e173

Browse files
authored
Merge pull request python#7 from cdce8p/eric-ast-changes
AST changes
2 parents 830b36a + 55d0fd9 commit 618e173

File tree

8 files changed

+81
-16
lines changed

8 files changed

+81
-16
lines changed

Grammar/python.gram

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ keyword_pattern[KeyPatternPair*]:
634634

635635
type_alias[stmt_ty]:
636636
| "type" n=NAME t=[type_params] '=' b=expression {
637-
CHECK_VERSION(stmt_ty, 12, "Type statement is", _PyAST_TypeAlias(n->v.Name.id, t, b, EXTRA)) }
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)) }
638639

639640
# Type parameter declaration
640641
# --------------------------

Include/internal/pycore_ast.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/ast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,8 @@ def visit_ParamSpec(self, node):
10701070
self.write("**" + node.name)
10711071

10721072
def visit_TypeAlias(self, node):
1073-
self.fill("type " + node.name)
1073+
self.fill("type ")
1074+
self.traverse(node.name)
10741075
self._typeparams_helper(node.typeparams)
10751076
self.write(" = ")
10761077
self.traverse(node.value)

Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Python
2525

2626
| Delete(expr* targets)
2727
| Assign(expr* targets, expr value, string? type_comment)
28-
| TypeAlias(identifier name, typeparam* typeparams, expr value)
28+
| TypeAlias(expr name, typeparam* typeparams, expr value)
2929
| AugAssign(expr target, operator op, expr value)
3030
-- 'simple' indicates that we annotate simple name without parens
3131
| AnnAssign(expr target, expr annotation, expr? value, int simple)

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/Python-ast.c

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/ast.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ struct validator {
1717
static int validate_stmts(struct validator *, asdl_stmt_seq *);
1818
static int validate_exprs(struct validator *, asdl_expr_seq *, expr_context_ty, int);
1919
static int validate_patterns(struct validator *, asdl_pattern_seq *, int);
20+
static int validate_typeparams(struct validator *, asdl_typeparam_seq *);
2021
static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
2122
static int validate_stmt(struct validator *, stmt_ty);
2223
static int validate_expr(struct validator *, expr_ty, expr_context_ty);
2324
static int validate_pattern(struct validator *, pattern_ty, int);
25+
static int validate_typeparam(struct validator *, typeparam_ty);
2426

2527
#define VALIDATE_POSITIONS(node) \
2628
if (node->lineno > node->end_lineno) { \
@@ -672,6 +674,27 @@ validate_pattern(struct validator *state, pattern_ty p, int star_ok)
672674
return ret;
673675
}
674676

677+
static int
678+
validate_typeparam(struct validator *state, typeparam_ty tp)
679+
{
680+
VALIDATE_POSITIONS(tp);
681+
int ret = -1;
682+
switch (tp->kind) {
683+
case TypeVar_kind:
684+
ret = validate_name(tp->v.TypeVar.name) &&
685+
(!tp->v.TypeVar.bound ||
686+
validate_expr(state, tp->v.TypeVar.bound, Load));
687+
break;
688+
case ParamSpec_kind:
689+
ret = validate_name(tp->v.ParamSpec.name);
690+
break;
691+
case TypeVarTuple_kind:
692+
ret = validate_name(tp->v.TypeVarTuple.name);
693+
break;
694+
}
695+
return ret;
696+
}
697+
675698
static int
676699
_validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
677700
{
@@ -709,13 +732,15 @@ validate_stmt(struct validator *state, stmt_ty stmt)
709732
switch (stmt->kind) {
710733
case FunctionDef_kind:
711734
ret = validate_body(state, stmt->v.FunctionDef.body, "FunctionDef") &&
735+
validate_typeparams(state, stmt->v.FunctionDef.typeparams) &&
712736
validate_arguments(state, stmt->v.FunctionDef.args) &&
713737
validate_exprs(state, stmt->v.FunctionDef.decorator_list, Load, 0) &&
714738
(!stmt->v.FunctionDef.returns ||
715739
validate_expr(state, stmt->v.FunctionDef.returns, Load));
716740
break;
717741
case ClassDef_kind:
718742
ret = validate_body(state, stmt->v.ClassDef.body, "ClassDef") &&
743+
validate_typeparams(state, stmt->v.ClassDef.typeparams) &&
719744
validate_exprs(state, stmt->v.ClassDef.bases, Load, 0) &&
720745
validate_keywords(state, stmt->v.ClassDef.keywords) &&
721746
validate_exprs(state, stmt->v.ClassDef.decorator_list, Load, 0);
@@ -747,7 +772,9 @@ validate_stmt(struct validator *state, stmt_ty stmt)
747772
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
748773
break;
749774
case TypeAlias_kind:
750-
ret = validate_expr(state, stmt->v.TypeAlias.value, Load);
775+
ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
776+
validate_typeparams(state, stmt->v.TypeAlias.typeparams) &&
777+
validate_expr(state, stmt->v.TypeAlias.value, Load);
751778
break;
752779
case For_kind:
753780
ret = validate_expr(state, stmt->v.For.target, Store) &&
@@ -896,6 +923,7 @@ validate_stmt(struct validator *state, stmt_ty stmt)
896923
break;
897924
case AsyncFunctionDef_kind:
898925
ret = validate_body(state, stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
926+
validate_typeparams(state, stmt->v.AsyncFunctionDef.typeparams) &&
899927
validate_arguments(state, stmt->v.AsyncFunctionDef.args) &&
900928
validate_exprs(state, stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
901929
(!stmt->v.AsyncFunctionDef.returns ||
@@ -968,6 +996,20 @@ validate_patterns(struct validator *state, asdl_pattern_seq *patterns, int star_
968996
return 1;
969997
}
970998

999+
static int
1000+
validate_typeparams(struct validator *state, asdl_typeparam_seq *tps)
1001+
{
1002+
Py_ssize_t i;
1003+
for (i = 0; i < asdl_seq_LEN(tps); i++) {
1004+
typeparam_ty tp = asdl_seq_GET(tps, i);
1005+
if (tp) {
1006+
if (!validate_typeparam(state, tp))
1007+
return 0;
1008+
}
1009+
}
1010+
return 1;
1011+
}
1012+
9711013

9721014
/* See comments in symtable.c. */
9731015
#define COMPILER_STACK_FRAME_SCALE 3

Python/ast_opt.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeStat
643643
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
644644
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
645645
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
646+
static int astfold_typeparam(typeparam_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
646647

647648
#define CALL(FUNC, TYPE, ARG) \
648649
if (!FUNC((ARG), ctx_, state)) \
@@ -881,6 +882,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
881882
}
882883
switch (node_->kind) {
883884
case FunctionDef_kind:
885+
CALL_SEQ(astfold_typeparam, typeparam, node_->v.FunctionDef.typeparams);
884886
CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
885887
CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
886888
CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
@@ -889,6 +891,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
889891
}
890892
break;
891893
case AsyncFunctionDef_kind:
894+
CALL_SEQ(astfold_typeparam, typeparam, node_->v.AsyncFunctionDef.typeparams);
892895
CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
893896
CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
894897
CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
@@ -897,6 +900,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
897900
}
898901
break;
899902
case ClassDef_kind:
903+
CALL_SEQ(astfold_typeparam, typeparam, node_->v.ClassDef.typeparams);
900904
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
901905
CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
902906
CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
@@ -924,6 +928,8 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
924928
CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
925929
break;
926930
case TypeAlias_kind:
931+
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
932+
CALL_SEQ(astfold_typeparam, typeparam, node_->v.TypeAlias.typeparams);
927933
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
928934
break;
929935
case For_kind:
@@ -1078,6 +1084,21 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
10781084
return 1;
10791085
}
10801086

1087+
static int
1088+
astfold_typeparam(typeparam_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
1089+
{
1090+
switch (node_->kind) {
1091+
case TypeVar_kind:
1092+
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
1093+
break;
1094+
case ParamSpec_kind:
1095+
break;
1096+
case TypeVarTuple_kind:
1097+
break;
1098+
}
1099+
return 1;
1100+
}
1101+
10811102
#undef CALL
10821103
#undef CALL_OPT
10831104
#undef CALL_SEQ

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy