Skip to content

Commit 4406bb9

Browse files
committed
Do not delete FuncItem.arguments on deserialize
When FuncDef is deserialized we don't reconstruct the arguments. Previous code was deleting the attribute, leading to #11899; instead, always set the attribute, maybe to the empty list, and get argument names from arg_names if arguments is empty.
1 parent 440ed02 commit 4406bb9

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

mypy/messages.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,12 @@ def [T <: int] f(self, x: int, y: T) -> None
19751975
s += ' = ...'
19761976

19771977
# If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list
1978-
if isinstance(tp.definition, FuncDef) and tp.definition.name is not None:
1978+
if isinstance(tp.definition, FuncDef) and \
1979+
tp.definition.name is not None and \
1980+
tp.definition.arguments:
19791981
definition_args = [arg.variable.name for arg in tp.definition.arguments]
19801982
if definition_args and tp.arg_names != definition_args \
1981-
and len(definition_args) > 0 and definition_args[0]:
1983+
and len(definition_args) > 0 and definition_args[0]:
19821984
if s:
19831985
s = ', ' + s
19841986
s = definition_args[0] + s

mypy/nodes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def set_line(self,
649649
class FuncItem(FuncBase):
650650
"""Base class for nodes usable as overloaded function items."""
651651

652-
__slots__ = ('arguments', # Note that can be None if deserialized (type is a lie!)
652+
__slots__ = ('arguments', # Note that can be empty if deserialized
653653
'arg_names', # Names of arguments
654654
'arg_kinds', # Kinds of arguments
655655
'min_args', # Minimum number of arguments
@@ -665,7 +665,7 @@ class FuncItem(FuncBase):
665665
'expanded', # Variants of function with type variables with values expanded
666666
)
667667

668-
__deletable__ = ('arguments', 'max_pos', 'min_args')
668+
__deletable__ = ('max_pos', 'min_args')
669669

670670
def __init__(self,
671671
arguments: Optional[List[Argument]] = None,
@@ -780,7 +780,6 @@ def deserialize(cls, data: JsonDict) -> 'FuncDef':
780780
ret.arg_names = data['arg_names']
781781
ret.arg_kinds = [ArgKind(x) for x in data['arg_kinds']]
782782
# Leave these uninitialized so that future uses will trigger an error
783-
del ret.arguments
784783
del ret.max_pos
785784
del ret.min_args
786785
return ret

mypy/types.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,16 +1524,13 @@ def __init__(self,
15241524
# after serialization, but it is useful in error messages.
15251525
# TODO: decide how to add more info here (file, line, column)
15261526
# without changing interface hash.
1527-
self.def_extras = {
1528-
'first_arg': (
1529-
definition.arguments[0].variable.name
1530-
if (getattr(definition, 'arguments', None)
1531-
and definition.arg_names
1532-
and definition.info
1533-
and not definition.is_static)
1534-
else None
1535-
),
1536-
}
1527+
first_arg: Optional[str] = None
1528+
if definition.info and not definition.is_static:
1529+
if definition.arguments:
1530+
first_arg = definition.arguments[0].variable.name
1531+
elif definition.arg_names:
1532+
first_arg = definition.arg_names[0]
1533+
self.def_extras = {'first_arg': first_arg}
15371534
else:
15381535
self.def_extras = {}
15391536
self.type_guard = type_guard

mypyc/irbuild/mapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def fdef_to_sig(self, fdef: FuncDef) -> FuncSignature:
145145
# deserialized FuncDef that lacks arguments. We won't ever
146146
# need to use those inside of a FuncIR, so we just make up
147147
# some crap.
148-
if hasattr(fdef, 'arguments'):
148+
if fdef.arguments:
149149
arg_names = [arg.variable.name for arg in fdef.arguments]
150150
else:
151151
arg_names = [name or '' for name in fdef.arg_names]

0 commit comments

Comments
 (0)