8
8
ManyToManyRel , ManyToOneRel , OneToOneRel ,
9
9
)
10
10
from mypy .nodes import ARG_STAR2 , Argument , Context , FuncDef , TypeInfo , Var
11
- from mypy .plugin import ClassDefContext
11
+ from mypy .plugin import ClassDefContext , SemanticAnalyzerPluginInterface
12
12
from mypy .plugins import common
13
13
from mypy .plugins .common import add_method
14
14
from mypy .types import AnyType , CallableType , Instance
22
22
23
23
24
24
class ModelClassInitializer :
25
+ api : SemanticAnalyzerPluginInterface
26
+
25
27
def __init__ (self , ctx : ClassDefContext , django_context : DjangoContext ):
26
28
self .api = ctx .api
27
29
self .model_classdef = ctx .cls
@@ -118,7 +120,14 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
118
120
continue
119
121
120
122
rel_primary_key_field = self .django_context .get_primary_key_field (related_model_cls )
121
- field_info = self .lookup_class_typeinfo_or_incomplete_defn_error (rel_primary_key_field .__class__ )
123
+ try :
124
+ field_info = self .lookup_class_typeinfo_or_incomplete_defn_error (rel_primary_key_field .__class__ )
125
+ except helpers .IncompleteDefnException as exc :
126
+ if not self .api .final_iteration :
127
+ raise exc
128
+ else :
129
+ continue
130
+
122
131
is_nullable = self .django_context .get_field_nullability (field , None )
123
132
set_type , get_type = get_field_descriptor_types (field_info , is_nullable )
124
133
self .add_new_node_to_model_class (field .attname ,
@@ -132,7 +141,13 @@ def _is_manager_any(self, typ: Instance) -> bool:
132
141
def run_with_model_cls (self , model_cls : Type [Model ]) -> None :
133
142
for manager_name , manager in model_cls ._meta .managers_map .items ():
134
143
manager_fullname = helpers .get_class_fullname (manager .__class__ )
135
- manager_info = self .lookup_typeinfo_or_incomplete_defn_error (manager_fullname )
144
+ try :
145
+ manager_info = self .lookup_typeinfo_or_incomplete_defn_error (manager_fullname )
146
+ except helpers .IncompleteDefnException as exc :
147
+ if not self .api .final_iteration :
148
+ raise exc
149
+ else :
150
+ continue
136
151
137
152
if manager_name not in self .model_classdef .info .names :
138
153
manager_type = Instance (manager_info , [Instance (self .model_classdef .info , [])])
@@ -143,15 +158,21 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
143
158
if has_manager_any_base :
144
159
custom_model_manager_name = manager .model .__name__ + '_' + manager .__class__ .__name__
145
160
146
- bases = []
147
- for original_base in manager_info .bases :
148
- if self ._is_manager_any (original_base ):
149
- if original_base .type is None :
150
- raise helpers .IncompleteDefnException ()
151
-
152
- original_base = helpers .reparametrize_instance (original_base ,
153
- [Instance (self .model_classdef .info , [])])
154
- bases .append (original_base )
161
+ try :
162
+ bases = []
163
+ for original_base in manager_info .bases :
164
+ if self ._is_manager_any (original_base ):
165
+ if original_base .type is None :
166
+ raise helpers .IncompleteDefnException ()
167
+
168
+ original_base = helpers .reparametrize_instance (original_base ,
169
+ [Instance (self .model_classdef .info , [])])
170
+ bases .append (original_base )
171
+ except helpers .IncompleteDefnException as exc :
172
+ if not self .api .final_iteration :
173
+ raise exc
174
+ else :
175
+ continue
155
176
156
177
current_module = self .api .modules [self .model_classdef .info .module_name ]
157
178
custom_manager_info = helpers .add_new_class_for_module (current_module ,
@@ -223,13 +244,26 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
223
244
if related_model_cls is None :
224
245
continue
225
246
226
- related_model_info = self .lookup_class_typeinfo_or_incomplete_defn_error (related_model_cls )
247
+ try :
248
+ related_model_info = self .lookup_class_typeinfo_or_incomplete_defn_error (related_model_cls )
249
+ except helpers .IncompleteDefnException as exc :
250
+ if not self .api .final_iteration :
251
+ raise exc
252
+ else :
253
+ continue
254
+
227
255
if isinstance (relation , OneToOneRel ):
228
256
self .add_new_node_to_model_class (attname , Instance (related_model_info , []))
229
257
continue
230
258
231
259
if isinstance (relation , (ManyToOneRel , ManyToManyRel )):
232
- manager_info = self .lookup_typeinfo_or_incomplete_defn_error (fullnames .RELATED_MANAGER_CLASS_FULLNAME )
260
+ try :
261
+ manager_info = self .lookup_typeinfo_or_incomplete_defn_error (fullnames .RELATED_MANAGER_CLASS )
262
+ except helpers .IncompleteDefnException as exc :
263
+ if not self .api .final_iteration :
264
+ raise exc
265
+ else :
266
+ continue
233
267
self .add_new_node_to_model_class (attname ,
234
268
Instance (manager_info , [Instance (related_model_info , [])]))
235
269
continue
0 commit comments