def extract_ref_to_fullname(rvalue_expr: CallExpr, module_file: MypyFile, all_modules: Dict[str, MypyFile]) -> Optional[str]: if 'to' in rvalue_expr.arg_names: to_expr = rvalue_expr.args[rvalue_expr.arg_names.index('to')] else: to_expr = rvalue_expr.args[0] if isinstance(to_expr, NameExpr): return module_file.names[to_expr.name].fullname elif isinstance(to_expr, StrExpr): typ_fullname = helpers.get_model_fullname_from_string(to_expr.value, all_modules) if typ_fullname is None: return None return typ_fullname return None
def extract_referred_to_type(ctx: FunctionContext) -> Optional[Instance]: api = cast(TypeChecker, ctx.api) if 'to' not in ctx.callee_arg_names: api.msg.fail( f'to= parameter must be set for {ctx.context.callee.fullname}', context=ctx.context) return None arg_type = ctx.arg_types[ctx.callee_arg_names.index('to')][0] if not isinstance(arg_type, CallableType): to_arg_expr = ctx.args[ctx.callee_arg_names.index('to')][0] if not isinstance(to_arg_expr, StrExpr): # not string, not supported return None try: model_fullname = helpers.get_model_fullname_from_string( to_arg_expr.value, all_modules=api.modules) except helpers.SelfReference: model_fullname = api.tscope.classes[-1].fullname() except helpers.SameFileModel as exc: model_fullname = api.tscope.classes[ -1].module_name + '.' + exc.model_cls_name if model_fullname is None: return None model_info = helpers.lookup_fully_qualified_generic( model_fullname, all_modules=api.modules) if model_info is None or not isinstance(model_info, TypeInfo): return None return Instance(model_info, []) referred_to_type = arg_type.ret_type if not isinstance(referred_to_type, Instance): return None if not referred_to_type.type.has_base(helpers.MODEL_CLASS_FULLNAME): ctx.api.msg.fail( f'to= parameter value must be ' f'a subclass of {helpers.MODEL_CLASS_FULLNAME}', context=ctx.context) return None return referred_to_type