Skip to content

Commit 1c03e10

Browse files
authored
Fix "attribute 'arguments' of 'FuncDef' undefined" incremental crash (#12324)
When deserializing from cache, FuncDef.arguments is not set, so check before use.
1 parent a4ae0ad commit 1c03e10

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

mypy/messages.py

+3-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def set_line(self,
658658
class FuncItem(FuncBase):
659659
"""Base class for nodes usable as overloaded function items."""
660660

661-
__slots__ = ('arguments', # Note that can be None if deserialized (type is a lie!)
661+
__slots__ = ('arguments', # Note that can be unset if deserialized (type is a lie!)
662662
'arg_names', # Names of arguments
663663
'arg_kinds', # Kinds of arguments
664664
'min_args', # Minimum number of arguments

mypy/types.py

+9-10
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

test-data/unit/check-modules.test

+32
Original file line numberDiff line numberDiff line change
@@ -3212,3 +3212,35 @@ from dir1 import *
32123212
from .test2 import *
32133213
[file dir1/test2.py]
32143214
from test1 import aaaa # E: Module "test1" has no attribute "aaaa"
3215+
3216+
[case testIncompatibleOverrideFromCachedModuleIncremental]
3217+
import b
3218+
[file a.py]
3219+
class Foo:
3220+
def frobnicate(self, *args, **kwargs): pass
3221+
[file b.py]
3222+
from a import Foo
3223+
class Bar(Foo):
3224+
def frobnicate(self) -> None: pass
3225+
[file b.py.2]
3226+
from a import Foo
3227+
class Bar(Foo):
3228+
def frobnicate(self, *args) -> None: pass
3229+
[file b.py.3]
3230+
from a import Foo
3231+
class Bar(Foo):
3232+
def frobnicate(self, *args) -> None: pass # type: ignore[override] # I know
3233+
[builtins fixtures/tuple.pyi]
3234+
[builtins fixtures/dict.pyi]
3235+
[out1]
3236+
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
3237+
tmp/b.py:3: note: Superclass:
3238+
tmp/b.py:3: note: def frobnicate(self, *args: Any, **kwargs: Any) -> Any
3239+
tmp/b.py:3: note: Subclass:
3240+
tmp/b.py:3: note: def frobnicate(self) -> None
3241+
[out2]
3242+
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
3243+
tmp/b.py:3: note: Superclass:
3244+
tmp/b.py:3: note: def frobnicate(self, *args: Any, **kwargs: Any) -> Any
3245+
tmp/b.py:3: note: Subclass:
3246+
tmp/b.py:3: note: def frobnicate(self, *args: Any) -> None

0 commit comments

Comments
 (0)