Fix crash when expanding invalid Unpack in a `Callable` alias by hamdanal · Pull Request #17028 · python/mypy
Expand Up
@@ -26,6 +26,7 @@
Type,
TypeAliasType,
TypedDictType,
TypeOfAny,
TypeType,
TypeVarId,
TypeVarLikeType,
Expand Down
Expand Up
@@ -312,24 +313,26 @@ def interpolate_args_for_unpack(self, t: CallableType, var_arg: UnpackType) -> l
suffix = self.expand_types(t.arg_types[star_index + 1 :])
var_arg_type = get_proper_type(var_arg.type) new_unpack: Type if isinstance(var_arg_type, Instance): # we have something like Unpack[Tuple[Any, ...]] new_unpack = var_arg else: if isinstance(var_arg_type, TupleType): # We have something like Unpack[Tuple[Unpack[Ts], X1, X2]] expanded_tuple = var_arg_type.accept(self) assert isinstance(expanded_tuple, ProperType) and isinstance( expanded_tuple, TupleType ) expanded_items = expanded_tuple.items fallback = var_arg_type.partial_fallback else: # We have plain Unpack[Ts] assert isinstance(var_arg_type, TypeVarTupleType), type(var_arg_type) fallback = var_arg_type.tuple_fallback expanded_items = self.expand_unpack(var_arg) elif isinstance(var_arg_type, TupleType): # We have something like Unpack[Tuple[Unpack[Ts], X1, X2]] expanded_tuple = var_arg_type.accept(self) assert isinstance(expanded_tuple, ProperType) and isinstance(expanded_tuple, TupleType) expanded_items = expanded_tuple.items fallback = var_arg_type.partial_fallback new_unpack = UnpackType(TupleType(expanded_items, fallback)) elif isinstance(var_arg_type, TypeVarTupleType): # We have plain Unpack[Ts] fallback = var_arg_type.tuple_fallback expanded_items = self.expand_unpack(var_arg) new_unpack = UnpackType(TupleType(expanded_items, fallback)) else: # We have invalid type in Unpack. This can happen when expanding aliases # to Callable[[*Invalid], Ret] new_unpack = AnyType(TypeOfAny.from_error, line=var_arg.line, column=var_arg.column) return prefix + [new_unpack] + suffix
def visit_callable_type(self, t: CallableType) -> CallableType: Expand Down
var_arg_type = get_proper_type(var_arg.type) new_unpack: Type if isinstance(var_arg_type, Instance): # we have something like Unpack[Tuple[Any, ...]] new_unpack = var_arg else: if isinstance(var_arg_type, TupleType): # We have something like Unpack[Tuple[Unpack[Ts], X1, X2]] expanded_tuple = var_arg_type.accept(self) assert isinstance(expanded_tuple, ProperType) and isinstance( expanded_tuple, TupleType ) expanded_items = expanded_tuple.items fallback = var_arg_type.partial_fallback else: # We have plain Unpack[Ts] assert isinstance(var_arg_type, TypeVarTupleType), type(var_arg_type) fallback = var_arg_type.tuple_fallback expanded_items = self.expand_unpack(var_arg) elif isinstance(var_arg_type, TupleType): # We have something like Unpack[Tuple[Unpack[Ts], X1, X2]] expanded_tuple = var_arg_type.accept(self) assert isinstance(expanded_tuple, ProperType) and isinstance(expanded_tuple, TupleType) expanded_items = expanded_tuple.items fallback = var_arg_type.partial_fallback new_unpack = UnpackType(TupleType(expanded_items, fallback)) elif isinstance(var_arg_type, TypeVarTupleType): # We have plain Unpack[Ts] fallback = var_arg_type.tuple_fallback expanded_items = self.expand_unpack(var_arg) new_unpack = UnpackType(TupleType(expanded_items, fallback)) else: # We have invalid type in Unpack. This can happen when expanding aliases # to Callable[[*Invalid], Ret] new_unpack = AnyType(TypeOfAny.from_error, line=var_arg.line, column=var_arg.column) return prefix + [new_unpack] + suffix
def visit_callable_type(self, t: CallableType) -> CallableType: Expand Down