Skip to content

Commit

Permalink
Revert AST changes for TypeParams
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Apr 17, 2023
1 parent 85a4409 commit 55d0fd9
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 49 deletions.
7 changes: 3 additions & 4 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,9 @@ type_params[asdl_typeparam_seq*]: '[' t=type_param_seq ']' { t }
type_param_seq[asdl_typeparam_seq*]: a[asdl_typeparam_seq*]=','.type_param+ [','] { a }

type_param[typeparam_ty] (memo):
| a=NAME b=[type_param_bound] { _PyAST_TypeVar(
CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
| '*' a=NAME { _PyAST_TypeVarTuple(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), EXTRA) }
| '**' a=NAME { _PyAST_ParamSpec(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), EXTRA) }
| a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
| '*' a=NAME { _PyAST_TypeVarTuple(a->v.Name.id, EXTRA) }
| '**' a=NAME { _PyAST_ParamSpec(a->v.Name.id, EXTRA) }

type_param_bound[expr_ty]: ":" e=expression { e }

Expand Down
14 changes: 7 additions & 7 deletions Include/internal/pycore_ast.h

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

8 changes: 3 additions & 5 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,18 +1058,16 @@ def _typeparams_helper(self, typeparams):
self.interleave(lambda: self.write(", "), self.traverse, typeparams)

def visit_TypeVar(self, node):
self.traverse(node.name)
self.write(node.name)
if node.bound:
self.write(": ")
self.traverse(node.bound)

def visit_TypeVarTuple(self, node):
self.write("*")
self.traverse(node.name)
self.write("*" + node.name)

def visit_ParamSpec(self, node):
self.write("**")
self.traverse(node.name)
self.write("**" + node.name)

def visit_TypeAlias(self, node):
self.fill("type ")
Expand Down
6 changes: 3 additions & 3 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ module Python

type_ignore = TypeIgnore(int lineno, string tag)

typeparam = TypeVar(expr name, expr? bound)
| ParamSpec(expr name)
| TypeVarTuple(expr name)
typeparam = TypeVar(identifier name, expr? bound)
| ParamSpec(identifier name)
| TypeVarTuple(identifier name)
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
}
6 changes: 3 additions & 3 deletions Parser/parser.c

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

40 changes: 20 additions & 20 deletions Python/Python-ast.c

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

8 changes: 4 additions & 4 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,15 @@ validate_typeparam(struct validator *state, typeparam_ty tp)
int ret = -1;
switch (tp->kind) {
case TypeVar_kind:
ret = validate_expr(state, tp->v.TypeVar.name, Store) &&
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_expr(state, tp->v.ParamSpec.name, Store);
ret = validate_name(tp->v.ParamSpec.name);
break;
case TypeVarTuple_kind:
ret = validate_expr(state, tp->v.TypeVarTuple.name, Store);
ret = validate_name(tp->v.TypeVarTuple.name);
break;
}
return ret;
Expand Down Expand Up @@ -772,7 +772,7 @@ validate_stmt(struct validator *state, stmt_ty stmt)
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
break;
case TypeAlias_kind:
ret = validate_name(stmt->v.TypeAlias.name->v.Name.id) &&
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;
Expand Down
3 changes: 0 additions & 3 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1089,14 +1089,11 @@ astfold_typeparam(typeparam_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
{
switch (node_->kind) {
case TypeVar_kind:
CALL(astfold_expr, expr_ty, node_->v.TypeVar.name);
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
break;
case ParamSpec_kind:
CALL(astfold_expr, expr_ty, node_->v.ParamSpec.name);
break;
case TypeVarTuple_kind:
CALL(astfold_expr, expr_ty, node_->v.TypeVarTuple.name);
break;
}
return 1;
Expand Down

0 comments on commit 55d0fd9

Please sign in to comment.