Skip to content

Commit

Permalink
Make ARG_* into an actual enum class instead of just using int everyw…
Browse files Browse the repository at this point in the history
…here (python#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...)
  • Loading branch information
msullivan authored Jul 9, 2021
1 parent ebc4f10 commit 3552971
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 137 deletions.
12 changes: 6 additions & 6 deletions mypy/argmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from mypy import nodes


def map_actuals_to_formals(actual_kinds: List[int],
def map_actuals_to_formals(actual_kinds: List[nodes.ArgKind],
actual_names: Optional[Sequence[Optional[str]]],
formal_kinds: List[int],
formal_kinds: List[nodes.ArgKind],
formal_names: Sequence[Optional[str]],
actual_arg_type: Callable[[int],
Type]) -> List[List[int]]:
Expand Down Expand Up @@ -99,9 +99,9 @@ def map_actuals_to_formals(actual_kinds: List[int],
return formal_to_actual


def map_formals_to_actuals(actual_kinds: List[int],
def map_formals_to_actuals(actual_kinds: List[nodes.ArgKind],
actual_names: Optional[Sequence[Optional[str]]],
formal_kinds: List[int],
formal_kinds: List[nodes.ArgKind],
formal_names: List[Optional[str]],
actual_arg_type: Callable[[int],
Type]) -> List[List[int]]:
Expand Down Expand Up @@ -149,9 +149,9 @@ def __init__(self) -> None:

def expand_actual_type(self,
actual_type: Type,
actual_kind: int,
actual_kind: nodes.ArgKind,
formal_name: Optional[str],
formal_kind: int) -> Type:
formal_kind: nodes.ArgKind) -> Type:
"""Return the actual (caller) type(s) of a formal argument with the given kinds.
If the actual argument is a tuple *args, return the next individual tuple item that
Expand Down
65 changes: 32 additions & 33 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
TypeAliasExpr, BackquoteExpr, EnumCallExpr, TypeAlias, SymbolNode, PlaceholderNode,
ParamSpecExpr,
ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
ArgKind, ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
)
from mypy.literals import literal
from mypy import nodes
Expand Down Expand Up @@ -73,7 +73,7 @@
# check_args() below for details.
ArgChecker = Callable[[Type,
Type,
int,
ArgKind,
Type,
int,
int,
Expand Down Expand Up @@ -476,7 +476,7 @@ def check_protocol_issubclass(self, e: CallExpr) -> None:
attr_members, e)

def check_typeddict_call(self, callee: TypedDictType,
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Sequence[Optional[str]],
args: List[Expression],
context: Context) -> Type:
Expand Down Expand Up @@ -697,7 +697,7 @@ def try_infer_partial_value_type_from_call(

def apply_function_plugin(self,
callee: CallableType,
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_types: List[Type],
arg_names: Optional[Sequence[Optional[str]]],
formal_to_actual: List[List[int]],
Expand All @@ -720,7 +720,7 @@ def apply_function_plugin(self,
formal_arg_types: List[List[Type]] = [[] for _ in range(num_formals)]
formal_arg_exprs: List[List[Expression]] = [[] for _ in range(num_formals)]
formal_arg_names: List[List[Optional[str]]] = [[] for _ in range(num_formals)]
formal_arg_kinds: List[List[int]] = [[] for _ in range(num_formals)]
formal_arg_kinds: List[List[ArgKind]] = [[] for _ in range(num_formals)]
for formal, actuals in enumerate(formal_to_actual):
for actual in actuals:
formal_arg_types[formal].append(arg_types[actual])
Expand Down Expand Up @@ -749,7 +749,7 @@ def apply_function_plugin(self,

def apply_signature_hook(
self, callee: FunctionLike, args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
hook: Callable[
[List[List[Expression]], CallableType],
Expand Down Expand Up @@ -779,7 +779,7 @@ def apply_signature_hook(

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

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

def transform_callee_type(
self, callable_name: Optional[str], callee: Type, args: List[Expression],
arg_kinds: List[int], context: Context,
arg_kinds: List[ArgKind], context: Context,
arg_names: Optional[Sequence[Optional[str]]] = None,
object_type: Optional[Type] = None) -> Type:
"""Attempt to determine a more accurate signature for a method call.
Expand Down Expand Up @@ -889,7 +889,7 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
def check_call(self,
callee: Type,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
context: Context,
arg_names: Optional[Sequence[Optional[str]]] = None,
callable_node: Optional[Expression] = None,
Expand Down Expand Up @@ -965,7 +965,7 @@ def check_call(self,
def check_callable_call(self,
callee: CallableType,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
context: Context,
arg_names: Optional[Sequence[Optional[str]]],
callable_node: Optional[Expression],
Expand Down Expand Up @@ -1098,7 +1098,7 @@ def infer_arg_types_in_empty_context(self, args: List[Expression]) -> List[Type]
return res

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

def infer_function_type_arguments(self, callee_type: CallableType,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
formal_to_actual: List[List[int]],
context: Context) -> CallableType:
"""Infer the type arguments for a generic callee type.
Expand Down Expand Up @@ -1260,7 +1260,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
def infer_function_type_arguments_pass2(
self, callee_type: CallableType,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
formal_to_actual: List[List[int]],
old_inferred_args: Sequence[Optional[Type]],
context: Context) -> Tuple[CallableType, List[Optional[Type]]]:
Expand Down Expand Up @@ -1334,7 +1334,7 @@ def apply_inferred_arguments(self, callee_type: CallableType,
def check_argument_count(self,
callee: CallableType,
actual_types: List[Type],
actual_kinds: List[int],
actual_kinds: List[ArgKind],
actual_names: Optional[Sequence[Optional[str]]],
formal_to_actual: List[List[int]],
context: Optional[Context],
Expand Down Expand Up @@ -1377,8 +1377,7 @@ def check_argument_count(self,
argname = callee.arg_names[i] or "?"
messages.missing_named_argument(callee, context, argname)
ok = False
elif kind in [nodes.ARG_POS, nodes.ARG_OPT,
nodes.ARG_NAMED, nodes.ARG_NAMED_OPT] and is_duplicate_mapping(
elif not kind.is_star() and is_duplicate_mapping(
formal_to_actual[i], actual_types, actual_kinds):
if (self.chk.in_checked_function() or
isinstance(get_proper_type(actual_types[formal_to_actual[i][0]]),
Expand All @@ -1397,7 +1396,7 @@ def check_argument_count(self,
def check_for_extra_actual_arguments(self,
callee: CallableType,
actual_types: List[Type],
actual_kinds: List[int],
actual_kinds: List[ArgKind],
actual_names: Optional[Sequence[Optional[str]]],
all_actuals: List[int],
context: Context,
Expand Down Expand Up @@ -1452,7 +1451,7 @@ def check_for_extra_actual_arguments(self,

def check_argument_types(self,
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
args: List[Expression],
callee: CallableType,
formal_to_actual: List[List[int]],
Expand Down Expand Up @@ -1494,7 +1493,7 @@ def check_argument_types(self,
def check_arg(self,
caller_type: Type,
original_caller_type: Type,
caller_kind: int,
caller_kind: ArgKind,
callee_type: Type,
n: int,
m: int,
Expand Down Expand Up @@ -1534,7 +1533,7 @@ def check_arg(self,
def check_overload_call(self,
callee: Overloaded,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
callable_name: Optional[str],
object_type: Optional[Type],
Expand Down Expand Up @@ -1640,7 +1639,7 @@ def check_overload_call(self,

def plausible_overload_call_targets(self,
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
overload: Overloaded) -> List[CallableType]:
"""Returns all overload call targets that having matching argument counts.
Expand Down Expand Up @@ -1690,7 +1689,7 @@ def infer_overload_return_type(self,
plausible_targets: List[CallableType],
args: List[Expression],
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
callable_name: Optional[str],
object_type: Optional[Type],
Expand Down Expand Up @@ -1772,7 +1771,7 @@ def infer_overload_return_type(self,
def overload_erased_call_targets(self,
plausible_targets: List[CallableType],
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
args: List[Expression],
context: Context) -> List[CallableType]:
Expand All @@ -1791,7 +1790,7 @@ def union_overload_result(self,
plausible_targets: List[CallableType],
args: List[Expression],
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
callable_name: Optional[str],
object_type: Optional[Type],
Expand Down Expand Up @@ -1964,7 +1963,7 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C

def erased_signature_similarity(self,
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
args: List[Expression],
callee: CallableType,
Expand All @@ -1984,7 +1983,7 @@ def erased_signature_similarity(self,

def check_arg(caller_type: Type,
original_ccaller_type: Type,
caller_kind: int,
caller_kind: ArgKind,
callee_type: Type,
n: int,
m: int,
Expand Down Expand Up @@ -2024,7 +2023,7 @@ def check_any_type_call(self, args: List[Expression], callee: Type) -> Tuple[Typ
def check_union_call(self,
callee: UnionType,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]],
context: Context,
arg_messages: MessageBuilder) -> Tuple[Type, Type]:
Expand Down Expand Up @@ -2375,7 +2374,7 @@ def check_method_call_by_name(self,
method: str,
base_type: Type,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
context: Context,
local_errors: Optional[MessageBuilder] = None,
original_type: Optional[Type] = None
Expand Down Expand Up @@ -2405,7 +2404,7 @@ def check_union_method_call_by_name(self,
method: str,
base_type: UnionType,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
context: Context,
local_errors: MessageBuilder,
original_type: Optional[Type] = None
Expand Down Expand Up @@ -2435,7 +2434,7 @@ def check_method_call(self,
base_type: Type,
method_type: Type,
args: List[Expression],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
context: Context,
local_errors: Optional[MessageBuilder] = None) -> Tuple[Type, Type]:
"""Type check a call to a method with the given name and type on an object.
Expand Down Expand Up @@ -4253,7 +4252,7 @@ def is_non_empty_tuple(t: Type) -> bool:

def is_duplicate_mapping(mapping: List[int],
actual_types: List[Type],
actual_kinds: List[int]) -> bool:
actual_kinds: List[ArgKind]) -> bool:
return (
len(mapping) > 1
# Multiple actuals can map to the same formal if they both come from
Expand Down Expand Up @@ -4390,7 +4389,7 @@ def is_typetype_like(typ: ProperType) -> bool:
def any_causes_overload_ambiguity(items: List[CallableType],
return_types: List[Type],
arg_types: List[Type],
arg_kinds: List[int],
arg_kinds: List[ArgKind],
arg_names: Optional[Sequence[Optional[str]]]) -> bool:
"""May an argument containing 'Any' cause ambiguous result type on call to overloaded function?
Expand Down
4 changes: 2 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mypy.sametypes
import mypy.typeops
from mypy.erasetype import erase_typevars
from mypy.nodes import COVARIANT, CONTRAVARIANT
from mypy.nodes import COVARIANT, CONTRAVARIANT, ArgKind
from mypy.argmap import ArgTypeExpander
from mypy.typestate import TypeState

Expand Down Expand Up @@ -45,7 +45,7 @@ def __repr__(self) -> str:


def infer_constraints_for_callable(
callee: CallableType, arg_types: Sequence[Optional[Type]], arg_kinds: List[int],
callee: CallableType, arg_types: Sequence[Optional[Type]], arg_kinds: List[ArgKind],
formal_to_actual: List[List[int]]) -> List[Constraint]:
"""Infer type variable constraints for a callable and actual arguments.
Expand Down
4 changes: 2 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
StarExpr, YieldFromExpr, NonlocalDecl, DictionaryComprehension,
SetComprehension, ComplexExpr, EllipsisExpr, YieldExpr, Argument,
AwaitExpr, TempNode, Expression, Statement,
ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_NAMED_OPT, ARG_STAR2,
ArgKind, ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_NAMED_OPT, ARG_STAR2,
check_arg_names,
FakeInfo,
)
Expand Down Expand Up @@ -696,7 +696,7 @@ def transform_args(self,

return new_args

def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: int,
def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: ArgKind,
no_type_check: bool) -> Argument:
if no_type_check:
arg_type = None
Expand Down
4 changes: 2 additions & 2 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
UnaryExpr, LambdaExpr, ComparisonExpr, DictionaryComprehension,
SetComprehension, ComplexExpr, EllipsisExpr, YieldExpr, Argument,
Expression, Statement, BackquoteExpr, PrintStmt, ExecStmt,
ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_STAR2, OverloadPart, check_arg_names,
ArgKind, ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_STAR2, OverloadPart, check_arg_names,
FakeInfo,
)
from mypy.types import (
Expand Down Expand Up @@ -923,7 +923,7 @@ def visit_Compare(self, n: ast27.Compare) -> ComparisonExpr:
# keyword = (identifier? arg, expr value)
def visit_Call(self, n: Call) -> CallExpr:
arg_types: List[ast27.expr] = []
arg_kinds: List[int] = []
arg_kinds: List[ArgKind] = []
signature: List[Optional[str]] = []

args = n.args
Expand Down
Loading

0 comments on commit 3552971

Please sign in to comment.