@@ -169,6 +169,8 @@ class SemanticAnalyzer(NodeVisitor):
169
169
170
170
# Stack of functions being analyzed
171
171
function_stack = None # type: List[FuncItem]
172
+ # Stack of next available function type variable ids
173
+ next_function_tvar_id_stack = None # type: List[int]
172
174
173
175
# Status of postponing analysis of nested function bodies. By using this we
174
176
# can have mutually recursive nested functions. Values are FUNCTION_x
@@ -198,6 +200,7 @@ def __init__(self, lib_path: List[str], errors: Errors,
198
200
self .bound_tvars = None
199
201
self .tvar_stack = []
200
202
self .function_stack = []
203
+ self .next_function_tvar_id_stack = [- 1 ]
201
204
self .block_depth = [0 ]
202
205
self .loop_depth = 0
203
206
self .lib_path = lib_path
@@ -336,7 +339,9 @@ def update_function_type_variables(self, defn: FuncDef) -> None:
336
339
typevars = [(name , tvar ) for name , tvar in typevars
337
340
if not self .is_defined_type_var (name , defn )]
338
341
if typevars :
339
- defs = [TypeVarDef (tvar [0 ], - i - 1 , tvar [1 ].values , self .object_type (),
342
+ next_tvar_id = self .next_function_tvar_id ()
343
+ defs = [TypeVarDef (tvar [0 ], next_tvar_id - i ,
344
+ tvar [1 ].values , self .object_type (),
340
345
tvar [1 ].variance )
341
346
for i , tvar in enumerate (typevars )]
342
347
functype .variables = defs
@@ -431,9 +436,17 @@ def analyze_property_with_multi_part_definition(self, defn: OverloadedFuncDef) -
431
436
self .fail ("Decorated property not supported" , item )
432
437
item .func .accept (self )
433
438
439
+ def next_function_tvar_id (self ) -> int :
440
+ return self .next_function_tvar_id_stack [- 1 ]
441
+
434
442
def analyze_function (self , defn : FuncItem ) -> None :
435
443
is_method = self .is_class_scope ()
444
+
436
445
tvarnodes = self .add_func_type_variables_to_symbol_table (defn )
446
+ next_function_tvar_id = min ([self .next_function_tvar_id ()] +
447
+ [n .tvar_id - 1 for n in tvarnodes ])
448
+ self .next_function_tvar_id_stack .append (next_function_tvar_id )
449
+
437
450
if defn .type :
438
451
# Signature must be analyzed in the surrounding scope so that
439
452
# class-level imported names and type variables are in scope.
@@ -472,7 +485,9 @@ def analyze_function(self, defn: FuncItem) -> None:
472
485
self .postpone_nested_functions_stack .pop ()
473
486
self .postponed_functions_stack .pop ()
474
487
488
+ self .next_function_tvar_id_stack .pop ()
475
489
disable_typevars (tvarnodes )
490
+
476
491
self .leave ()
477
492
self .function_stack .pop ()
478
493
0 commit comments