support for some Field -> builtin type mapping, OneToOneField, ForeignKey

This commit is contained in:
Maxim Kurnikov
2018-10-04 03:32:58 +03:00
parent 97ec73411e
commit 3d7b517e59
8 changed files with 164 additions and 167 deletions

View File

View File

@@ -0,0 +1,77 @@
from typing import Callable, Optional
from mypy.nodes import AssignmentStmt, CallExpr, RefExpr, StrExpr
from mypy.plugin import Plugin, ClassDefContext
from mypy_django_plugin.helpers import get_app_model
from mypy_django_plugin.model_classes import DjangoModelsRegistry
# fields which real type is inside to= expression
REFERENCING_DB_FIELDS = {
'django.db.models.fields.related.ForeignKey',
'django.db.models.fields.related.OneToOneField'
}
def save_referred_to_model_in_metadata(rvalue: CallExpr) -> None:
to_arg_value = rvalue.args[rvalue.arg_names.index('to')]
if isinstance(to_arg_value, StrExpr):
referred_model_fullname = get_app_model(to_arg_value.value)
else:
referred_model_fullname = to_arg_value.fullname
rvalue.callee.node.metadata['base'] = referred_model_fullname
class CollectModelsInformation(object):
def __init__(self, model_registry: DjangoModelsRegistry):
self.model_registry = model_registry
def __call__(self, model_definition: ClassDefContext) -> None:
self.model_registry.base_models.add(model_definition.cls.fullname)
for member in model_definition.cls.defs.body:
if isinstance(member, AssignmentStmt):
if len(member.lvalues) > 1:
return None
arg_name = member.lvalues[0].name
arg_name_as_id = arg_name + '_id'
rvalue = member.rvalue
if isinstance(rvalue, CallExpr):
if not isinstance(rvalue.callee, RefExpr):
return None
if rvalue.callee.fullname in REFERENCING_DB_FIELDS:
if rvalue.callee.fullname == 'django.db.models.fields.related.ForeignKey':
model_definition.cls.info.names[arg_name_as_id] = \
model_definition.api.lookup_fully_qualified('builtins.int')
if rvalue.callee.fullname == 'django.db.models.fields.related.OneToOneField':
if 'related_name' in rvalue.arg_names:
referred_to_model = rvalue.args[rvalue.arg_names.index('to')]
related_arg_value = rvalue.args[rvalue.arg_names.index('related_name')].value
if isinstance(referred_to_model, StrExpr):
referred_model_fullname = get_app_model(referred_to_model.value)
else:
referred_model_fullname = referred_to_model.fullname
referred_model = model_definition.api.lookup_fully_qualified_or_none(referred_model_fullname)
referred_model.node.names[related_arg_value] = \
model_definition.api.lookup_fully_qualified_or_none(model_definition.cls.fullname)
return save_referred_to_model_in_metadata(rvalue)
class BaseDjangoModelsPlugin(Plugin):
model_registry = DjangoModelsRegistry()
def get_base_class_hook(self, fullname: str
) -> Optional[Callable[[ClassDefContext], None]]:
if fullname in self.model_registry:
return CollectModelsInformation(self.model_registry)
return None

View File

@@ -0,0 +1,60 @@
from typing import Optional, Callable
from mypy.plugin import AttributeContext
from mypy.types import Type, Instance
from mypy_django_plugin.helpers import lookup_django_model
from mypy_django_plugin.model_classes import DjangoModelsRegistry
from mypy_django_plugin.plugins.base import BaseDjangoModelsPlugin
# mapping between field types and plain python types
DB_FIELDS_TO_TYPES = {
'django.db.models.fields.CharField': 'builtins.str',
'django.db.models.fields.TextField': 'builtins.str',
'django.db.models.fields.BooleanField': 'builtins.bool',
# 'django.db.models.fields.NullBooleanField': 'typing.Optional[builtins.bool]',
'django.db.models.fields.IntegerField': 'builtins.int',
'django.db.models.fields.AutoField': 'builtins.int',
'django.db.models.fields.FloatField': 'builtins.float',
'django.contrib.postgres.fields.jsonb.JSONField': 'builtins.dict',
'django.contrib.postgres.fields.array.ArrayField': 'typing.Iterable'
}
class DetermineFieldPythonTypeCallback(object):
def __init__(self, models_registry: DjangoModelsRegistry):
self.models_registry = models_registry
def __call__(self, attr_context: AttributeContext) -> Type:
default_attr_type = attr_context.default_attr_type
if isinstance(default_attr_type, Instance):
attr_type_fullname = default_attr_type.type.fullname()
if attr_type_fullname in DB_FIELDS_TO_TYPES:
return attr_context.api.named_type(DB_FIELDS_TO_TYPES[attr_type_fullname])
if 'base' in default_attr_type.type.metadata:
referred_base_model = default_attr_type.type.metadata['base']
try:
node = lookup_django_model(attr_context.api, referred_base_model).node
return Instance(node, [])
except AssertionError as e:
print(e)
print('name to lookup:', referred_base_model)
pass
return default_attr_type
class FieldToPythonTypePlugin(BaseDjangoModelsPlugin):
def get_attribute_hook(self, fullname: str
) -> Optional[Callable[[AttributeContext], Type]]:
classname, _, attrname = fullname.rpartition('.')
if classname and classname in self.model_registry:
return DetermineFieldPythonTypeCallback(self.model_registry)
return None
def plugin(version):
return FieldToPythonTypePlugin