@@ -196,6 +196,7 @@ enum {
196
196
COMPILER_SCOPE_ASYNC_FUNCTION ,
197
197
COMPILER_SCOPE_LAMBDA ,
198
198
COMPILER_SCOPE_COMPREHENSION ,
199
+ COMPILER_SCOPE_TYPEPARAMS ,
199
200
};
200
201
201
202
typedef struct {
@@ -780,6 +781,19 @@ compiler_set_qualname(struct compiler *c)
780
781
capsule = PyList_GET_ITEM (c -> c_stack , stack_size - 1 );
781
782
parent = (struct compiler_unit * )PyCapsule_GetPointer (capsule , CAPSULE_NAME );
782
783
assert (parent );
784
+ if (parent -> u_scope_type == COMPILER_SCOPE_TYPEPARAMS ) {
785
+ /* The parent is a type parameter scope, so we need to
786
+ look at the grandparent. */
787
+ if (stack_size == 2 ) {
788
+ // If we're immediately within the module, we can skip
789
+ // the rest and just set the qualname to be the same as name.
790
+ u -> u_qualname = Py_NewRef (u -> u_name );
791
+ return SUCCESS ;
792
+ }
793
+ capsule = PyList_GET_ITEM (c -> c_stack , stack_size - 2 );
794
+ parent = (struct compiler_unit * )PyCapsule_GetPointer (capsule , CAPSULE_NAME );
795
+ assert (parent );
796
+ }
783
797
784
798
if (u -> u_scope_type == COMPILER_SCOPE_FUNCTION
785
799
|| u -> u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
@@ -2242,7 +2256,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2242
2256
if (!typeparams_name ) {
2243
2257
return ERROR ;
2244
2258
}
2245
- if (compiler_enter_scope (c , typeparams_name , COMPILER_SCOPE_FUNCTION ,
2259
+ if (compiler_enter_scope (c , typeparams_name , COMPILER_SCOPE_TYPEPARAMS ,
2246
2260
(void * )typeparams , firstlineno ) == -1 ) {
2247
2261
Py_DECREF (typeparams_name );
2248
2262
return ERROR ;
@@ -2355,7 +2369,7 @@ compiler_class(struct compiler *c, stmt_ty s)
2355
2369
if (!typeparams_name ) {
2356
2370
return ERROR ;
2357
2371
}
2358
- if (compiler_enter_scope (c , typeparams_name , COMPILER_SCOPE_FUNCTION ,
2372
+ if (compiler_enter_scope (c , typeparams_name , COMPILER_SCOPE_TYPEPARAMS ,
2359
2373
(void * )typeparams , firstlineno ) == -1 ) {
2360
2374
Py_DECREF (typeparams_name );
2361
2375
return ERROR ;
0 commit comments