10000 gh-116126: Implement PEP 696 by JelleZijlstra · Pull Request #116129 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116126: Implement PEP 696 #116129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 56 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e7ca092
gh-116126: Grammar changes for PEP 696
JelleZijlstra Feb 29, 2024
828679e
Fix test_ast
JelleZijlstra Feb 29, 2024
d3b472b
new tests
JelleZijlstra Feb 29, 2024
6e467ee
fix test_unparse
JelleZijlstra Feb 29, 2024
fa48580
possible solution
JelleZijlstra Mar 1, 2024
49b077b
Can't use the name either
JelleZijlstra Mar 1, 2024
5cfd5e2
blurb
JelleZijlstra Mar 1, 2024
01b3270
should not have pushed that
JelleZijlstra Mar 1, 2024
7a69204
maybe this will compile
JelleZijlstra Mar 1, 2024
a265008
Feature version support
JelleZijlstra Mar 1, 2024
0c28297
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 10, 2024
8aa5b27
default_ -> default_value
JelleZijlstra Apr 10, 2024
bdd01bb
ast docs
JelleZijlstra Apr 10, 2024
2c28b9a
more docs
JelleZijlstra Apr 10, 2024
b0d467f
Fix typevarobject.c
JelleZijlstra Apr 10, 2024
d7e91e2
default=<unrepresentable>
JelleZijlstra Apr 10, 2024
35226b4
NoneType is immortal
JelleZijlstra Apr 10, 2024
2e079ee
Runtime implementation
JelleZijlstra Apr 10, 2024
e037052
What's New
JelleZijlstra Apr 10, 2024
7b15f11
Document None/NoneType weirdness
JelleZijlstra Apr 10, 2024
11bd102
fix doctest
JelleZijlstra Apr 11, 2024
c54aeed
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
71a348b
Feedback from Alex
JelleZijlstra Apr 23, 2024
900cd40
Partial work
JelleZijlstra Apr 23, 2024
c13420b
Allow * in TypeVarTuple default
JelleZijlstra Apr 23, 2024
8a08c62
Test and fix grammar
JelleZijlstra Apr 23, 2024
613159f
Fix TypeVarTuple substitution
JelleZijlstra Apr 23, 2024
4e0435e
Two more cases
JelleZijlstra Apr 23, 2024
7d54ace
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
12581d8
Fix TypeVar substitution
JelleZijlstra Apr 23, 2024
2b5a102
Fix ParamSpec
JelleZijlstra Apr 23, 2024
a2c48c9
Prohibit default after TypeVarTuple
JelleZijlstra Apr 23, 2024
29b9435
Wrap new syntax in run_code()
JelleZijlstra Apr 23, 2024
326de17
Silence Ruff
JelleZijlstra Apr 23, 2024
6f66775
Fix parameter markup
JelleZijlstra Apr 23, 2024
7efa4ce
default_ -> default_value
JelleZijlstra Apr 23, 2024
29ad843
Fix errors
JelleZijlstra Apr 23, 2024
2c04c14
arguments, not parameters
JelleZijlstra Apr 23, 2024
7d4f3fd
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 23, 2024
1a14367
Apply suggestions from code review
JelleZijlstra Apr 23, 2024
5890d79
regen clinic
JelleZijlstra Apr 23, 2024
19a7b48
variadic args for _unpack_args
JelleZijlstra Apr 23, 2024
ef0616b
Update Lib/typing.py
JelleZijlstra Apr 23, 2024
9d4e842
Update Doc/reference/executionmodel.rst
JelleZijlstra Apr 23, 2024
2686b97
Roundtrip tests
JelleZijlstra Apr 24, 2024
98d1dec
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 28, 2024
fbb6405
Fix doctest
JelleZijlstra Apr 28, 2024
e0ccfeb
Add typing.NoDefault as the default for TypeVar's default
JelleZijlstra Apr 28, 2024
15aaff9
Apply suggestions from code review
JelleZijlstra Apr 28, 2024
86f5aed
Add .has_default() and update docs
JelleZijlstra Apr 28, 2024
3e0c0fc
Apply suggestions from code review
JelleZijlstra Apr 28, 2024
f5a3d4b
remove trailing space
JelleZijlstra Apr 28, 2024
92ea108
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra Apr 30, 2024
5f6fdfd
Ignore new C globals
JelleZijlstra Apr 30, 2024
b3f053c
Merge remote-tracking branch 'upstream/main' into pep696v2
JelleZijlstra May 3, 2024
8c3e0b4
Fix scoping key
JelleZijlstra May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
possible solution
  • Loading branch information
JelleZijlstra committed Mar 1, 2024
commit fa48580c2c17f69bb9c7641d7899501d91e80561
8 changes: 8 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ def test_comprehension_02(self):
func, = T.__bound__
self.assertEqual(func(), 1)

