Skip to content

Commit 9cc69b8

Browse files
committed
Check FuncDef.arguments before use
When deserializing from cache, FuncDef.arguments is not set, so check before use and fallback to arg_names.
1 parent 74e49cc commit 9cc69b8

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

mypy/messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,9 @@ def [T <: int] f(self, x: int, y: T) -> None
19781978
s += ' = ...'
19791979

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

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
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 unset if deserialized (type is a lie!)
653653
'arg_names', # Names of arguments
654654
'arg_kinds', # Kinds of arguments
655655
'min_args', # Minimum number of arguments

mypy/types.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,16 +1524,15 @@ 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.arg_names and
1529+
definition.info and
1530+
not definition.is_static):
1531+
if getattr(definition, 'arguments', None):
1532+
first_arg = definition.arguments[0].variable.name
1533+
else:
1534+
first_arg = definition.arg_names[0]
1535+
self.def_extras = {'first_arg': first_arg}
15371536
else:
15381537
self.def_extras = {}
15391538
self.type_guard = type_guard

0 commit comments

Comments
 (0)