Skip to content

Commit

Permalink
Do not delete FuncItem.arguments on deserialize
Browse files Browse the repository at this point in the history
When FuncDef is deserialized we don't reconstruct the arguments.
Previous code was deleting the attribute, leading to python#11899; instead,
always set the attribute, maybe to the empty list, and get argument
names from arg_names if arguments is empty.
  • Loading branch information
fperrin committed May 20, 2022
1 parent 440ed02 commit f706a7d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
6 changes: 4 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,10 +1975,12 @@ def [T <: int] f(self, x: int, y: T) -> None
s += ' = ...'

# If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list
if isinstance(tp.definition, FuncDef) and tp.definition.name is not None:
if isinstance(tp.definition, FuncDef) and \
tp.definition.name is not None and \
tp.definition.arguments:
definition_args = [arg.variable.name for arg in tp.definition.arguments]
if definition_args and tp.arg_names != definition_args \
and len(definition_args) > 0 and definition_args[0]:
and len(definition_args) > 0 and definition_args[0]:
if s:
s = ', ' + s
s = definition_args[0] + s
Expand Down
5 changes: 2 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def set_line(self,
class FuncItem(FuncBase):
"""Base class for nodes usable as overloaded function items."""

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

__deletable__ = ('arguments', 'max_pos', 'min_args')
__deletable__ = ('max_pos', 'min_args')

def __init__(self,
arguments: Optional[List[Argument]] = None,
Expand Down Expand Up @@ -780,7 +780,6 @@ def deserialize(cls, data: JsonDict) -> 'FuncDef':
ret.arg_names = data['arg_names']
ret.arg_kinds = [ArgKind(x) for x in data['arg_kinds']]
# Leave these uninitialized so that future uses will trigger an error
del ret.arguments
del ret.max_pos
del ret.min_args
return ret
Expand Down
1 change: 1 addition & 0 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ def get_args(self, is_method: bool,
return types

def get_default_arg_types(self, fdef: FuncDef) -> List[Optional[Type]]:
assert fdef.arguments is not None
return [
self.manager.all_types[arg.initializer] if arg.initializer else None
for arg in fdef.arguments
Expand Down
17 changes: 7 additions & 10 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,16 +1524,13 @@ def __init__(self,
# after serialization, but it is useful in error messages.
# TODO: decide how to add more info here (file, line, column)
# without changing interface hash.
self.def_extras = {
'first_arg': (
definition.arguments[0].variable.name
if (getattr(definition, 'arguments', None)
and definition.arg_names
and definition.info
and not definition.is_static)
else None
),
}
first_arg: Optional[str] = None
if definition.info and not definition.is_static:
if definition.arguments:
first_arg = definition.arguments[0].variable.name
elif definition.arg_names:
first_arg = definition.arg_names[0]
self.def_extras = {'first_arg': first_arg}
else:
self.def_extras = {}
self.type_guard = type_guard
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def fdef_to_sig(self, fdef: FuncDef) -> FuncSignature:
# deserialized FuncDef that lacks arguments. We won't ever
# need to use those inside of a FuncIR, so we just make up
# some crap.
if hasattr(fdef, 'arguments'):
if fdef.arguments:
arg_names = [arg.variable.name for arg in fdef.arguments]
else:
arg_names = [name or '' for name in fdef.arg_names]
Expand Down

0 comments on commit f706a7d

Please sign in to comment.