def test_comprehension_03(self):
def F[T: [lambda: T for T in (T, [1])[1]]](): return [lambda: T for T in T.__name__]
func, = F()
self.assertEqual(func(), "T")
T, = F.__type_params__
func, = T.__bound__
self.assertEqual(func(), 1)

def test_gen_exp_in_nested_class(self):
code = """
from test.test_type_params import make_base
Expand Down
17 changes: 11 additions & 6 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2180,10 +2180,11 @@ wrap_in_stopiteration_handler(struct compiler *c)
}

static int
compiler_type_param_bound_or_default(struct compiler *c, expr_ty e, identifier name)
compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
identifier name, void *key)
{
if (compiler_enter_scope(c, name, COMPILER_SCOPE_TYPEPARAMS,
(void *)e, e->lineno) == -1) {
key, e->lineno) == -1) {
return ERROR;
}
VISIT(c, expr, e);
Expand Down Expand Up @@ -2218,7 +2219,8 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVar.name);
if (typeparam->v.TypeVar.bound) {
expr_ty bound = typeparam->v.TypeVar.bound;
if (compiler_type_param_bound_or_default(c, bound, typeparam->v.TypeVar.name) < 0) {
if (compiler_type_param_bound_or_default(c, bound, typeparam->v.TypeVar.name,
(void *)typeparam) < 0) {
return ERROR;
}

Expand All @@ -2233,7 +2235,8 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
if (typeparam->v.TypeVar.default_) {
seen_default = true;
expr_ty default_ = typeparam->v.TypeVar.default_;
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVar.name) < 0) {
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVar.name,
(void *)typeparam->v.TypeVar.name) < 0) {
return ERROR;
}
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
Expand All @@ -2251,7 +2254,8 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVARTUPLE);
if (typeparam->v.TypeVarTuple.default_) {
expr_ty default_ = typeparam->v.TypeVarTuple.default_;
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVarTuple.name) < 0) {
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVarTuple.name,
(void *)typeparam) < 0) {
return ERROR;
}
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
Expand All @@ -2270,7 +2274,8 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PARAMSPEC);
if (typeparam->v.ParamSpec.default_) {
expr_ty default_ = typeparam->v.ParamSpec.default_;
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.ParamSpec.name) < 0) {
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.ParamSpec.name,
(void *)typeparam) < 0) {
return ERROR;
}
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
Expand Down
25 changes: 17 additions & 8 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,13 +2285,11 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
}

static int
symtable_visit_type_param_bound_or_default(struct symtable *st, expr_ty e, identifier name)
symtable_visit_type_param_bound_or_default(struct symtable *st, expr_ty e, identifier name, void *key)
{
if (e) {
int is_in_class = st->st_cur->ste_can_see_class_scope;
if (!symtable_enter_block(st, name,
TypeVarBoundBlock, (void *)e,
LOCATION(e)))
if (!symtable_enter_block(st, name, TypeVarBoundBlock, key, LOCATION(e)))
return 0;
st->st_cur->ste_can_see_class_scope = is_in_class;
if (is_in_class && !symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(e))) {
Expand All @@ -2316,21 +2314,32 @@ symtable_visit_type_param(struct symtable *st, type_param_ty tp)
case TypeVar_kind:
if (!symtable_add_def(st, tp->v.TypeVar.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
VISIT_QUIT(st, 0);
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.bound, tp->v.TypeVar.name))

// We must use a different key for the bound and default. The obvious choice would be to
// use the .bound and .default_ pointers, but that fails when the expression immediately
// inside the bound or default is a comprehension: we would reuse the same key for
// the comprehension scope. Therefore, use the name of the type parameter as the second key.
// The only requirement for the key is that it is unique and it matches the logic in
// compile.c where the scope is retrieved.
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.bound, tp->v.TypeVar.name,
(void *)tp))
VISIT_QUIT(st, 0);
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.default_, tp->v.TypeVar.name))
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.default_, tp->v.TypeVar.name,
(void *)tp->v.TypeVar.name))
VISIT_QUIT(st, 0);
break;
case TypeVarTuple_kind:
if (!symtable_add_def(st, tp->v.TypeVarTuple.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
VISIT_QUIT(st, 0);
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVarTuple.default_, tp->v.TypeVarTuple.name))
if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVarTuple.default_, tp->v.TypeVarTuple.name,
(void *)tp))
VISIT_QUIT(st, 0);
break;
case ParamSpec_kind:
if (!symtable_add_def(st, tp->v.ParamSpec.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
VISIT_QUIT(st, 0);
if (!symtable_visit_type_param_bound_or_default(st, tp->v.ParamSpec.default_, tp->v.ParamSpec.name))
if (!symtable_visit_type_param_bound_or_default(st, tp->v.ParamSpec.default_, tp->v.ParamSpec.name,
(void *)tp))
VISIT_QUIT(st, 0);
break;
}
Expand Down
0