Skip to content

Commit 3cfd8e4

Browse files
committed
Cannot have non-default after default
1 parent 3fd5a77 commit 3cfd8e4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Python/compile.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,7 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
21232123
return SUCCESS;
21242124
}
21252125
Py_ssize_t n = asdl_seq_LEN(typeparams);
2126+
bool seen_default = false;
21262127

21272128
for (Py_ssize_t i = 0; i < n; i++) {
21282129
typeparam_ty typeparam = asdl_seq_GET(typeparams, i);
@@ -2145,12 +2146,18 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
21452146
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVAR);
21462147
}
21472148
if (typeparam->v.TypeVar.default_) {
2149+
seen_default = true;
21482150
expr_ty default_ = typeparam->v.TypeVar.default_;
21492151
if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVar.name) < 0) {
21502152
return ERROR;
21512153
}
21522154
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
21532155
}
2156+
else if (seen_default) {
2157+
return compiler_error(c, loc, "non-default type parameter '%U' "
2158+
"follows default type parameter",
2159+
typeparam->v.TypeVar.name);
2160+
}
21542161
ADDOP_I(c, loc, COPY, 1);
21552162
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store));
21562163
break;
@@ -2163,6 +2170,12 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
21632170
return ERROR;
21642171
}
21652172
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
2173+
seen_default = true;
2174+
}
2175+
else if (seen_default) {
2176+
return compiler_error(c, loc, "non-default type parameter '%U' "
2177+
"follows default type parameter",
2178+
typeparam->v.TypeVarTuple.name);
21662179
}
21672180
ADDOP_I(c, loc, COPY, 1);
21682181
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
@@ -2176,6 +2189,12 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
21762189
return ERROR;
21772190
}
21782191
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
2192+
seen_default = true;
2193+
}
2194+
else if (seen_default) {
2195+
return compiler_error(c, loc, "non-default type parameter '%U' "
2196+
"follows default type parameter",
2197+
typeparam->v.ParamSpec.name);
21792198
}
21802199
ADDOP_I(c, loc, COPY, 1);
21812200
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store));

0 commit comments

Comments
 (0)