Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3552971

Browse files
authored
Make ARG_* into an actual enum class instead of just using int everywhere (#10789)
This is cleaner and more precise and will allow us to add convenience methods to the class. (I added one convenience method so far, to verify that mypyc doesn't choke on it...)
1 parent ebc4f10 commit 3552971

25 files changed

Lines changed: 154 additions & 137 deletions

mypy/argmap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from mypy import nodes
99

1010

11-
def map_actuals_to_formals(actual_kinds: List[int],
11+
def map_actuals_to_formals(actual_kinds: List[nodes.ArgKind],
1212
actual_names: Optional[Sequence[Optional[str]]],
13-
formal_kinds: List[int],
13+
formal_kinds: List[nodes.ArgKind],
1414
formal_names: Sequence[Optional[str]],
1515
actual_arg_type: Callable[[int],
1616
Type]) -> List[List[int]]:
@@ -99,9 +99,9 @@ def map_actuals_to_formals(actual_kinds: List[int],
9999
return formal_to_actual
100100

101101

102-
def map_formals_to_actuals(actual_kinds: List[int],
102+
def map_formals_to_actuals(actual_kinds: List[nodes.ArgKind],
103103
actual_names: Optional[Sequence[Optional[str]]],
104-
formal_kinds: List[int],
104+
formal_kinds: List[nodes.ArgKind],
105105
formal_names: List[Optional[str]],
106106
actual_arg_type: Callable[[int],
107107
Type]) -> List[List[int]]:
@@ -149,9 +149,9 @@ def __init__(self) -> None:
149149

150150
def expand_actual_type(self,
151151
actual_type: Type,
152-
actual_kind: int,
152+
actual_kind: nodes.ArgKind,
153153
formal_name: Optional[str],
154-
formal_kind: int) -> Type:
154+
formal_kind: nodes.ArgKind) -> Type:
155155
"""Return the actual (caller) type(s) of a formal argument with the given kinds.
156156
157157
If the actual argument is a tuple *args, return the next individual tuple item that

mypy/checkexpr.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
3333
TypeAliasExpr, BackquoteExpr, EnumCallExpr, TypeAlias, SymbolNode, PlaceholderNode,
3434
ParamSpecExpr,
35-
ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
35+
ArgKind, ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
3636
)
3737
from mypy.literals import literal
3838
from mypy import nodes
@@ -73,7 +73,7 @@
7373
# check_args() below for details.
7474
ArgChecker = Callable[[Type,
7575
Type,
76-
int,
76+
ArgKind,
7777
Type,
7878
int,
7979
int,
@@ -476,7 +476,7 @@ def check_protocol_issubclass(self, e: CallExpr) -> None:
476476
attr_members, e)
477477

478478
def check_typeddict_call(self, callee: TypedDictType,
479-
arg_kinds: List[int],
479+
arg_kinds: List[ArgKind],
480480
arg_names: Sequence[Optional[str]],
481481
args: List[Expression],
482482
context: Context) -> Type:
@@ -697,7 +697,7 @@ def try_infer_partial_value_type_from_call(
697697

698698
def apply_function_plugin(self,
699699
callee: CallableType,
700-
arg_kinds: List[int],
700+
arg_kinds: List[ArgKind],
701701
arg_types: List[Type],
702702
arg_names: Optional[Sequence[Optional[str]]],
703703
formal_to_actual: List[List[int]],
@@ -720,7 +720,7 @@ def apply_function_plugin(self,
720720
formal_arg_types: List[List[Type]] = [[] for _ in range(num_formals)]
721721
formal_arg_exprs: List[List[Expression]] = [[] for _ in range(num_formals)]
722722
formal_arg_names: List[List[Optional[str]]] = [[] for _ in range(num_formals)]
723-
formal_arg_kinds: List[List[int]] = [[] for _ in range(num_formals)]
723+
formal_arg_kinds: List[List[ArgKind]] = [[] for _ in range(num_formals)]
724724
for formal, actuals in enumerate(formal_to_actual):
725725
for actual in actuals:
726726
formal_arg_types[formal].append(arg_types[actual])
@@ -749,7 +749,7 @@ def apply_function_plugin(self,
749749

750750
def apply_signature_hook(
751751
self, callee: FunctionLike, args: List[Expression],
752-
arg_kinds: List[int],
752+
arg_kinds: List[ArgKind],
753753
arg_names: Optional[Sequence[Optional[str]]],
754754
hook: Callable[
755755
[List[List[Expression]], CallableType],
@@ -779,7 +779,7 @@ def apply_signature_hook(
779779

780780
def apply_function_signature_hook(
781781
self, callee: FunctionLike, args: List[Expression],
782-
arg_kinds: List[int], context: Context,
782+
arg_kinds: List[ArgKind], context: Context,
783783
arg_names: Optional[Sequence[Optional[str]]],
784784
signature_hook: Callable[[FunctionSigContext], FunctionLike]) -> FunctionLike:
785785
"""Apply a plugin hook that may infer a more precise signature for a function."""
@@ -790,7 +790,7 @@ def apply_function_signature_hook(
790790

791791
def apply_method_signature_hook(
792792
self, callee: FunctionLike, args: List[Expression],
793-
arg_kinds: List[int], context: Context,
793+
arg_kinds: List[ArgKind], context: Context,
794794
arg_names: Optional[Sequence[Optional[str]]], object_type: Type,
795795
signature_hook: Callable[[MethodSigContext], FunctionLike]) -> FunctionLike:
796796
"""Apply a plugin hook that may infer a more precise signature for a method."""
@@ -802,7 +802,7 @@ def apply_method_signature_hook(
802802

803803
def transform_callee_type(
804804
self, callable_name: Optional[str], callee: Type, args: List[Expression],
805-
arg_kinds: List[int], context: Context,
805+
arg_kinds: List[ArgKind], context: Context,
806806
arg_names: Optional[Sequence[Optional[str]]] = None,
807807
object_type: Optional[Type] = None) -> Type:
808808
"""Attempt to determine a more accurate signature for a method call.
@@ -889,7 +889,7 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
889889
def check_call(self,
890890
callee: Type,
891891
args: List[Expression],
892-
arg_kinds: List[int],
892+
arg_kinds: List[ArgKind],
893893
context: Context,
894894
arg_names: Optional[Sequence[Optional[str]]] = None,
895895
callable_node: Optional[Expression] = None,
@@ -965,7 +965,7 @@ def check_call(self,
965965
def check_callable_call(self,
966966
callee: CallableType,
967967
args: List[Expression],
968-
arg_kinds: List[int],
968+
arg_kinds: List[ArgKind],
969969
context: Context,
970970
arg_names: Optional[Sequence[Optional[str]]],
971971
callable_node: Optional[Expression],
@@ -1098,7 +1098,7 @@ def infer_arg_types_in_empty_context(self, args: List[Expression]) -> List[Type]
10981098
return res
10991099

11001100
def infer_arg_types_in_context(
1101-
self, callee: CallableType, args: List[Expression], arg_kinds: List[int],
1101+
self, callee: CallableType, args: List[Expression], arg_kinds: List[ArgKind],
11021102
formal_to_actual: List[List[int]]) -> List[Type]:
11031103
"""Infer argument expression types using a callable type as context.
11041104
@@ -1197,7 +1197,7 @@ def infer_function_type_arguments_using_context(
11971197

11981198
def infer_function_type_arguments(self, callee_type: CallableType,
11991199
args: List[Expression],
1200-
arg_kinds: List[int],
1200+
arg_kinds: List[ArgKind],
12011201
formal_to_actual: List[List[int]],
12021202
context: Context) -> CallableType:
12031203
"""Infer the type arguments for a generic callee type.
@@ -1260,7 +1260,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
12601260
def infer_function_type_arguments_pass2(
12611261
self, callee_type: CallableType,
12621262
args: List[Expression],
1263-
arg_kinds: List[int],
1263+
arg_kinds: List[ArgKind],
12641264
formal_to_actual: List[List[int]],
12651265
old_inferred_args: Sequence[Optional[Type]],
12661266
context: Context) -> Tuple[CallableType, List[Optional[Type]]]:
@@ -1334,7 +1334,7 @@ def apply_inferred_arguments(self, callee_type: CallableType,
13341334
def check_argument_count(self,
13351335
callee: CallableType,
13361336
actual_types: List[Type],
1337-
actual_kinds: List[int],
1337+
actual_kinds: List[ArgKind],
13381338
actual_names: Optional[Sequence[Optional[str]]],
13391339
formal_to_actual: List[List[int]],
13401340
context: Optional[Context],
@@ -1377,8 +1377,7 @@ def check_argument_count(self,
13771377
argname = callee.arg_names[i] or "?"
13781378
messages.missing_named_argument(callee, context, argname)
13791379
ok = False
1380-
elif kind in [nodes.ARG_POS, nodes.ARG_OPT,
1381-
nodes.ARG_NAMED, nodes.ARG_NAMED_OPT] and is_duplicate_mapping(
1380+
elif not kind.is_star() and is_duplicate_mapping(
13821381
formal_to_actual[i], actual_types, actual_kinds):
13831382
if (self.chk.in_checked_function() or
13841383
isinstance(get_proper_type(actual_types[formal_to_actual[i][0]]),
@@ -1397,7 +1396,7 @@ def check_argument_count(self,
13971396
def check_for_extra_actual_arguments(self,
13981397
callee: CallableType,
13991398
actual_types: List[Type],
1400-
actual_kinds: List[int],
1399+
actual_kinds: List[ArgKind],
14011400
actual_names: Optional[Sequence[Optional[str]]],
14021401
all_actuals: List[int],
14031402
context: Context,
@@ -1452,7 +1451,7 @@ def check_for_extra_actual_arguments(self,
14521451

14531452
def check_argument_types(self,
14541453
arg_types: List[Type],
1455-
arg_kinds: List[int],
1454+
arg_kinds: List[ArgKind],
14561455
args: List[Expression],
14571456
callee: CallableType,
14581457
formal_to_actual: List[List[int]],
@@ -1494,7 +1493,7 @@ def check_argument_types(self,
14941493
def check_arg(self,
14951494
caller_type: Type,
14961495
original_caller_type: Type,
1497-
caller_kind: int,
1496+
caller_kind: ArgKind,
14981497
callee_type: Type,
14991498
n: int,
15001499
m: int,
@@ -1534,7 +1533,7 @@ def check_arg(self,
15341533
def check_overload_call(self,
15351534
callee: Overloaded,
15361535
args: List[Expression],
1537-
arg_kinds: List[int],
1536+
arg_kinds: List[ArgKind],
15381537
arg_names: Optional[Sequence[Optional[str]]],
15391538
callable_name: Optional[str],
15401539
object_type: Optional[Type],
@@ -1640,7 +1639,7 @@ def check_overload_call(self,
16401639

16411640
def plausible_overload_call_targets(self,
16421641
arg_types: List[Type],
1643-
arg_kinds: List[int],
1642+
arg_kinds: List[ArgKind],
16441643
arg_names: Optional[Sequence[Optional[str]]],
16451644
overload: Overloaded) -> List[CallableType]:
16461645
"""Returns all overload call targets that having matching argument counts.
@@ -1690,7 +1689,7 @@ def infer_overload_return_type(self,
16901689
plausible_targets: List[CallableType],
16911690
args: List[Expression],
16921691
arg_types: List[Type],
1693-
arg_kinds: List[int],
1692+
arg_kinds: List[ArgKind],
16941693
arg_names: Optional[Sequence[Optional[str]]],
16951694
callable_name: Optional[str],
16961695
object_type: Optional[Type],
@@ -1772,7 +1771,7 @@ def infer_overload_return_type(self,
17721771
def overload_erased_call_targets(self,
17731772
plausible_targets: List[CallableType],
17741773
arg_types: List[Type],
1775-
arg_kinds: List[int],
1774+
arg_kinds: List[ArgKind],
17761775
arg_names: Optional[Sequence[Optional[str]]],
17771776
args: List[Expression],
17781777
context: Context) -> List[CallableType]:
@@ -1791,7 +1790,7 @@ def union_overload_result(self,
17911790
plausible_targets: List[CallableType],
17921791
args: List[Expression],
17931792
arg_types: List[Type],
1794-
arg_kinds: List[int],
1793+
arg_kinds: List[ArgKind],
17951794
arg_names: Optional[Sequence[Optional[str]]],
17961795
callable_name: Optional[str],
17971796
object_type: Optional[Type],
@@ -1964,7 +1963,7 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C
19641963

19651964
def erased_signature_similarity(self,
19661965
arg_types: List[Type],
1967-
arg_kinds: List[int],
1966+
arg_kinds: List[ArgKind],
19681967
arg_names: Optional[Sequence[Optional[str]]],
19691968
args: List[Expression],
19701969
callee: CallableType,
@@ -1984,7 +1983,7 @@ def erased_signature_similarity(self,
19841983

19851984
def check_arg(caller_type: Type,
19861985
original_ccaller_type: Type,
1987-
caller_kind: int,
1986+
caller_kind: ArgKind,
19881987
callee_type: Type,
19891988
n: int,
19901989
m: int,
@@ -2024,7 +2023,7 @@ def check_any_type_call(self, args: List[Expression], callee: Type) -> Tuple[Typ
20242023
def check_union_call(self,
20252024
callee: UnionType,
20262025
args: List[Expression],
2027-
arg_kinds: List[int],
2026+
arg_kinds: List[ArgKind],
20282027
arg_names: Optional[Sequence[Optional[str]]],
20292028
context: Context,
20302029
arg_messages: MessageBuilder) -> Tuple[Type, Type]:
@@ -2375,7 +2374,7 @@ def check_method_call_by_name(self,
23752374
method: str,
23762375
base_type: Type,
23772376
args: List[Expression],
2378-
arg_kinds: List[int],
2377+
arg_kinds: List[ArgKind],
23792378
context: Context,
23802379
local_errors: Optional[MessageBuilder] = None,
23812380
original_type: Optional[Type] = None
@@ -2405,7 +2404,7 @@ def check_union_method_call_by_name(self,
24052404
method: str,
24062405
base_type: UnionType,
24072406
args: List[Expression],
2408-
arg_kinds: List[int],
2407+
arg_kinds: List[ArgKind],
24092408
context: Context,
24102409
local_errors: MessageBuilder,
24112410
original_type: Optional[Type] = None
@@ -2435,7 +2434,7 @@ def check_method_call(self,
24352434
base_type: Type,
24362435
method_type: Type,
24372436
args: List[Expression],
2438-
arg_kinds: List[int],
2437+
arg_kinds: List[ArgKind],
24392438
context: Context,
24402439
local_errors: Optional[MessageBuilder] = None) -> Tuple[Type, Type]:
24412440
"""Type check a call to a method with the given name and type on an object.
@@ -4253,7 +4252,7 @@ def is_non_empty_tuple(t: Type) -> bool:
42534252

42544253
def is_duplicate_mapping(mapping: List[int],
42554254
actual_types: List[Type],
4256-
actual_kinds: List[int]) -> bool:
4255+
actual_kinds: List[ArgKind]) -> bool:
42574256
return (
42584257
len(mapping) > 1
42594258
# Multiple actuals can map to the same formal if they both come from
@@ -4390,7 +4389,7 @@ def is_typetype_like(typ: ProperType) -> bool:
43904389
def any_causes_overload_ambiguity(items: List[CallableType],
43914390
return_types: List[Type],
43924391
arg_types: List[Type],
4393-
arg_kinds: List[int],
4392+
arg_kinds: List[ArgKind],
43944393
arg_names: Optional[Sequence[Optional[str]]]) -> bool:
43954394
"""May an argument containing 'Any' cause ambiguous result type on call to overloaded function?
43964395

mypy/constraints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import mypy.sametypes
1515
import mypy.typeops
1616
from mypy.erasetype import erase_typevars
17-
from mypy.nodes import COVARIANT, CONTRAVARIANT
17+
from mypy.nodes import COVARIANT, CONTRAVARIANT, ArgKind
1818
from mypy.argmap import ArgTypeExpander
1919
from mypy.typestate import TypeState
2020

@@ -45,7 +45,7 @@ def __repr__(self) -> str:
4545

4646

4747
def infer_constraints_for_callable(
48-
callee: CallableType, arg_types: Sequence[Optional[Type]], arg_kinds: List[int],
48+
callee: CallableType, arg_types: Sequence[Optional[Type]], arg_kinds: List[ArgKind],
4949
formal_to_actual: List[List[int]]) -> List[Constraint]:
5050
"""Infer type variable constraints for a callable and actual arguments.
5151

mypy/fastparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
StarExpr, YieldFromExpr, NonlocalDecl, DictionaryComprehension,
2626
SetComprehension, ComplexExpr, EllipsisExpr, YieldExpr, Argument,
2727
AwaitExpr, TempNode, Expression, Statement,
28-
ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_NAMED_OPT, ARG_STAR2,
28+
ArgKind, ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_NAMED_OPT, ARG_STAR2,
2929
check_arg_names,
3030
FakeInfo,
3131
)
@@ -696,7 +696,7 @@ def transform_args(self,
696696

697697
return new_args
698698

699-
def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: int,
699+
def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: ArgKind,
700700
no_type_check: bool) -> Argument:
701701
if no_type_check:
702702
arg_type = None

mypy/fastparse2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
UnaryExpr, LambdaExpr, ComparisonExpr, DictionaryComprehension,
3737
SetComprehension, ComplexExpr, EllipsisExpr, YieldExpr, Argument,
3838
Expression, Statement, BackquoteExpr, PrintStmt, ExecStmt,
39-
ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_STAR2, OverloadPart, check_arg_names,
39+
ArgKind, ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_STAR2, OverloadPart, check_arg_names,
4040
FakeInfo,
4141
)
4242
from mypy.types import (
@@ -923,7 +923,7 @@ def visit_Compare(self, n: ast27.Compare) -> ComparisonExpr:
923923
# keyword = (identifier? arg, expr value)
924924
def visit_Call(self, n: Call) -> CallExpr:
925925
arg_types: List[ast27.expr] = []
926-
arg_kinds: List[int] = []
926+
arg_kinds: List[ArgKind] = []
927927
signature: List[Optional[str]] = []
928928

929929
args = n.args

0 commit comments

Comments
 (0)