Skip to content

Commit

Permalink
Merge pull request python#7 from cdce8p/eric-ast-changes
Browse files Browse the repository at this point in the history
AST changes
  • Loading branch information
erictraut authored Apr 18, 2023
2 parents 830b36a + 55d0fd9 commit 618e173
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ keyword_pattern[KeyPatternPair*]:

type_alias[stmt_ty]:
| "type" n=NAME t=[type_params] '=' b=expression {
CHECK_VERSION(stmt_ty, 12, "Type statement is", _PyAST_TypeAlias(n->v.Name.id, t, b, EXTRA)) }
CHECK_VERSION(stmt_ty, 12, "Type statement is",
_PyAST_TypeAlias(CHECK(expr_ty, _PyPegen_set_expr_context(p, n, Store)), t, b, EXTRA)) }

# Type parameter declaration
# --------------------------
Expand Down
8 changes: 4 additions & 4 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,8 @@ def visit_ParamSpec(self, node):
self.write("**" + node.name)

def visit_TypeAlias(self, node):
self.fill("type " + node.name)
self.fill("type ")
self.traverse(node.name)
self._typeparams_helper(node.typeparams)
self.write(" = ")
self.traverse(node.value)
Expand Down
2 changes: 1 addition & 1 deletion Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Python

| Delete(expr* targets)
| Assign(expr* targets, expr value, string? type_comment)
| TypeAlias(identifier name, typeparam* typeparams, expr value)
| TypeAlias(expr name, typeparam* typeparams, expr value)
| AugAssign(expr target, operator op, expr value)
-- 'simple' indicates that we annotate simple name without parens
| AnnAssign(expr target, expr annotation, expr? value, int simple)
Expand Down
2 changes: 1 addition & 1 deletion Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ struct validator {
static int validate_stmts(struct validator *, asdl_stmt_seq *);
static int validate_exprs(struct validator *, asdl_expr_seq *, expr_context_ty, int);
static int validate_patterns(struct validator *, asdl_pattern_seq *, int);
static int validate_typeparams(struct validator *, asdl_typeparam_seq *);
static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
static int validate_stmt(struct validator *, stmt_ty);
static int validate_expr(struct validator *, expr_ty, expr_context_ty);
static int validate_pattern(struct validator *, pattern_ty, int);
static int validate_typeparam(struct validator *, typeparam_ty);

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

static int
validate_typeparam(struct validator *state, typeparam_ty tp)
{
VALIDATE_POSITIONS(tp);
int ret = -1;
switch (tp->kind) {
case TypeVar_kind:
ret = validate_name(tp->v.TypeVar.name) &&
(!tp->v.TypeVar.bound ||
validate_expr(state, tp->v.TypeVar.bound, Load));
break;
case ParamSpec_kind:
ret = validate_name(tp->v.ParamSpec.name);
break;
case TypeVarTuple_kind:
ret = validate_name(tp->v.TypeVarTuple.name);
break;
}
return ret;
}

static int
_validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
{
Expand Down Expand Up @@ -709,13 +732,15 @@ validate_stmt(struct validator *state, stmt_ty stmt)
switch (stmt->kind) {
case FunctionDef_kind:
ret = validate_body(state, stmt->v.FunctionDef.body, "FunctionDef") &&
validate_typeparams(state, stmt->v.FunctionDef.typeparams) &&
validate_arguments(state, stmt->v.FunctionDef.args) &&
validate_exprs(state, stmt->v.FunctionDef.decorator_list, Load, 0) &&
(!stmt->v.FunctionDef.returns ||
validate_expr(state, stmt->v.FunctionDef.returns, Load));
break;
case ClassDef_kind:
ret = validate_body(state, stmt->v.ClassDef.body, "ClassDef") &&
validate_typeparams(state, stmt->v.ClassDef.typeparams) &&
validate_exprs(state, stmt->v.ClassDef.bases, Load, 0) &&
validate_keywords(state, stmt->v.ClassDef.keywords) &&
validate_exprs(state, stmt->v.ClassDef.decorator_list, Load, 0);
Expand Down Expand Up @@ -747,7 +772,9 @@ validate_stmt(struct validator *state, stmt_ty stmt)
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
break;
case TypeAlias_kind:
ret = validate_expr(state, stmt->v.TypeAlias.value, Load);
ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
validate_typeparams(state, stmt->v.TypeAlias.typeparams) &&
validate_expr(state, stmt->v.TypeAlias.value, Load);
break;
case For_kind:
ret = validate_expr(state, stmt->v.For.target, Store) &&
Expand Down Expand Up @@ -896,6 +923,7 @@ validate_stmt(struct validator *state, stmt_ty stmt)
break;
case AsyncFunctionDef_kind:
ret = validate_body(state, stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
validate_typeparams(state, stmt->v.AsyncFunctionDef.typeparams) &&
validate_arguments(state, stmt->v.AsyncFunctionDef.args) &&
validate_exprs(state, stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
(!stmt->v.AsyncFunctionDef.returns ||
Expand Down Expand Up @@ -968,6 +996,20 @@ validate_patterns(struct validator *state, asdl_pattern_seq *patterns, int star_
return 1;
}

static int
validate_typeparams(struct validator *state, asdl_typeparam_seq *tps)
{
Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(tps); i++) {
typeparam_ty tp = asdl_seq_GET(tps, i);
if (tp) {
if (!validate_typeparam(state, tp))
return 0;
}
}
return 1;
}


/* See comments in symtable.c. */
#define COMPILER_STACK_FRAME_SCALE 3
Expand Down
21 changes: 21 additions & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeStat
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
static int astfold_typeparam(typeparam_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);

#define CALL(FUNC, TYPE, ARG) \
if (!FUNC((ARG), ctx_, state)) \
Expand Down Expand Up @@ -881,6 +882,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
}
switch (node_->kind) {
case FunctionDef_kind:
CALL_SEQ(astfold_typeparam, typeparam, node_->v.FunctionDef.typeparams);
CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
Expand All @@ -889,6 +891,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
}
break;
case AsyncFunctionDef_kind:
CALL_SEQ(astfold_typeparam, typeparam, node_->v.AsyncFunctionDef.typeparams);
CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
Expand All @@ -897,6 +900,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
}
break;
case ClassDef_kind:
CALL_SEQ(astfold_typeparam, typeparam, node_->v.ClassDef.typeparams);
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
Expand Down Expand Up @@ -924,6 +928,8 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
break;
case TypeAlias_kind:
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
CALL_SEQ(astfold_typeparam, typeparam, node_->v.TypeAlias.typeparams);
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
break;
case For_kind:
Expand Down Expand Up @@ -1078,6 +1084,21 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
return 1;
}

static int
astfold_typeparam(typeparam_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
{
switch (node_->kind) {
case TypeVar_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
break;
case ParamSpec_kind:
break;
case TypeVarTuple_kind:
break;
}
return 1;
}

#undef CALL
#undef CALL_OPT
#undef CALL_SEQ
Expand Down

0 comments on commit 618e173

Please sign in to comment.