Skip to content

Commit 8bbfef1

Browse files
JukkaLmsullivan
authored andcommitted
Fix callable types with inconsistent argument counts (#4611)
This fixes potential issues with callable types where there are a different number of argument names compared to argument types. These malformed callable types might have caused crashes. This may address #4599. Even if the issue remains unfixed, future tracebacks could be more useful due to an assert that I added.
1 parent 76774fa commit 8bbfef1

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
TupleType, TypedDictType, Instance, TypeVarType, ErasedType, UnionType,
1111
PartialType, DeletedType, UnboundType, UninhabitedType, TypeType, TypeOfAny,
1212
true_only, false_only, is_named_instance, function_type, callable_type, FunctionLike,
13-
get_typ_args, set_typ_args,
14-
StarType)
13+
get_typ_args, StarType
14+
)
1515
from mypy.nodes import (
1616
NameExpr, RefExpr, Var, FuncDef, OverloadedFuncDef, TypeInfo, CallExpr,
1717
MemberExpr, IntExpr, StrExpr, BytesExpr, UnicodeExpr, FloatExpr,
@@ -2088,7 +2088,8 @@ def infer_lambda_type_using_context(self, e: LambdaExpr) -> Tuple[Optional[Calla
20882088
callable_ctx = callable_ctx.copy_modified(
20892089
is_ellipsis_args=False,
20902090
arg_types=[AnyType(TypeOfAny.special_form)] * len(arg_kinds),
2091-
arg_kinds=arg_kinds
2091+
arg_kinds=arg_kinds,
2092+
arg_names=[None] * len(arg_kinds)
20922093
)
20932094

20942095
if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,6 +3966,8 @@ def accept(self, node: Node) -> None:
39663966

39673967
def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
39683968
if isinstance(sig, CallableType):
3969+
if len(sig.arg_types) == 0:
3970+
return sig
39693971
return sig.copy_modified(arg_types=[new] + sig.arg_types[1:])
39703972
elif isinstance(sig, Overloaded):
39713973
return Overloaded([cast(CallableType, replace_implicit_first_type(i, new))

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ def __init__(self,
663663
from_type_type: bool = False,
664664
bound_args: Optional[List[Optional[Type]]] = None,
665665
) -> None:
666+
assert len(arg_types) == len(arg_kinds) == len(arg_names)
666667
if variables is None:
667668
variables = []
668669
assert len(arg_types) == len(arg_kinds)

0 commit comments

Comments
 (0)