@@ -2191,6 +2191,24 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
21912191 VISIT_QUIT (st , 1 );
21922192}
21932193
2194+ static int
2195+ symtable_visit_type_param_check_reserved_name (struct symtable * st , type_param_ty tp , identifier name )
2196+ {
2197+ if (_PyUnicode_Equal (name , & _Py_ID (__classdict__ ))) {
2198+ PyObject * error_msg = PyUnicode_FromFormat ("reserved name '%U' cannot be "
2199+ "used for type parameter" , name );
2200+ PyErr_SetObject (PyExc_SyntaxError , error_msg );
2201+ Py_DECREF (error_msg );
2202+ PyErr_RangedSyntaxLocationObject (st -> st_filename ,
2203+ tp -> lineno ,
2204+ tp -> col_offset + 1 ,
2205+ tp -> end_lineno ,
2206+ tp -> end_col_offset + 1 );
2207+ return 0 ;
2208+ }
2209+ return 1 ;
2210+ }
2211+
21942212static int
21952213symtable_visit_type_param (struct symtable * st , type_param_ty tp )
21962214{
@@ -2201,6 +2219,8 @@ symtable_visit_type_param(struct symtable *st, type_param_ty tp)
22012219 }
22022220 switch (tp -> kind ) {
22032221 case TypeVar_kind :
2222+ if (!symtable_visit_type_param_check_reserved_name (st , tp , tp -> v .TypeVar .name ))
2223+ VISIT_QUIT (st , 0 );
22042224 if (!symtable_add_def (st , tp -> v .TypeVar .name , DEF_TYPE_PARAM | DEF_LOCAL , LOCATION (tp )))
22052225 VISIT_QUIT (st , 0 );
22062226 if (tp -> v .TypeVar .bound ) {
@@ -2219,10 +2239,14 @@ symtable_visit_type_param(struct symtable *st, type_param_ty tp)
22192239 }
22202240 break ;
22212241 case TypeVarTuple_kind :
2242+ if (!symtable_visit_type_param_check_reserved_name (st , tp , tp -> v .TypeVarTuple .name ))
2243+ VISIT_QUIT (st , 0 );
22222244 if (!symtable_add_def (st , tp -> v .TypeVarTuple .name , DEF_TYPE_PARAM | DEF_LOCAL , LOCATION (tp )))
22232245 VISIT_QUIT (st , 0 );
22242246 break ;
22252247 case ParamSpec_kind :
2248+ if (!symtable_visit_type_param_check_reserved_name (st , tp , tp -> v .ParamSpec .name ))
2249+ VISIT_QUIT (st , 0 );
22262250 if (!symtable_add_def (st , tp -> v .ParamSpec .name , DEF_TYPE_PARAM | DEF_LOCAL , LOCATION (tp )))
22272251 VISIT_QUIT (st , 0 );
22282252 break ;
0 commit comments