mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-06 20:24:31 +08:00
remove old semanal plugin data
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
import os
|
||||
from configparser import ConfigParser
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
import dataclasses
|
||||
from dataclasses import dataclass
|
||||
from pytest_mypy.utils import temp_environ
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
django_settings_module: Optional[str] = None
|
||||
installed_apps: List[str] = dataclasses.field(default_factory=list)
|
||||
|
||||
ignore_missing_settings: bool = False
|
||||
ignore_missing_model_attributes: bool = False
|
||||
|
||||
@classmethod
|
||||
def from_config_file(cls, fpath: str) -> 'Config':
|
||||
ini_config = ConfigParser()
|
||||
ini_config.read(fpath)
|
||||
if not ini_config.has_section('mypy_django_plugin'):
|
||||
raise ValueError('Invalid config file: no [mypy_django_plugin] section')
|
||||
|
||||
django_settings = ini_config.get('mypy_django_plugin', 'django_settings',
|
||||
fallback=None)
|
||||
if django_settings:
|
||||
django_settings = django_settings.strip()
|
||||
|
||||
return Config(django_settings_module=django_settings,
|
||||
ignore_missing_settings=bool(ini_config.get('mypy_django_plugin',
|
||||
'ignore_missing_settings',
|
||||
fallback=False)),
|
||||
ignore_missing_model_attributes=bool(ini_config.get('mypy_django_plugin',
|
||||
'ignore_missing_model_attributes',
|
||||
fallback=False)))
|
||||
|
||||
|
||||
def extract_app_model_aliases(settings_module: str) -> Dict[str, str]:
|
||||
with temp_environ():
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
|
||||
import django
|
||||
django.setup()
|
||||
|
||||
app_model_mapping: Dict[str, str] = {}
|
||||
|
||||
from django.apps import apps
|
||||
|
||||
for name, app_config in apps.app_configs.items():
|
||||
app_label = app_config.label
|
||||
for model_name, model_class in app_config.models.items():
|
||||
app_model_mapping[app_label + '.' + model_class.__name__] = model_class.__module__ + '.' + model_class.__name__
|
||||
|
||||
return app_model_mapping
|
||||
@@ -1,35 +0,0 @@
|
||||
|
||||
MODEL_CLASS_FULLNAME = 'django.db.models.base.Model'
|
||||
FIELD_FULLNAME = 'django.db.models.fields.Field'
|
||||
CHAR_FIELD_FULLNAME = 'django.db.models.fields.CharField'
|
||||
ARRAY_FIELD_FULLNAME = 'django.contrib.postgres.fields.array.ArrayField'
|
||||
AUTO_FIELD_FULLNAME = 'django.db.models.fields.AutoField'
|
||||
GENERIC_FOREIGN_KEY_FULLNAME = 'django.contrib.contenttypes.fields.GenericForeignKey'
|
||||
FOREIGN_KEY_FULLNAME = 'django.db.models.fields.related.ForeignKey'
|
||||
ONETOONE_FIELD_FULLNAME = 'django.db.models.fields.related.OneToOneField'
|
||||
MANYTOMANY_FIELD_FULLNAME = 'django.db.models.fields.related.ManyToManyField'
|
||||
DUMMY_SETTINGS_BASE_CLASS = 'django.conf._DjangoConfLazyObject'
|
||||
|
||||
QUERYSET_CLASS_FULLNAME = 'django.db.models.query.QuerySet'
|
||||
BASE_MANAGER_CLASS_FULLNAME = 'django.db.models.manager.BaseManager'
|
||||
MANAGER_CLASS_FULLNAME = 'django.db.models.manager.Manager'
|
||||
RELATED_MANAGER_CLASS_FULLNAME = 'django.db.models.manager.RelatedManager'
|
||||
|
||||
BASEFORM_CLASS_FULLNAME = 'django.forms.forms.BaseForm'
|
||||
FORM_CLASS_FULLNAME = 'django.forms.forms.Form'
|
||||
MODELFORM_CLASS_FULLNAME = 'django.forms.models.ModelForm'
|
||||
|
||||
FORM_MIXIN_CLASS_FULLNAME = 'django.views.generic.edit.FormMixin'
|
||||
|
||||
MANAGER_CLASSES = {
|
||||
MANAGER_CLASS_FULLNAME,
|
||||
RELATED_MANAGER_CLASS_FULLNAME,
|
||||
BASE_MANAGER_CLASS_FULLNAME,
|
||||
QUERYSET_CLASS_FULLNAME
|
||||
}
|
||||
|
||||
RELATED_FIELDS_CLASSES = {
|
||||
FOREIGN_KEY_FULLNAME,
|
||||
ONETOONE_FIELD_FULLNAME,
|
||||
MANYTOMANY_FIELD_FULLNAME
|
||||
}
|
||||
@@ -1,452 +0,0 @@
|
||||
from collections import OrderedDict
|
||||
from typing import Dict, Iterator, List, Optional, Set, TYPE_CHECKING, Tuple, Union, cast
|
||||
|
||||
from mypy.mro import calculate_mro
|
||||
from mypy.nodes import (AssignmentStmt, Block, CallExpr, ClassDef, Expression, FakeInfo, GDEF, ImportedName, Lvalue, MDEF,
|
||||
MemberExpr, MypyFile, NameExpr, SymbolNode, SymbolTable, SymbolTableNode, TypeInfo, Var)
|
||||
from mypy.plugin import CheckerPluginInterface, FunctionContext, MethodContext
|
||||
from mypy.types import (AnyType, Instance, NoneTyp, TupleType, Type as MypyType, TypeOfAny, TypeVarType, TypedDictType,
|
||||
UnionType)
|
||||
|
||||
from mypy_django_plugin.lib import fullnames, metadata
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mypy.checker import TypeChecker
|
||||
|
||||
|
||||
def get_models_file(app_name: str, all_modules: Dict[str, MypyFile]) -> Optional[MypyFile]:
|
||||
models_module = '.'.join([app_name, 'models'])
|
||||
return all_modules.get(models_module)
|
||||
|
||||
|
||||
def get_model_fullname(app_name: str, model_name: str,
|
||||
all_modules: Dict[str, MypyFile]) -> Optional[str]:
|
||||
models_file = get_models_file(app_name, all_modules)
|
||||
if models_file is None:
|
||||
# not imported so far, not supported
|
||||
return None
|
||||
sym = models_file.names.get(model_name)
|
||||
if not sym:
|
||||
return None
|
||||
|
||||
if isinstance(sym.node, TypeInfo):
|
||||
return sym.node.fullname()
|
||||
elif isinstance(sym.node, ImportedName):
|
||||
return sym.node.target_fullname
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class SameFileModel(Exception):
|
||||
def __init__(self, model_cls_name: str):
|
||||
self.model_cls_name = model_cls_name
|
||||
|
||||
|
||||
class SelfReference(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def get_model_fullname_from_string(model_string: str,
|
||||
all_modules: Dict[str, MypyFile]) -> Optional[str]:
|
||||
if model_string == 'self':
|
||||
raise SelfReference()
|
||||
|
||||
if '.' not in model_string:
|
||||
raise SameFileModel(model_string)
|
||||
|
||||
app_name, model_name = model_string.split('.')
|
||||
return get_model_fullname(app_name, model_name, all_modules)
|
||||
|
||||
|
||||
def lookup_fully_qualified_generic(name: str, all_modules: Dict[str, MypyFile]) -> Optional[SymbolNode]:
|
||||
if '.' not in name:
|
||||
return None
|
||||
module, cls_name = name.rsplit('.', 1)
|
||||
|
||||
module_file = all_modules.get(module)
|
||||
if module_file is None:
|
||||
return None
|
||||
sym = module_file.names.get(cls_name)
|
||||
if sym is None:
|
||||
return None
|
||||
return sym.node
|
||||
|
||||
|
||||
def parse_bool(expr: Expression) -> Optional[bool]:
|
||||
if isinstance(expr, NameExpr):
|
||||
if expr.fullname == 'builtins.True':
|
||||
return True
|
||||
if expr.fullname == 'builtins.False':
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def reparametrize_instance(instance: Instance, new_args: List[MypyType]) -> Instance:
|
||||
return Instance(instance.type, args=new_args,
|
||||
line=instance.line, column=instance.column)
|
||||
|
||||
|
||||
def fill_typevars_with_any(instance: Instance) -> Instance:
|
||||
return reparametrize_instance(instance, [AnyType(TypeOfAny.unannotated)])
|
||||
|
||||
|
||||
def extract_typevar_value(tp: Instance, typevar_name: str) -> MypyType:
|
||||
if typevar_name in {'_T', '_T_co'}:
|
||||
if '_T' in tp.type.type_vars:
|
||||
return tp.args[tp.type.type_vars.index('_T')]
|
||||
if '_T_co' in tp.type.type_vars:
|
||||
return tp.args[tp.type.type_vars.index('_T_co')]
|
||||
return tp.args[tp.type.type_vars.index(typevar_name)]
|
||||
|
||||
|
||||
def fill_typevars(tp: Instance, type_to_fill: Instance) -> Instance:
|
||||
typevar_values: List[MypyType] = []
|
||||
for typevar_arg in type_to_fill.args:
|
||||
if isinstance(typevar_arg, TypeVarType):
|
||||
typevar_values.append(extract_typevar_value(tp, typevar_arg.name))
|
||||
return Instance(type_to_fill.type, typevar_values)
|
||||
|
||||
|
||||
def get_call_argument_by_name(ctx: Union[FunctionContext, MethodContext], name: str) -> Optional[Expression]:
|
||||
"""
|
||||
Return the expression for the specific argument.
|
||||
This helper should only be used with non-star arguments.
|
||||
"""
|
||||
if name not in ctx.callee_arg_names:
|
||||
return None
|
||||
idx = ctx.callee_arg_names.index(name)
|
||||
args = ctx.args[idx]
|
||||
if len(args) != 1:
|
||||
# Either an error or no value passed.
|
||||
return None
|
||||
return args[0]
|
||||
|
||||
|
||||
def get_call_argument_type_by_name(ctx: Union[FunctionContext, MethodContext], name: str) -> Optional[MypyType]:
|
||||
"""Return the type for the specific argument.
|
||||
|
||||
This helper should only be used with non-star arguments.
|
||||
"""
|
||||
if name not in ctx.callee_arg_names:
|
||||
return None
|
||||
idx = ctx.callee_arg_names.index(name)
|
||||
arg_types = ctx.arg_types[idx]
|
||||
if len(arg_types) != 1:
|
||||
# Either an error or no value passed.
|
||||
return None
|
||||
return arg_types[0]
|
||||
|
||||
|
||||
def get_setting_expr(api: 'TypeChecker', setting_name: str) -> Optional[Expression]:
|
||||
try:
|
||||
settings_sym = api.modules['django.conf'].names['settings']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
settings_type: TypeInfo = settings_sym.type.type
|
||||
auth_user_model_sym = settings_type.get(setting_name)
|
||||
if not auth_user_model_sym:
|
||||
return None
|
||||
|
||||
module, _, name = auth_user_model_sym.fullname.rpartition('.')
|
||||
if module not in api.modules:
|
||||
return None
|
||||
|
||||
module_file = api.modules.get(module)
|
||||
for name_expr, value_expr in iter_over_module_level_assignments(module_file):
|
||||
if isinstance(name_expr, NameExpr) and name_expr.name == setting_name:
|
||||
return value_expr
|
||||
return None
|
||||
|
||||
|
||||
def iter_over_class_level_assignments(klass: ClassDef) -> Iterator[Tuple[str, Expression]]:
|
||||
for stmt in klass.defs.body:
|
||||
if not isinstance(stmt, AssignmentStmt):
|
||||
continue
|
||||
if len(stmt.lvalues) > 1:
|
||||
# skip multiple assignments
|
||||
continue
|
||||
lvalue = stmt.lvalues[0]
|
||||
if isinstance(lvalue, NameExpr):
|
||||
yield lvalue.name, stmt.rvalue
|
||||
|
||||
|
||||
def iter_over_module_level_assignments(module: MypyFile) -> Iterator[Tuple[str, Expression]]:
|
||||
for stmt in module.defs:
|
||||
if not isinstance(stmt, AssignmentStmt):
|
||||
continue
|
||||
if len(stmt.lvalues) > 1:
|
||||
# skip multiple assignments
|
||||
continue
|
||||
lvalue = stmt.lvalues[0]
|
||||
if isinstance(lvalue, NameExpr):
|
||||
yield lvalue.name, stmt.rvalue
|
||||
|
||||
|
||||
def iter_over_assignments_in_class(class_or_module: Union[ClassDef, MypyFile]
|
||||
) -> Iterator[Tuple[str, Expression]]:
|
||||
if isinstance(class_or_module, ClassDef):
|
||||
statements = class_or_module.defs.body
|
||||
else:
|
||||
statements = class_or_module.defs
|
||||
|
||||
for stmt in statements:
|
||||
if not isinstance(stmt, AssignmentStmt):
|
||||
continue
|
||||
if len(stmt.lvalues) > 1:
|
||||
# not supported yet
|
||||
continue
|
||||
lvalue = stmt.lvalues[0]
|
||||
if isinstance(lvalue, NameExpr):
|
||||
yield lvalue.name, stmt.rvalue
|
||||
|
||||
|
||||
def extract_field_setter_type(tp: Instance) -> Optional[MypyType]:
|
||||
""" Extract __set__ value of a field. """
|
||||
if tp.type.has_base(fullnames.FIELD_FULLNAME):
|
||||
return tp.args[0]
|
||||
# GenericForeignKey
|
||||
if tp.type.has_base(fullnames.GENERIC_FOREIGN_KEY_FULLNAME):
|
||||
return AnyType(TypeOfAny.special_form)
|
||||
return None
|
||||
|
||||
|
||||
def extract_field_getter_type(tp: MypyType) -> Optional[MypyType]:
|
||||
""" Extract return type of __get__ of subclass of Field"""
|
||||
if not isinstance(tp, Instance):
|
||||
return None
|
||||
if tp.type.has_base(fullnames.FIELD_FULLNAME):
|
||||
return tp.args[1]
|
||||
# GenericForeignKey
|
||||
if tp.type.has_base(fullnames.GENERIC_FOREIGN_KEY_FULLNAME):
|
||||
return AnyType(TypeOfAny.special_form)
|
||||
return None
|
||||
|
||||
|
||||
def extract_explicit_set_type_of_model_primary_key(model: TypeInfo) -> Optional[MypyType]:
|
||||
"""
|
||||
If field with primary_key=True is set on the model, extract its __set__ type.
|
||||
"""
|
||||
for field_name, props in metadata.get_fields_metadata(model).items():
|
||||
is_primary_key = props.get('primary_key', False)
|
||||
if is_primary_key:
|
||||
return extract_field_setter_type(model.names[field_name].type)
|
||||
return None
|
||||
|
||||
|
||||
def extract_primary_key_type_for_get(model: TypeInfo) -> Optional[MypyType]:
|
||||
for field_name, props in metadata.get_fields_metadata(model).items():
|
||||
is_primary_key = props.get('primary_key', False)
|
||||
if is_primary_key:
|
||||
return extract_field_getter_type(model.names[field_name].type)
|
||||
return None
|
||||
|
||||
|
||||
def make_optional(typ: MypyType) -> MypyType:
|
||||
return UnionType.make_union([typ, NoneTyp()])
|
||||
|
||||
|
||||
def make_required(typ: MypyType) -> MypyType:
|
||||
if not isinstance(typ, UnionType):
|
||||
return typ
|
||||
items = [item for item in typ.items if not isinstance(item, NoneTyp)]
|
||||
# will reduce to Instance, if only one item
|
||||
return UnionType.make_union(items)
|
||||
|
||||
|
||||
def is_optional(typ: MypyType) -> bool:
|
||||
if not isinstance(typ, UnionType):
|
||||
return False
|
||||
|
||||
return any([isinstance(item, NoneTyp) for item in typ.items])
|
||||
|
||||
|
||||
def has_any_of_bases(info: TypeInfo, bases: Set[str]) -> bool:
|
||||
for base_fullname in bases:
|
||||
if info.has_base(base_fullname):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def is_none_expr(expr: Expression) -> bool:
|
||||
return isinstance(expr, NameExpr) and expr.fullname == 'builtins.None'
|
||||
|
||||
|
||||
def get_nested_meta_node_for_current_class(info: TypeInfo) -> Optional[TypeInfo]:
|
||||
metaclass_sym = info.names.get('Meta')
|
||||
if metaclass_sym is not None and isinstance(metaclass_sym.node, TypeInfo):
|
||||
return metaclass_sym.node
|
||||
return None
|
||||
|
||||
|
||||
def get_assignment_stmt_by_name(type_info: TypeInfo, name: str) -> Optional[Expression]:
|
||||
for assignment_name, call_expr in iter_over_class_level_assignments(type_info.defn):
|
||||
if assignment_name == name:
|
||||
return call_expr
|
||||
return None
|
||||
|
||||
|
||||
def is_field_nullable(model: TypeInfo, field_name: str) -> bool:
|
||||
return metadata.get_fields_metadata(model).get(field_name, {}).get('null', False)
|
||||
|
||||
|
||||
def is_foreign_key_like(t: MypyType) -> bool:
|
||||
if not isinstance(t, Instance):
|
||||
return False
|
||||
return has_any_of_bases(t.type, {fullnames.FOREIGN_KEY_FULLNAME, fullnames.ONETOONE_FIELD_FULLNAME})
|
||||
|
||||
|
||||
def build_class_with_annotated_fields(api: 'TypeChecker', base: MypyType, fields: 'OrderedDict[str, MypyType]',
|
||||
name: str) -> Instance:
|
||||
"""Build an Instance with `name` that contains the specified `fields` as attributes and extends `base`."""
|
||||
# Credit: This code is largely copied/modified from TypeChecker.intersect_instance_callable and
|
||||
# NamedTupleAnalyzer.build_namedtuple_typeinfo
|
||||
from mypy.checker import gen_unique_name
|
||||
|
||||
cur_module = cast(MypyFile, api.scope.stack[0])
|
||||
gen_name = gen_unique_name(name, cur_module.names)
|
||||
|
||||
cdef = ClassDef(name, Block([]))
|
||||
cdef.fullname = cur_module.fullname() + '.' + gen_name
|
||||
info = TypeInfo(SymbolTable(), cdef, cur_module.fullname())
|
||||
cdef.info = info
|
||||
info.bases = [base]
|
||||
|
||||
def add_field(var: Var, is_initialized_in_class: bool = False,
|
||||
is_property: bool = False) -> None:
|
||||
var.info = info
|
||||
var.is_initialized_in_class = is_initialized_in_class
|
||||
var.is_property = is_property
|
||||
var._fullname = '%s.%s' % (info.fullname(), var.name())
|
||||
info.names[var.name()] = SymbolTableNode(MDEF, var)
|
||||
|
||||
vars = [Var(item, typ) for item, typ in fields.items()]
|
||||
for var in vars:
|
||||
add_field(var, is_property=True)
|
||||
|
||||
calculate_mro(info)
|
||||
info.calculate_metaclass_type()
|
||||
|
||||
cur_module.names[gen_name] = SymbolTableNode(GDEF, info, plugin_generated=True)
|
||||
return Instance(info, [])
|
||||
|
||||
|
||||
def make_named_tuple(api: 'TypeChecker', fields: 'OrderedDict[str, MypyType]', name: str) -> MypyType:
|
||||
if not fields:
|
||||
# No fields specified, so fallback to a subclass of NamedTuple that allows
|
||||
# __getattr__ / __setattr__ for any attribute name.
|
||||
fallback = api.named_generic_type('django._NamedTupleAnyAttr', [])
|
||||
else:
|
||||
fallback = build_class_with_annotated_fields(
|
||||
api=api,
|
||||
base=api.named_generic_type('NamedTuple', []),
|
||||
fields=fields,
|
||||
name=name
|
||||
)
|
||||
return TupleType(list(fields.values()), fallback=fallback)
|
||||
|
||||
|
||||
def make_typeddict(api: CheckerPluginInterface, fields: 'OrderedDict[str, MypyType]',
|
||||
required_keys: Set[str]) -> TypedDictType:
|
||||
object_type = api.named_generic_type('mypy_extensions._TypedDict', [])
|
||||
typed_dict_type = TypedDictType(fields, required_keys=required_keys, fallback=object_type)
|
||||
return typed_dict_type
|
||||
|
||||
|
||||
def make_tuple(api: 'TypeChecker', fields: List[MypyType]) -> TupleType:
|
||||
implicit_any = AnyType(TypeOfAny.special_form)
|
||||
fallback = api.named_generic_type('builtins.tuple', [implicit_any])
|
||||
return TupleType(fields, fallback=fallback)
|
||||
|
||||
|
||||
def get_private_descriptor_type(type_info: TypeInfo, private_field_name: str, is_nullable: bool) -> MypyType:
|
||||
node = type_info.get(private_field_name).node
|
||||
if isinstance(node, Var):
|
||||
descriptor_type = node.type
|
||||
if is_nullable:
|
||||
descriptor_type = make_optional(descriptor_type)
|
||||
return descriptor_type
|
||||
return AnyType(TypeOfAny.unannotated)
|
||||
|
||||
|
||||
class IncompleteDefnException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def iter_over_toplevel_classes(module_file: MypyFile) -> Iterator[ClassDef]:
|
||||
for defn in module_file.defs:
|
||||
if isinstance(defn, ClassDef):
|
||||
yield defn
|
||||
|
||||
|
||||
def iter_call_assignments_in_class(klass: ClassDef) -> Iterator[Tuple[str, CallExpr]]:
|
||||
for name, expression in iter_over_assignments_in_class(klass):
|
||||
if isinstance(expression, CallExpr):
|
||||
yield name, expression
|
||||
|
||||
|
||||
def iter_over_field_inits_in_class(klass: ClassDef) -> Iterator[Tuple[str, CallExpr]]:
|
||||
for lvalue, rvalue in iter_over_assignments_in_class(klass):
|
||||
if isinstance(lvalue, NameExpr) and isinstance(rvalue, CallExpr):
|
||||
field_name = lvalue.name
|
||||
if isinstance(rvalue.callee, MemberExpr) and isinstance(rvalue.callee.node, TypeInfo):
|
||||
if isinstance(rvalue.callee.node, FakeInfo):
|
||||
raise IncompleteDefnException()
|
||||
|
||||
field_info = rvalue.callee.node
|
||||
if field_info.has_base(fullnames.FIELD_FULLNAME):
|
||||
yield field_name, rvalue
|
||||
|
||||
|
||||
def get_related_manager_type_from_metadata(model_info: TypeInfo, related_manager_name: str,
|
||||
api: CheckerPluginInterface) -> Optional[Instance]:
|
||||
related_manager_metadata = metadata.get_related_managers_metadata(model_info)
|
||||
if not related_manager_metadata:
|
||||
return None
|
||||
|
||||
if related_manager_name not in related_manager_metadata:
|
||||
return None
|
||||
|
||||
manager_class_name = related_manager_metadata[related_manager_name]['manager']
|
||||
of = related_manager_metadata[related_manager_name]['of']
|
||||
of_types = []
|
||||
for of_type_name in of:
|
||||
if of_type_name == 'any':
|
||||
of_types.append(AnyType(TypeOfAny.implementation_artifact))
|
||||
else:
|
||||
try:
|
||||
of_type = api.named_generic_type(of_type_name, [])
|
||||
except AssertionError:
|
||||
# Internal error: attempted lookup of unknown name
|
||||
of_type = AnyType(TypeOfAny.implementation_artifact)
|
||||
|
||||
of_types.append(of_type)
|
||||
|
||||
return api.named_generic_type(manager_class_name, of_types)
|
||||
|
||||
|
||||
def get_primary_key_field_name(model_info: TypeInfo) -> Optional[str]:
|
||||
for base in model_info.mro:
|
||||
fields = metadata.get_fields_metadata(base)
|
||||
for field_name, field_props in fields.items():
|
||||
is_primary_key = field_props.get('primary_key', False)
|
||||
if is_primary_key:
|
||||
return field_name
|
||||
return None
|
||||
|
||||
|
||||
def _get_app_models_file(app_name: str, all_modules: Dict[str, MypyFile]) -> Optional[MypyFile]:
|
||||
models_module = '.'.join([app_name, 'models'])
|
||||
return all_modules.get(models_module)
|
||||
|
||||
|
||||
def get_model_info(app_name_dot_model_name: str, all_modules: Dict[str, MypyFile]) -> Optional[TypeInfo]:
|
||||
""" Resolve app_name.ModelName into model fullname """
|
||||
app_name, model_name = app_name_dot_model_name.split('.')
|
||||
models_file = _get_app_models_file(app_name, all_modules)
|
||||
if models_file is None:
|
||||
return None
|
||||
|
||||
sym = models_file.names.get(model_name)
|
||||
if sym and isinstance(sym.node, TypeInfo):
|
||||
return sym.node
|
||||
@@ -1,159 +0,0 @@
|
||||
from typing import List, Union
|
||||
|
||||
import dataclasses
|
||||
from mypy.nodes import TypeInfo
|
||||
from mypy.plugin import CheckerPluginInterface
|
||||
from mypy.types import Instance, Type
|
||||
|
||||
from mypy_django_plugin.lib import metadata, helpers
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class RelatedModelNode:
|
||||
typ: Instance
|
||||
is_nullable: bool
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class FieldNode:
|
||||
typ: Type
|
||||
|
||||
|
||||
LookupNode = Union[RelatedModelNode, FieldNode]
|
||||
|
||||
|
||||
class LookupException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def resolve_lookup(api: CheckerPluginInterface, model_type_info: TypeInfo, lookup: str) -> List[LookupNode]:
|
||||
"""Resolve a lookup str to a list of LookupNodes.
|
||||
|
||||
Each node represents a part of the lookup (separated by "__"), in order.
|
||||
Each node is the Model or Field that was resolved.
|
||||
|
||||
Raises LookupException if there were any issues resolving the lookup.
|
||||
"""
|
||||
lookup_parts = lookup.split("__")
|
||||
|
||||
nodes = []
|
||||
while lookup_parts:
|
||||
lookup_part = lookup_parts.pop(0)
|
||||
|
||||
if not nodes:
|
||||
current_node = None
|
||||
else:
|
||||
current_node = nodes[-1]
|
||||
|
||||
if current_node is None:
|
||||
new_node = resolve_model_lookup(api, model_type_info, lookup_part)
|
||||
elif isinstance(current_node, RelatedModelNode):
|
||||
new_node = resolve_model_lookup(api, current_node.typ.type, lookup_part)
|
||||
elif isinstance(current_node, FieldNode):
|
||||
raise LookupException(f"Field lookups not yet supported for lookup {lookup}")
|
||||
else:
|
||||
raise LookupException(f"Unsupported node type: {type(current_node)}")
|
||||
nodes.append(new_node)
|
||||
return nodes
|
||||
|
||||
|
||||
def resolve_model_pk_lookup(api: CheckerPluginInterface, model_type_info: TypeInfo) -> LookupNode:
|
||||
# Primary keys are special-cased
|
||||
primary_key_type = helpers.extract_primary_key_type_for_get(model_type_info)
|
||||
if primary_key_type:
|
||||
return FieldNode(primary_key_type)
|
||||
else:
|
||||
# No PK, use the get type for AutoField as PK type.
|
||||
autofield_info = api.lookup_typeinfo('django.db.models.fields.AutoField')
|
||||
pk_type = helpers.get_private_descriptor_type(autofield_info, '_pyi_private_get_type',
|
||||
is_nullable=False)
|
||||
return FieldNode(pk_type)
|
||||
|
||||
|
||||
def resolve_model_lookup(api: CheckerPluginInterface, model_type_info: TypeInfo,
|
||||
lookup: str) -> LookupNode:
|
||||
"""Resolve a lookup on the given model."""
|
||||
if lookup == 'pk':
|
||||
return resolve_model_pk_lookup(api, model_type_info)
|
||||
|
||||
field_name = get_actual_field_name_for_lookup_field(lookup, model_type_info)
|
||||
|
||||
field_node = model_type_info.get(field_name)
|
||||
if not field_node:
|
||||
raise LookupException(
|
||||
f'When resolving lookup "{lookup}", field "{field_name}" was not found in model {model_type_info.name()}')
|
||||
|
||||
if field_name.endswith('_id'):
|
||||
field_name_without_id = field_name.rstrip('_id')
|
||||
foreign_key_field = model_type_info.get(field_name_without_id)
|
||||
if foreign_key_field is not None and helpers.is_foreign_key_like(foreign_key_field.type):
|
||||
# Hack: If field ends with '_id' and there is a model field without the '_id' suffix, then use that field.
|
||||
field_node = foreign_key_field
|
||||
field_name = field_name_without_id
|
||||
|
||||
field_node_type = field_node.type
|
||||
if field_node_type is None or not isinstance(field_node_type, Instance):
|
||||
raise LookupException(
|
||||
f'When resolving lookup "{lookup}", could not determine type for {model_type_info.name()}.{field_name}')
|
||||
|
||||
if field_node_type.type.fullname() == 'builtins.object':
|
||||
# could be related manager
|
||||
related_manager_type = helpers.get_related_manager_type_from_metadata(model_type_info, field_name, api)
|
||||
if related_manager_type:
|
||||
model_arg = related_manager_type.args[0]
|
||||
if not isinstance(model_arg, Instance):
|
||||
raise LookupException(
|
||||
f'When resolving lookup "{lookup}", could not determine type '
|
||||
f'for {model_type_info.name()}.{field_name}')
|
||||
|
||||
return RelatedModelNode(typ=model_arg, is_nullable=False)
|
||||
|
||||
if helpers.is_foreign_key_like(field_node_type):
|
||||
field_type = helpers.extract_field_getter_type(field_node_type)
|
||||
is_nullable = helpers.is_optional(field_type)
|
||||
if is_nullable:
|
||||
# type is always non-optional
|
||||
field_type = helpers.make_required(field_type)
|
||||
|
||||
if isinstance(field_type, Instance):
|
||||
return RelatedModelNode(typ=field_type, is_nullable=is_nullable)
|
||||
else:
|
||||
raise LookupException(f"Not an instance for field {field_type} lookup {lookup}")
|
||||
|
||||
field_type = helpers.extract_field_getter_type(field_node_type)
|
||||
if field_type:
|
||||
return FieldNode(typ=field_type)
|
||||
|
||||
# Not a Field
|
||||
if field_name == 'id':
|
||||
# If no 'id' field was found, use an int
|
||||
return FieldNode(api.named_generic_type('builtins.int', []))
|
||||
|
||||
raise LookupException(
|
||||
f'When resolving lookup {lookup!r}, could not determine type for {model_type_info.name()}.{field_name}')
|
||||
|
||||
|
||||
def get_actual_field_name_for_lookup_field(lookup: str, model_type_info: TypeInfo) -> str:
|
||||
"""Attempt to find out the real field name if this lookup is a related_query_name (for reverse relations).
|
||||
|
||||
If it's not, return the original lookup.
|
||||
"""
|
||||
lookups_metadata = metadata.get_lookups_metadata(model_type_info)
|
||||
lookup_metadata = lookups_metadata.get(lookup)
|
||||
if lookup_metadata is None:
|
||||
# If not found on current model, look in all bases for their lookup metadata
|
||||
for base in model_type_info.mro:
|
||||
lookups_metadata = metadata.get_lookups_metadata(base)
|
||||
lookup_metadata = lookups_metadata.get(lookup)
|
||||
if lookup_metadata:
|
||||
break
|
||||
if not lookup_metadata:
|
||||
lookup_metadata = {}
|
||||
related_name = lookup_metadata.get('related_query_name_target', None)
|
||||
if related_name:
|
||||
# If the lookup is a related lookup, then look at the field specified by related_name.
|
||||
# This is to support if related_query_name is set and differs from.
|
||||
field_name = related_name
|
||||
else:
|
||||
field_name = lookup
|
||||
return field_name
|
||||
@@ -1,23 +0,0 @@
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from mypy.nodes import TypeInfo
|
||||
|
||||
|
||||
def get_django_metadata(model_info: TypeInfo) -> Dict[str, Any]:
|
||||
return model_info.metadata.setdefault('django', {})
|
||||
|
||||
|
||||
def get_related_field_primary_key_names(base_model: TypeInfo) -> List[str]:
|
||||
return get_django_metadata(base_model).setdefault('related_field_primary_keys', [])
|
||||
|
||||
|
||||
def get_fields_metadata(model: TypeInfo) -> Dict[str, Any]:
|
||||
return get_django_metadata(model).setdefault('fields', {})
|
||||
|
||||
|
||||
def get_lookups_metadata(model: TypeInfo) -> Dict[str, Any]:
|
||||
return get_django_metadata(model).setdefault('lookups', {})
|
||||
|
||||
|
||||
def get_related_managers_metadata(model: TypeInfo) -> Dict[str, Any]:
|
||||
return get_django_metadata(model).setdefault('related_managers', {})
|
||||
@@ -1,21 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sample_django_project.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -1,5 +0,0 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MyappConfig(AppConfig):
|
||||
label = 'myapp22'
|
||||
@@ -1,6 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
# Create your models here.
|
||||
class MyModel(models.Model):
|
||||
pass
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
@@ -1,121 +0,0 @@
|
||||
"""
|
||||
Django settings for sample_django_project project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 2.2.3.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/2.2/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/2.2/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'e6gj!2x(*odqwmjafrn7#35%)&rnn&^*0x-f&j0prgr--&xf+%'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'mypy_django_plugin.lib.tests.sample_django_project.myapp'
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'sample_django_project.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'sample_django_project.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/2.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
@@ -1,21 +0,0 @@
|
||||
"""sample_django_project URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/2.2/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
@@ -1,16 +0,0 @@
|
||||
"""
|
||||
WSGI config for sample_django_project project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sample_django_project.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
@@ -1,14 +0,0 @@
|
||||
from mypy.options import Options
|
||||
|
||||
from mypy_django_plugin.lib.config import extract_app_model_aliases
|
||||
from mypy_django_plugin.main import DjangoPlugin
|
||||
|
||||
|
||||
def test_parse_django_settings():
|
||||
app_model_mapping = extract_app_model_aliases('mypy_django_plugin.lib.tests.sample_django_project.root.settings')
|
||||
assert app_model_mapping['myapp.MyModel'] == 'mypy_django_plugin.lib.tests.sample_django_project.myapp.models.MyModel'
|
||||
|
||||
|
||||
def test_instantiate_plugin_with_config():
|
||||
plugin = DjangoPlugin(Options())
|
||||
|
||||
Reference in New Issue
Block a user