support for models.Model.objects, abstract mixins

This commit is contained in:
Maxim Kurnikov
2018-11-14 02:33:50 +03:00
parent 9a68263257
commit 41cc79b957
13 changed files with 200 additions and 103 deletions

View File

@@ -1,25 +1,14 @@
from typing import Optional, Callable
from mypy.plugin import Plugin, FunctionContext
from mypy.plugin import FunctionContext
from mypy.types import Type
from mypy_django_plugin import helpers
def determine_type_of_array_field(ctx: FunctionContext) -> Type:
assert 'base_field' in ctx.context.arg_names
base_field_arg_index = ctx.context.arg_names.index('base_field')
base_field_arg_type = ctx.arg_types[base_field_arg_index][0]
signature = helpers.get_call_signature_or_none(ctx)
if signature is None:
return ctx.default_return_type
_, base_field_arg_type = signature['base_field']
return ctx.api.named_generic_type(ctx.context.callee.fullname,
args=[base_field_arg_type.type.names['__get__'].type.ret_type])
class PostgresFieldsPlugin(Plugin):
def get_function_hook(self, fullname: str
) -> Optional[Callable[[FunctionContext], Type]]:
if fullname == 'django.contrib.postgres.fields.array.ArrayField':
return determine_type_of_array_field
return None
def plugin(version):
return PostgresFieldsPlugin