Skip to content

Commit 1dc3030

Browse files
committed
Drop num_pos_only in favor of tracking pos_only in Argument
1 parent ae8b284 commit 1dc3030

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed

mypy/fastparse.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,13 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef],
522522

523523
lineno = n.lineno
524524
args = self.transform_args(n.args, lineno, no_type_check=no_type_check)
525+
if special_function_elide_names(n.name):
526+
for arg in args:
527+
arg.pos_only = True
525528

526-
posonlyargs = [arg.arg for arg in getattr(n.args, "posonlyargs", [])]
527529
arg_kinds = [arg.kind for arg in args]
528-
arg_names: List[Optional[str]] = [arg.variable.name for arg in args]
529-
arg_names = [None if argument_elide_name(name) or name in posonlyargs else name
530-
for name in arg_names]
531-
if special_function_elide_names(n.name):
532-
arg_names = [None] * len(arg_names)
530+
arg_names = [None if arg.pos_only else arg.variable.name for arg in args]
531+
533532
arg_types: List[Optional[Type]] = []
534533
if no_type_check:
535534
arg_types = [None] * len(args)
@@ -606,8 +605,7 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef],
606605
n.name,
607606
args,
608607
self.as_required_block(n.body, lineno),
609-
func_type,
610-
num_pos_only=len(posonlyargs))
608+
func_type)
611609
if isinstance(func_def.type, CallableType):
612610
# semanal.py does some in-place modifications we want to avoid
613611
func_def.unanalyzed_type = func_def.type.copy_modified()
@@ -662,17 +660,20 @@ def transform_args(self,
662660
) -> List[Argument]:
663661
new_args = []
664662
names: List[ast3.arg] = []
665-
args_args = getattr(args, "posonlyargs", cast(List[ast3.arg], [])) + args.args
663+
posonlyargs = getattr(args, "posonlyargs", cast(List[ast3.arg], []))
664+
args_args = posonlyargs + args.args
666665
args_defaults = args.defaults
667666
num_no_defaults = len(args_args) - len(args_defaults)
668667
# positional arguments without defaults
669-
for a in args_args[:num_no_defaults]:
670-
new_args.append(self.make_argument(a, None, ARG_POS, no_type_check))
668+
for i, a in enumerate(args_args[:num_no_defaults]):
669+
pos_only = i < len(posonlyargs)
670+
new_args.append(self.make_argument(a, None, ARG_POS, no_type_check, pos_only))
671671
names.append(a)
672672

673673
# positional arguments with defaults
674-
for a, d in zip(args_args[num_no_defaults:], args_defaults):
675-
new_args.append(self.make_argument(a, d, ARG_OPT, no_type_check))
674+
for i, (a, d) in enumerate(zip(args_args[num_no_defaults:], args_defaults)):
675+
pos_only = num_no_defaults + i < len(posonlyargs)
676+
new_args.append(self.make_argument(a, d, ARG_OPT, no_type_check, pos_only))
676677
names.append(a)
677678

678679
# *arg
@@ -699,7 +700,7 @@ def transform_args(self,
699700
return new_args
700701

701702
def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: ArgKind,
702-
no_type_check: bool) -> Argument:
703+
no_type_check: bool, pos_only: bool = False) -> Argument:
703704
if no_type_check:
704705
arg_type = None
705706
else:
@@ -712,7 +713,10 @@ def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: ArgKi
712713
arg_type = TypeConverter(self.errors, line=arg.lineno).visit(annotation)
713714
else:
714715
arg_type = self.translate_type_comment(arg, type_comment)
715-
return Argument(Var(arg.arg), arg_type, self.visit(default), kind)
716+
if argument_elide_name(arg.arg):
717+
pos_only = True
718+
719+
return Argument(Var(arg.arg), arg_type, self.visit(default), kind, pos_only)
716720

717721
def fail_arg(self, msg: str, arg: ast3.arg) -> None:
718722
self.fail(msg, arg.lineno, arg.col_offset)
@@ -994,10 +998,8 @@ def visit_Lambda(self, n: ast3.Lambda) -> LambdaExpr:
994998
body.lineno = n.body.lineno
995999
body.col_offset = n.body.col_offset
9961000

997-
num_pos_only = len(getattr(n.args, "posonlyargs", []))
9981001
e = LambdaExpr(self.transform_args(n.args, n.lineno),
999-
self.as_required_block([body], n.lineno),
1000-
num_pos_only=num_pos_only)
1002+
self.as_required_block([body], n.lineno))
10011003
e.set_line(n.lineno, n.col_offset) # Overrides set_line -- can't use self.set_line
10021004
return e
10031005

mypy/fastparse2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,12 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
369369
converter = TypeConverter(self.errors, line=lineno, override_column=n.col_offset,
370370
assume_str_is_unicode=self.unicode_literals)
371371
args, decompose_stmts = self.transform_args(n.args, lineno)
372+
if special_function_elide_names(n.name):
373+
for arg in args:
374+
arg.pos_only = True
372375

373376
arg_kinds = [arg.kind for arg in args]
374-
arg_names: List[Optional[str]] = [arg.variable.name for arg in args]
375-
arg_names = [None if argument_elide_name(name) else name for name in arg_names]
376-
if special_function_elide_names(n.name):
377-
arg_names = [None] * len(arg_names)
377+
arg_names = [None if arg.pos_only else arg.variable.name for arg in args]
378378

