gh-119180: Add evaluate functions for type params and type aliases (#122212)

This commit is contained in:
Jelle Zijlstra 2024-07-27 10:24:10 -07:00 committed by GitHub
parent cbac8a3888
commit ae192262ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 385 additions and 159 deletions

View file

@ -260,6 +260,7 @@ static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
static int symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc);
static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);
static int symtable_add_def(struct symtable *st, PyObject *name, int flag, _Py_SourceLocation loc);
/* For debugging purposes only */
#if _PY_DUMP_SYMTABLE
@ -1388,6 +1389,16 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
return 0;
int result = symtable_enter_existing_block(st, ste);
Py_DECREF(ste);
if (block == AnnotationBlock || block == TypeVariableBlock || block == TypeAliasBlock) {
_Py_DECLARE_STR(format, ".format");
// We need to insert code that reads this "parameter" to the function.
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM, loc)) {
return 0;
}
if (!symtable_add_def(st, &_Py_STR(format), USE, loc)) {
return 0;
}
}
return result;
}
@ -2630,18 +2641,6 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
return 0;
}
}
_Py_DECLARE_STR(format, ".format");
// The generated __annotate__ function takes a single parameter with the
// internal name ".format".
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM,
LOCATION(annotation))) {
return 0;
}
if (!symtable_add_def(st, &_Py_STR(format), USE,
LOCATION(annotation))) {
return 0;
}
}
else {
if (!symtable_enter_existing_block(st, parent_ste->ste_annotation_block)) {
@ -2690,14 +2689,6 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
return 0;
}
}
_Py_DECLARE_STR(format, ".format");
// We need to insert code that reads this "parameter" to the function.
if (!symtable_add_def(st, &_Py_STR(format), DEF_PARAM, LOCATION(o))) {
return 0;
}
if (!symtable_add_def(st, &_Py_STR(format), USE, LOCATION(o))) {
return 0;
}
if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
return 0;
if (a->args && !symtable_visit_argannotations(st, a->args))