Skip to content

Commit

Permalink
Do it in the AST validator instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 12, 2023
1 parent 8a459c7 commit f6c10f5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def test_compile_invalid_typealias(self):
type_ignores=[],
)

with self.assertRaises(SyntaxError):
with self.assertRaisesRegex(TypeError, "TypeAlias with non-Name name"):
compile(ast.fix_missing_locations(m), "<file>", "exec")

def test_dict_evaluation_order(self):
Expand Down
5 changes: 5 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,11 @@ validate_stmt(struct validator *state, stmt_ty stmt)
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
break;
case TypeAlias_kind:
if (stmt->v.TypeAlias.name->kind != Name_kind) {
PyErr_SetString(PyExc_TypeError,
"TypeAlias with non-Name name");
return 0;
}
ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
validate_type_params(state, stmt->v.TypeAlias.type_params) &&
validate_expr(state, stmt->v.TypeAlias.value, Load);
Expand Down
7 changes: 1 addition & 6 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1581,12 +1581,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
}
case TypeAlias_kind: {
VISIT(st, expr, s->v.TypeAlias.name);
// This is only possible for a manually created AST
if (s->v.TypeAlias.name->kind != Name_kind) {
PyErr_SetString(PyExc_SyntaxError,
"Type alias name must be a simple name");
VISIT_QUIT(st, 0);
}
assert(s->v.TypeAlias.name->kind == Name_kind);
PyObject *name = s->v.TypeAlias.name->v.Name.id;
int is_in_class = st->st_cur->ste_type == ClassBlock;
int is_generic = asdl_seq_LEN(s->v.TypeAlias.type_params) > 0;
Expand Down

0 comments on commit f6c10f5

Please sign in to comment.