379379
arg_types: List[Optional[Type]] = []
380380
type_comment = n.type_comment
@@ -518,6 +518,10 @@ def transform_args(self,
518518
new_args.append(Argument(Var(n.kwarg), typ, None, ARG_STAR2))
519519
names.append(n.kwarg)
520520

521+
for arg in new_args:
522+
if argument_elide_name(arg.variable.name):
523+
arg.pos_only = True
524+
521525
# We don't have any context object to give, but we have closed around the line num
522526
def fail_arg(msg: str, arg: None) -> None:
523527
self.fail(msg, line, 0)

mypy/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,9 +1936,9 @@ def [T <: int] f(self, x: int, y: T) -> None
19361936

19371937
# If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list
19381938
if isinstance(tp.definition, FuncDef) and tp.definition.name is not None:
1939-
definition_args = tp.definition.arg_names
1939+
definition_args = [arg.variable.name for arg in tp.definition.arguments]
19401940
if definition_args and tp.arg_names != definition_args \
1941-
and len(definition_args) > 0:
1941+
and len(definition_args) > 0 and definition_args[0]:
19421942
if s:
19431943
s = ', ' + s
19441944
s = definition_args[0] + s

mypy/nodes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,20 @@ def deserialize(cls, data: JsonDict) -> 'OverloadedFuncDef':
561561
class Argument(Node):
562562
"""A single argument in a FuncItem."""
563563

564-
__slots__ = ('variable', 'type_annotation', 'initializer', 'kind')
564+
__slots__ = ('variable', 'type_annotation', 'initializer', 'kind', 'pos_only')
565565

566566
def __init__(self,
567567
variable: 'Var',
568568
type_annotation: 'Optional[mypy.types.Type]',
569569
initializer: Optional[Expression],
570-
kind: 'ArgKind') -> None:
570+
kind: 'ArgKind',
571+
pos_only: bool = False) -> None:
571572
super().__init__()
572573
self.variable = variable
573574
self.type_annotation = type_annotation
574575
self.initializer = initializer
575576
self.kind = kind # must be an ARG_* constant
577+
self.pos_only = pos_only
576578

577579
def set_line(self,
578580
target: Union[Context, int],
@@ -617,13 +619,11 @@ class FuncItem(FuncBase):
617619
def __init__(self,
618620
arguments: List[Argument],
619621
body: 'Block',
620-
typ: 'Optional[mypy.types.FunctionLike]' = None,
621-
num_pos_only: int = 0) -> None:
622+
typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
622623
super().__init__()
623624
self.arguments = arguments
624-
self.arg_names = [arg.variable.name for arg in self.arguments]
625+
self.arg_names = [None if arg.pos_only else arg.variable.name for arg in arguments]
625626
self.arg_kinds: List[ArgKind] = [arg.kind for arg in self.arguments]
626-
self.num_pos_only = num_pos_only
627627
self.max_pos: int = (
628628
self.arg_kinds.count(ARG_POS) + self.arg_kinds.count(ARG_OPT))
629629
self.body: 'Block' = body
@@ -678,9 +678,8 @@ def __init__(self,
678678
name: str, # Function name
679679
arguments: List[Argument],
680680
body: 'Block',
681-
typ: 'Optional[mypy.types.FunctionLike]' = None,
682-
num_pos_only: int = 0) -> None:
683-
super().__init__(arguments, body, typ, num_pos_only)
681+
typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
682+
super().__init__(arguments, body, typ)
684683
self._name = name
685684
self.is_decorated = False
686685
self.is_conditional = False # Defined conditionally (within block)?

mypy/typeops.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
)
2323
from mypy.maptype import map_instance_to_supertype
2424
from mypy.expandtype import expand_type_by_instance, expand_type
25-
from mypy.sharedparse import argument_elide_name
2625

2726
from mypy.typevars import fill_typevars
2827

@@ -564,8 +563,7 @@ def callable_type(fdef: FuncItem, fallback: Instance,
564563
return CallableType(
565564
args,
566565
fdef.arg_kinds,
567-
[None if argument_elide_name(n) or i < fdef.num_pos_only else n
568-
for i, n in enumerate(fdef.arg_names)],
566+
fdef.arg_names,
569567
ret_type or AnyType(TypeOfAny.unannotated),
570568
fallback,
571569
name=fdef.name,

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ def __init__(self,
10771077
# after serialization, but it is useful in error messages.
10781078
# TODO: decide how to add more info here (file, line, column)
10791079
# without changing interface hash.
1080-
self.def_extras = {'first_arg': definition.arg_names[0]
1080+
self.def_extras = {'first_arg': definition.arguments[0].variable.name
10811081
if definition.arg_names and definition.info and
10821082
not definition.is_static else None}
10831083
else:

mypyc/irbuild/mapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def fdef_to_sig(self, fdef: FuncDef) -> FuncSignature:
133133
else:
134134
ret = object_rprimitive
135135

136-
args = [RuntimeArg(arg_name, arg_type, arg_kind)
137-
for arg_name, arg_kind, arg_type in zip(fdef.arg_names, fdef.arg_kinds, arg_types)]
136+
args = [RuntimeArg(arg.variable.name, arg_type, arg.kind)
137+
for arg, arg_type in zip(fdef.arguments, arg_types)]
138138

139139
# We force certain dunder methods to return objects to support letting them
140140
# return NotImplemented. It also avoids some pointless boxing and unboxing,

test-data/unit/check-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ t = Test()
27592759
t.crash = 'test' # E: "Test" has no attribute "crash"
27602760

27612761
class A:
2762-
def __setattr__(self): ... # E: Invalid signature "def (self: __main__.A) -> Any" for "__setattr__"
2762+
def __setattr__(self): ... # E: Invalid signature "def (__main__.A) -> Any" for "__setattr__"
27632763
a = A()
27642764
a.test = 4 # E: "A" has no attribute "test"
27652765

0 commit comments

Comments
 (0)