Beispiel #1
0
 def _get_current_model_bases(self) -> Dict[str, int]:
     model_sym = self.lookup_fully_qualified(fullnames.MODEL_CLASS_FULLNAME)
     if model_sym is not None and isinstance(model_sym.node, TypeInfo):
         return helpers.get_django_metadata(model_sym.node).setdefault(
             'model_bases', {fullnames.MODEL_CLASS_FULLNAME: 1})
     else:
         return {}
Beispiel #2
0
def transform_form_class(ctx: ClassDefContext) -> None:
    sym = ctx.api.lookup_fully_qualified_or_none(
        fullnames.BASEFORM_CLASS_FULLNAME)
    if sym is not None and isinstance(sym.node, TypeInfo):
        helpers.get_django_metadata(
            sym.node)['baseform_bases'][ctx.cls.fullname] = 1

    forms.make_meta_nested_class_inherit_from_any(ctx)
Beispiel #3
0
    def get_reverse_manager_info(self, model_info: TypeInfo, derived_from: str) -> Optional[TypeInfo]:
        manager_fullname = helpers.get_django_metadata(model_info).get("reverse_managers", {}).get(derived_from)
        if not manager_fullname:
            return None

        symbol = self.api.lookup_fully_qualified_or_none(manager_fullname)
        if symbol is None or not isinstance(symbol.node, TypeInfo):
            return None
        return symbol.node
Beispiel #4
0
    def run(self) -> None:
        basemodel_info = self.lookup_typeinfo_or_incomplete_defn_error(
            fullnames.MODEL_CLASS_FULLNAME)
        basemodel_metadata = helpers.get_django_metadata(basemodel_info)
        if 'model_mixins' not in basemodel_metadata:
            basemodel_metadata['model_mixins'] = {}

        for base_info in self.model_classdef.info.mro[1:]:
            if base_info.fullname() != 'builtins.object':
                basemodel_metadata['model_mixins'][base_info.fullname()] = 1
Beispiel #5
0
def transform_model_class(ctx: ClassDefContext, django_context: DjangoContext) -> None:
    sym = ctx.api.lookup_fully_qualified_or_none(fullnames.MODEL_CLASS_FULLNAME)

    if sym is not None and isinstance(sym.node, TypeInfo):
        helpers.get_django_metadata(sym.node)["model_bases"][ctx.cls.fullname] = 1
    else:
        if not ctx.api.final_iteration:
            ctx.api.defer()
            return

    process_model_class(ctx, django_context)
Beispiel #6
0
    def get_attribute_hook(
            self,
            fullname: str) -> Optional[Callable[[AttributeContext], MypyType]]:
        class_name, _, attr_name = fullname.rpartition(".")

        # Lookup of a settings variable
        if class_name == fullnames.DUMMY_SETTINGS_BASE_CLASS:
            return partial(settings.get_type_of_settings_attribute,
                           django_context=self.django_context)

        info = self._get_typeinfo_or_none(class_name)

        # Lookup of the '.is_superuser' attribute
        if info and info.has_base(fullnames.PERMISSION_MIXIN_CLASS_FULLNAME
                                  ) and attr_name == "is_superuser":
            return partial(set_auth_user_model_boolean_fields,
                           django_context=self.django_context)

        # Lookup of the 'request.user' attribute
        if info and info.has_base(
                fullnames.HTTPREQUEST_CLASS_FULLNAME) and attr_name == "user":
            return partial(
                request.set_auth_user_model_as_type_for_request_user,
                django_context=self.django_context)

        # Lookup of the 'user.is_staff' or 'user.is_active' attribute
        if info and info.has_base(
                fullnames.ABSTRACT_USER_MODEL_FULLNAME) and attr_name in (
                    "is_staff", "is_active"):
            return partial(set_auth_user_model_boolean_fields,
                           django_context=self.django_context)

        # Lookup of a method on a dynamically generated manager class
        # i.e. a manager class only existing while mypy is running, not collected from the AST
        if (info and info.has_base(fullnames.BASE_MANAGER_CLASS_FULLNAME) and
                "from_queryset_manager" in helpers.get_django_metadata(info)):
            return resolve_manager_method

        return None
Beispiel #7
0
def add_new_manager_base(ctx: ClassDefContext) -> None:
    sym = ctx.api.lookup_fully_qualified_or_none(
        fullnames.MANAGER_CLASS_FULLNAME)
    if sym is not None and isinstance(sym.node, TypeInfo):
        helpers.get_django_metadata(
            sym.node)['manager_bases'][ctx.cls.fullname] = 1
Beispiel #8
0
 def set_reverse_manager_info(self, model_info: TypeInfo, derived_from: str,
                              fullname: str) -> None:
     helpers.get_django_metadata(model_info).setdefault(
         "reverse_managers", {})[derived_from] = fullname