@@ -1606,7 +1606,7 @@ def check_callable_call(
1606
1606
lambda i : self .accept (args [i ]),
1607
1607
)
1608
1608
callee = self .infer_function_type_arguments (
1609
- callee , args , arg_kinds , formal_to_actual , context
1609
+ callee , args , arg_kinds , arg_names , formal_to_actual , need_refresh , context
1610
1610
)
1611
1611
if need_refresh :
1612
1612
formal_to_actual = map_actuals_to_formals (
@@ -1896,7 +1896,9 @@ def infer_function_type_arguments(
1896
1896
callee_type : CallableType ,
1897
1897
args : list [Expression ],
1898
1898
arg_kinds : list [ArgKind ],
1899
+ arg_names : Sequence [str | None ] | None ,
1899
1900
formal_to_actual : list [list [int ]],
1901
+ need_refresh : bool ,
1900
1902
context : Context ,
1901
1903
) -> CallableType :
1902
1904
"""Infer the type arguments for a generic callee type.
@@ -1938,7 +1940,14 @@ def infer_function_type_arguments(
1938
1940
if 2 in arg_pass_nums :
1939
1941
# Second pass of type inference.
1940
1942
(callee_type , inferred_args ) = self .infer_function_type_arguments_pass2 (
1941
- callee_type , args , arg_kinds , formal_to_actual , inferred_args , context
1943
+ callee_type ,
1944
+ args ,
1945
+ arg_kinds ,
1946
+ arg_names ,
1947
+ formal_to_actual ,
1948
+ inferred_args ,
1949
+ need_refresh ,
1950
+ context ,
1942
1951
)
1943
1952
1944
1953
if (
@@ -1964,6 +1973,17 @@ def infer_function_type_arguments(
1964
1973
or set (get_type_vars (a )) & set (callee_type .variables )
1965
1974
for a in inferred_args
1966
1975
):
1976
+ if need_refresh :
1977
+ # Technically we need to refresh formal_to_actual after *each* inference pass,
1978
+ # since each pass can expand ParamSpec or TypeVarTuple. Although such situations
1979
+ # are very rare, not doing this can cause crashes.
1980
+ formal_to_actual = map_actuals_to_formals (
1981
+ arg_kinds ,
1982
+ arg_names ,
1983
+ callee_type .arg_kinds ,
1984
+ callee_type .arg_names ,
1985
+ lambda a : self .accept (args [a ]),
1986
+ )
1967
1987
# If the regular two-phase inference didn't work, try inferring type
1968
1988
# variables while allowing for polymorphic solutions, i.e. for solutions
1969
1989
# potentially involving free variables.
@@ -2011,8 +2031,10 @@ def infer_function_type_arguments_pass2(
2011
2031
callee_type : CallableType ,
2012
2032
args : list [Expression ],
2013
2033
arg_kinds : list [ArgKind ],
2034
+ arg_names : Sequence [str | None ] | None ,
2014
2035
formal_to_actual : list [list [int ]],
2015
2036
old_inferred_args : Sequence [Type | None ],
2037
+ need_refresh : bool ,
2016
2038
context : Context ,
2017
2039
) -> tuple [CallableType , list [Type | None ]]:
2018
2040
"""Perform second pass of generic function type argument inference.
@@ -2034,6 +2056,14 @@ def infer_function_type_arguments_pass2(
2034
2056
if isinstance (arg , (NoneType , UninhabitedType )) or has_erased_component (arg ):
2035
2057
inferred_args [i ] = None
2036
2058
callee_type = self .apply_generic_arguments (callee_type , inferred_args , context )
2059
+ if need_refresh :
2060
+ formal_to_actual = map_actuals_to_formals (
2061
+ arg_kinds ,
2062
+ arg_names ,
2063
+ callee_type .arg_kinds ,
2064
+ callee_type .arg_names ,
2065
+ lambda a : self .accept (args [a ]),
2066
+ )
2037
2067
2038
2068
arg_types = self .infer_arg_types_in_context (callee_type , args , arg_kinds , formal_to_actual )
2039
2069
0 commit comments