@@ -126,6 +126,15 @@ def infer_constraints_for_callable(
126126 param_spec_arg_names = []
127127 param_spec_arg_kinds = []
128128
129+ incomplete_star_mapping = False
130+ for i , actuals in enumerate (formal_to_actual ): # TODO: isn't this `enumerate(arg_types)`?
131+ for actual in actuals :
132+ if actual is None and callee .arg_kinds [i ] in (ARG_STAR , ARG_STAR2 ): # type: ignore[unreachable]
133+ # We can't use arguments to infer ParamSpec constraint, if only some
134+ # are present in the current inference pass.
135+ incomplete_star_mapping = True # type: ignore[unreachable]
136+ break
137+
129138 for i , actuals in enumerate (formal_to_actual ):
130139 if isinstance (callee .arg_types [i ], UnpackType ):
131140 unpack_type = callee .arg_types [i ]
@@ -221,16 +230,17 @@ def infer_constraints_for_callable(
221230 # constraints, instead store them and infer single constraint at the end.
222231 # It is impossible to map actual kind to formal kind, so use some heuristic.
223232 # This inference is used as a fallback, so relying on heuristic should be OK.
224- param_spec_arg_types .append (
225- mapper .expand_actual_type (
226- actual_arg_type , arg_kinds [actual ], None , arg_kinds [actual ]
233+ if not incomplete_star_mapping :
234+ param_spec_arg_types .append (
235+ mapper .expand_actual_type (
236+ actual_arg_type , arg_kinds [actual ], None , arg_kinds [actual ]
237+ )
227238 )
228- )
229- actual_kind = arg_kinds [actual ]
230- param_spec_arg_kinds .append (
231- ARG_POS if actual_kind not in (ARG_STAR , ARG_STAR2 ) else actual_kind
232- )
233- param_spec_arg_names .append (arg_names [actual ] if arg_names else None )
239+ actual_kind = arg_kinds [actual ]
240+ param_spec_arg_kinds .append (
241+ ARG_POS if actual_kind not in (ARG_STAR , ARG_STAR2 ) else actual_kind
242+ )
243+ param_spec_arg_names .append (arg_names [actual ] if arg_names else None )
234244 else :
235245 actual_type = mapper .expand_actual_type (
236246 actual_arg_type ,
@@ -240,7 +250,11 @@ def infer_constraints_for_callable(
240250 )
241251 c = infer_constraints (callee .arg_types [i ], actual_type , SUPERTYPE_OF )
242252 constraints .extend (c )
243- if param_spec and not any (c .type_var == param_spec .id for c in constraints ):
253+ if (
254+ param_spec
255+ and not any (c .type_var == param_spec .id for c in constraints )
256+ and not incomplete_star_mapping
257+ ):
244258 # Use ParamSpec constraint from arguments only if there are no other constraints,
245259 # since as explained above it is quite ad-hoc.
246260 constraints .append (
0 commit comments