Skip to content

Commit 18b3cbd

Browse files
authored
Refactor argument mapping (#6093)
This only does some variable renamings, mainly to make things more consistent. We know use actuals/formals instead of caller/callee, as the latter look too similar.
1 parent 833517d commit 18b3cbd

File tree

1 file changed

+66
-65
lines changed

1 file changed

+66
-65
lines changed

mypy/argmap.py

+66-65
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from mypy import nodes
77

88

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],
1414
Type]) -> List[List[int]]:
1515
"""Calculate mapping between actual (caller) args and formals.
1616
@@ -20,86 +20,87 @@ def map_actuals_to_formals(caller_kinds: List[int],
2020
The caller_arg_type argument should evaluate to the type of the actual
2121
argument type with the given index.
2222
"""
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:
3636
# 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):
3939
# 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)
4444
else:
4545
break
46-
if callee_kinds[j] != nodes.ARG_STAR:
47-
j += 1
46+
if formal_kinds[fi] != nodes.ARG_STAR:
47+
fi += 1
4848
else:
4949
# Assume that it is an iterable (if it isn't, there will be
5050
# 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):
5353
break
5454
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:
5757
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)
6666
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)
7575
else:
7676
# We don't exactly know which **kwargs are provided by the
7777
# caller. Assume that they will fill the remaining arguments.
78-
for j in range(ncallee):
78+
for fi in range(nformals):
7979
# TODO: If there are also tuple varargs, we might be missing some potential
8080
# matches if the tuple was short enough to not match everything.
8181
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],
9495
Type]) -> List[List[int]]:
9596
"""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)
101102
# 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]]
103104
for formal, actuals in enumerate(formal_to_actual):
104105
for actual in actuals:
105106
actual_to_formal[actual].append(formal)

0 commit comments

Comments
 (0)