2
2
3
3
from collections import OrderedDict
4
4
5
- from typing import Dict , List , Set , Tuple
5
+ from typing import Dict , List , Set , Tuple , Optional
6
6
from typing_extensions import Final
7
7
8
8
from mypy .nodes import (
@@ -79,7 +79,9 @@ def transform(self) -> None:
79
79
ctx = self ._ctx
80
80
info = self ._ctx .cls .info
81
81
attributes = self .collect_attributes ()
82
- # Check if attribute types are ready.
82
+ if attributes is None :
83
+ # Some definitions are not ready, defer() should be already called.
84
+ return
83
85
for attr in attributes :
84
86
if info [attr .name ].type is None :
85
87
ctx .api .defer ()
@@ -95,7 +97,9 @@ def transform(self) -> None:
95
97
# processed them yet. In order to work around this, we can simply skip generating
96
98
# __init__ if there are no attributes, because if the user truly did not define any,
97
99
# then the object default __init__ with an empty signature will be present anyway.
98
- if decorator_arguments ['init' ] and '__init__' not in info .names and attributes :
100
+ if (decorator_arguments ['init' ] and
101
+ ('__init__' not in info .names or info .names ['__init__' ].plugin_generated ) and
102
+ attributes ):
99
103
add_method (
100
104
ctx ,
101
105
'__init__' ,
@@ -189,7 +193,7 @@ def reset_init_only_vars(self, info: TypeInfo, attributes: List[DataclassAttribu
189
193
# recreate a symbol node for this attribute.
190
194
lvalue .node = None
191
195
192
- def collect_attributes (self ) -> List [DataclassAttribute ]:
196
+ def collect_attributes (self ) -> Optional [ List [DataclassAttribute ] ]:
193
197
"""Collect all attributes declared in the dataclass and its parents.
194
198
195
199
All assignments of the form
@@ -225,7 +229,7 @@ def collect_attributes(self) -> List[DataclassAttribute]:
225
229
node = sym .node
226
230
if isinstance (node , PlaceholderNode ):
227
231
# This node is not ready yet.
228
- continue
232
+ return None
229
233
assert isinstance (node , Var )
230
234
231
235
# x: ClassVar[int] is ignored by dataclasses.
0 commit comments