Skip to content

Commit dacf88c

Browse files
committed
optimize hooks a bit
1 parent 3d14d07 commit dacf88c

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

mypy_django_plugin/main.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
2-
from typing import Callable, Optional, Set, Union, cast, Dict
2+
from typing import Callable, Dict, Optional, Union, cast
33

44
from mypy.checker import TypeChecker
55
from mypy.nodes import MemberExpr, TypeInfo
66
from mypy.options import Options
77
from mypy.plugin import AttributeContext, ClassDefContext, FunctionContext, MethodContext, Plugin
88
from mypy.types import AnyType, Instance, Type, TypeOfAny, TypeType, UnionType
9+
910
from mypy_django_plugin import helpers, monkeypatch
1011
from mypy_django_plugin.config import Config
1112
from mypy_django_plugin.transformers import fields, init_create
@@ -211,10 +212,6 @@ def _get_current_modelform_bases(self) -> Dict[str, int]:
211212

212213
def get_function_hook(self, fullname: str
213214
) -> Optional[Callable[[FunctionContext], Type]]:
214-
sym = self.lookup_fully_qualified(fullname)
215-
if sym and isinstance(sym.node, TypeInfo) and sym.node.has_base(helpers.FIELD_FULLNAME):
216-
return fields.adjust_return_type_of_field_instantiation
217-
218215
if fullname == 'django.contrib.auth.get_user_model':
219216
return return_user_model_hook
220217

@@ -223,24 +220,34 @@ def get_function_hook(self, fullname: str
223220
return determine_proper_manager_type
224221

225222
sym = self.lookup_fully_qualified(fullname)
226-
if sym and isinstance(sym.node, TypeInfo):
223+
if sym is not None and isinstance(sym.node, TypeInfo):
224+
if sym.node.has_base(helpers.FIELD_FULLNAME):
225+
return fields.adjust_return_type_of_field_instantiation
226+
227227
if sym.node.metadata.get('django', {}).get('generated_init'):
228228
return init_create.redefine_and_typecheck_model_init
229229

230230
def get_method_hook(self, fullname: str
231231
) -> Optional[Callable[[MethodContext], Type]]:
232+
if fullname in {'django.apps.registry.Apps.get_model',
233+
'django.db.migrations.state.StateApps.get_model'}:
234+
return determine_model_cls_from_string_for_migrations
235+
232236
manager_classes = self._get_current_manager_bases()
233237
class_fullname, _, method_name = fullname.rpartition('.')
234238
if class_fullname in manager_classes and method_name == 'create':
235239
return init_create.redefine_and_typecheck_model_create
236-
237-
if fullname in {'django.apps.registry.Apps.get_model',
238-
'django.db.migrations.state.StateApps.get_model'}:
239-
return determine_model_cls_from_string_for_migrations
240240
return None
241241

242242
def get_base_class_hook(self, fullname: str
243243
) -> Optional[Callable[[ClassDefContext], None]]:
244+
if fullname == helpers.DUMMY_SETTINGS_BASE_CLASS:
245+
settings_modules = ['django.conf.global_settings']
246+
if self.django_settings_module:
247+
settings_modules.append(self.django_settings_module)
248+
return AddSettingValuesToDjangoConfObject(settings_modules,
249+
self.config.ignore_missing_settings)
250+
244251
if fullname in self._get_current_model_bases():
245252
return transform_model_class
246253

@@ -250,27 +257,20 @@ def get_base_class_hook(self, fullname: str
250257
if fullname in self._get_current_modelform_bases():
251258
return transform_modelform_class
252259

253-
if fullname == helpers.DUMMY_SETTINGS_BASE_CLASS:
254-
settings_modules = ['django.conf.global_settings']
255-
if self.django_settings_module:
256-
settings_modules.append(self.django_settings_module)
257-
return AddSettingValuesToDjangoConfObject(settings_modules,
258-
self.config.ignore_missing_settings)
259-
260260
return None
261261

262262
def get_attribute_hook(self, fullname: str
263263
) -> Optional[Callable[[AttributeContext], Type]]:
264+
if fullname == 'builtins.object.id':
265+
return return_integer_type_for_id_for_non_defined_primary_key_in_models
266+
264267
module, _, name = fullname.rpartition('.')
265268
sym = self.lookup_fully_qualified('django.conf.LazySettings')
266269
if sym and isinstance(sym.node, TypeInfo):
267270
metadata = get_settings_metadata(sym.node)
268271
if module == 'builtins.object' and name in metadata:
269272
return ExtractSettingType(module_fullname=metadata[name])
270273

271-
if fullname == 'builtins.object.id':
272-
return return_integer_type_for_id_for_non_defined_primary_key_in_models
273-
274274
return extract_and_return_primary_key_of_bound_related_field_parameter
275275

276276

0 commit comments

Comments
 (0)