Skip to content

Commit b602ecc

Browse files
rwbartongvanrossum
authored andcommitted
Make argument types in function type inference explicitly Optional (#1392)
The old version happened to work because an argument would only be omitted when the corresponding formal argument has a Callable type, and in that case constraint generation does isinstance checks on the actual argument type. But calling infer_constraints with actual=None is not really good. (I noticed this while experimenting with moving some Union argument types to the second pass of function type inference.)
1 parent db4d2e1 commit b602ecc

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
405405
arg_pass_nums = self.get_arg_infer_passes(
406406
callee_type.arg_types, formal_to_actual, len(args))
407407

408-
pass1_args = [] # type: List[Type]
408+
pass1_args = [] # type: List[Optional[Type]]
409409
for i, arg in enumerate(arg_types):
410410
if arg_pass_nums[i] > 1:
411411
pass1_args.append(None)

mypy/constraints.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Type inference constraints."""
22

3-
from typing import List, cast
3+
from typing import List, Optional, cast
44

55
from mypy.types import (
66
CallableType, Type, TypeVisitor, UnboundType, AnyType, Void, NoneTyp, TypeVarType,
@@ -39,7 +39,7 @@ def __repr__(self) -> str:
3939

4040

4141
def infer_constraints_for_callable(
42-
callee: CallableType, arg_types: List[Type], arg_kinds: List[int],
42+
callee: CallableType, arg_types: List[Optional[Type]], arg_kinds: List[int],
4343
formal_to_actual: List[List[int]]) -> List[Constraint]:
4444
"""Infer type variable constraints for a callable and actual arguments.
4545
@@ -51,6 +51,9 @@ def infer_constraints_for_callable(
5151

5252
for i, actuals in enumerate(formal_to_actual):
5353
for actual in actuals:
54+
if arg_types[actual] is None:
55+
continue
56+
5457
actual_type = get_actual_type(arg_types[actual], arg_kinds[actual],
5558
tuple_counter)
5659
c = infer_constraints(callee.arg_types[i], actual_type,

mypy/infer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Utilities for type argument inference."""
22

3-
from typing import List
3+
from typing import List, Optional
44

55
from mypy.constraints import infer_constraints, infer_constraints_for_callable
66
from mypy.types import Type, CallableType
@@ -9,7 +9,7 @@
99

1010

1111
def infer_function_type_arguments(callee_type: CallableType,
12-
arg_types: List[Type],
12+
arg_types: List[Optional[Type]],
1313
arg_kinds: List[int],
1414
formal_to_actual: List[List[int]],
1515
strict: bool = True) -> List[Type]:
@@ -21,7 +21,8 @@ def infer_function_type_arguments(callee_type: CallableType,
2121
2222
Arguments:
2323
callee_type: the target generic function
24-
arg_types: argument types at the call site
24+
arg_types: argument types at the call site (each optional; if None,
25+
we are not considering this argument in the current pass)
2526
arg_kinds: nodes.ARG_* values for arg_types
2627
formal_to_actual: mapping from formal to actual variable indices
2728
"""

0 commit comments

Comments
 (0)