6
6
from mypy import nodes
7
7
8
8
9
- def map_actuals_to_formals (caller_kinds : List [int ],
10
- caller_names : Optional [Sequence [Optional [str ]]],
11
- callee_kinds : List [int ],
12
- callee_names : Sequence [Optional [str ]],
13
- caller_arg_type : Callable [[int ],
9
+ def map_actuals_to_formals (actual_kinds : List [int ],
10
+ actual_names : Optional [Sequence [Optional [str ]]],
11
+ formal_kinds : List [int ],
12
+ formal_names : Sequence [Optional [str ]],
13
+ actual_arg_type : Callable [[int ],
14
14
Type ]) -> List [List [int ]]:
15
15
"""Calculate mapping between actual (caller) args and formals.
16
16
@@ -20,86 +20,87 @@ def map_actuals_to_formals(caller_kinds: List[int],
20
20
The caller_arg_type argument should evaluate to the type of the actual
21
21
argument type with the given index.
22
22
"""
23
- ncallee = len (callee_kinds )
24
- map = [[] for i in range (ncallee )] # type: List[List[int]]
25
- j = 0
26
- for i , kind in enumerate (caller_kinds ):
27
- if kind == nodes .ARG_POS :
28
- if j < ncallee :
29
- if callee_kinds [ j ] in [nodes .ARG_POS , nodes .ARG_OPT ,
30
- nodes .ARG_NAMED , nodes .ARG_NAMED_OPT ]:
31
- map [ j ].append (i )
32
- j += 1
33
- elif callee_kinds [ j ] == nodes .ARG_STAR :
34
- map [ j ].append (i )
35
- elif kind == nodes .ARG_STAR :
23
+ nformals = len (formal_kinds )
24
+ formal_to_actual = [[] for i in range (nformals )] # type: List[List[int]]
25
+ fi = 0
26
+ for ai , actual_kind in enumerate (actual_kinds ):
27
+ if actual_kind == nodes .ARG_POS :
28
+ if fi < nformals :
29
+ if formal_kinds [ fi ] in [nodes .ARG_POS , nodes .ARG_OPT ,
30
+ nodes .ARG_NAMED , nodes .ARG_NAMED_OPT ]:
31
+ formal_to_actual [ fi ].append (ai )
32
+ fi += 1
33
+ elif formal_kinds [ fi ] == nodes .ARG_STAR :
34
+ formal_to_actual [ fi ].append (ai )
35
+ elif actual_kind == nodes .ARG_STAR :
36
36
# We need to know the actual type to map varargs.
37
- argt = caller_arg_type ( i )
38
- if isinstance (argt , TupleType ):
37
+ actualt = actual_arg_type ( ai )
38
+ if isinstance (actualt , TupleType ):
39
39
# A tuple actual maps to a fixed number of formals.
40
- for _ in range (len (argt .items )):
41
- if j < ncallee :
42
- if callee_kinds [ j ] != nodes .ARG_STAR2 :
43
- map [ j ].append (i )
40
+ for _ in range (len (actualt .items )):
41
+ if fi < nformals :
42
+ if formal_kinds [ fi ] != nodes .ARG_STAR2 :
43
+ formal_to_actual [ fi ].append (ai )
44
44
else :
45
45
break
46
- if callee_kinds [ j ] != nodes .ARG_STAR :
47
- j += 1
46
+ if formal_kinds [ fi ] != nodes .ARG_STAR :
47
+ fi += 1
48
48
else :
49
49
# Assume that it is an iterable (if it isn't, there will be
50
50
# an error later).
51
- while j < ncallee :
52
- if callee_kinds [ j ] in (nodes .ARG_NAMED , nodes .ARG_NAMED_OPT , nodes .ARG_STAR2 ):
51
+ while fi < nformals :
52
+ if formal_kinds [ fi ] in (nodes .ARG_NAMED , nodes .ARG_NAMED_OPT , nodes .ARG_STAR2 ):
53
53
break
54
54
else :
55
- map [ j ].append (i )
56
- if callee_kinds [ j ] == nodes .ARG_STAR :
55
+ formal_to_actual [ fi ].append (ai )
56
+ if formal_kinds [ fi ] == nodes .ARG_STAR :
57
57
break
58
- j += 1
59
- elif kind in (nodes .ARG_NAMED , nodes .ARG_NAMED_OPT ):
60
- assert caller_names is not None , "Internal error: named kinds without names given"
61
- name = caller_names [ i ]
62
- if name in callee_names :
63
- map [ callee_names .index (name )].append (i )
64
- elif nodes .ARG_STAR2 in callee_kinds :
65
- map [ callee_kinds .index (nodes .ARG_STAR2 )].append (i )
58
+ fi += 1
59
+ elif actual_kind in (nodes .ARG_NAMED , nodes .ARG_NAMED_OPT ):
60
+ assert actual_names is not None , "Internal error: named kinds without names given"
61
+ name = actual_names [ ai ]
62
+ if name in formal_names :
63
+ formal_to_actual [ formal_names .index (name )].append (ai )
64
+ elif nodes .ARG_STAR2 in formal_kinds :
65
+ formal_to_actual [ formal_kinds .index (nodes .ARG_STAR2 )].append (ai )
66
66
else :
67
- assert kind == nodes .ARG_STAR2
68
- argt = caller_arg_type ( i )
69
- if isinstance (argt , TypedDictType ):
70
- for name , value in argt .items .items ():
71
- if name in callee_names :
72
- map [ callee_names .index (name )].append (i )
73
- elif nodes .ARG_STAR2 in callee_kinds :
74
- map [ callee_kinds .index (nodes .ARG_STAR2 )].append (i )
67
+ assert actual_kind == nodes .ARG_STAR2
68
+ actualt = actual_arg_type ( ai )
69
+ if isinstance (actualt , TypedDictType ):
70
+ for name , value in actualt .items .items ():
71
+ if name in formal_names :
72
+ formal_to_actual [ formal_names .index (name )].append (ai )
73
+ elif nodes .ARG_STAR2 in formal_kinds :
74
+ formal_to_actual [ formal_kinds .index (nodes .ARG_STAR2 )].append (ai )
75
75
else :
76
76
# We don't exactly know which **kwargs are provided by the
77
77
# caller. Assume that they will fill the remaining arguments.
78
- for j in range (ncallee ):
78
+ for fi in range (nformals ):
79
79
# TODO: If there are also tuple varargs, we might be missing some potential
80
80
# matches if the tuple was short enough to not match everything.
81
81
no_certain_match = (
82
- not map [j ] or caller_kinds [map [j ][0 ]] == nodes .ARG_STAR )
83
- if ((callee_names [j ] and no_certain_match )
84
- or callee_kinds [j ] == nodes .ARG_STAR2 ):
85
- map [j ].append (i )
86
- return map
87
-
88
-
89
- def map_formals_to_actuals (caller_kinds : List [int ],
90
- caller_names : Optional [Sequence [Optional [str ]]],
91
- callee_kinds : List [int ],
92
- callee_names : List [Optional [str ]],
93
- caller_arg_type : Callable [[int ],
82
+ not formal_to_actual [fi ]
83
+ or actual_kinds [formal_to_actual [fi ][0 ]] == nodes .ARG_STAR )
84
+ if ((formal_names [fi ] and no_certain_match )
85
+ or formal_kinds [fi ] == nodes .ARG_STAR2 ):
86
+ formal_to_actual [fi ].append (ai )
87
+ return formal_to_actual
88
+
89
+
90
+ def map_formals_to_actuals (actual_kinds : List [int ],
91
+ actual_names : Optional [Sequence [Optional [str ]]],
92
+ formal_kinds : List [int ],
93
+ formal_names : List [Optional [str ]],
94
+ actual_arg_type : Callable [[int ],
94
95
Type ]) -> List [List [int ]]:
95
96
"""Calculate the reverse mapping of map_actuals_to_formals."""
96
- formal_to_actual = map_actuals_to_formals (caller_kinds ,
97
- caller_names ,
98
- callee_kinds ,
99
- callee_names ,
100
- caller_arg_type )
97
+ formal_to_actual = map_actuals_to_formals (actual_kinds ,
98
+ actual_names ,
99
+ formal_kinds ,
100
+ formal_names ,
101
+ actual_arg_type )
101
102
# Now reverse the mapping.
102
- actual_to_formal = [[] for _ in caller_kinds ] # type: List[List[int]]
103
+ actual_to_formal = [[] for _ in actual_kinds ] # type: List[List[int]]
103
104
for formal , actuals in enumerate (formal_to_actual ):
104
105
for actual in actuals :
105
106
actual_to_formal [actual ].append (formal )
0 commit comments