def test_pylint_config_attr(self): try: from pylint import lint # pylint: disable=unused-variable except ImportError: self.skipTest('pylint not available') mod = MANAGER.ast_from_module_name('pylint.lint') pylinter = mod['PyLinter'] expect = ['OptionsManagerMixIn', 'object', 'MessagesHandlerMixIn', 'ReportsHandlerMixIn', 'BaseTokenChecker', 'BaseChecker', 'OptionsProviderMixIn'] self.assertListEqual([c.name for c in pylinter.ancestors()], expect) self.assertTrue(list(Instance(pylinter).getattr('config'))) inferred = list(Instance(pylinter).igetattr('config')) self.assertEqual(len(inferred), 1) self.assertEqual(inferred[0].root().name, 'optparse') self.assertEqual(inferred[0].name, 'Values')
def transform_model_class(cls): if cls.is_subtype_of('django.db.models.base.Model'): core_exceptions = MANAGER.ast_from_module_name( 'django.core.exceptions') # add DoesNotExist exception DoesNotExist = Class('DoesNotExist', None) DoesNotExist.bases = core_exceptions.lookup('ObjectDoesNotExist')[1] cls.locals['DoesNotExist'] = [DoesNotExist] # add MultipleObjectsReturned exception MultipleObjectsReturned = Class('MultipleObjectsReturned', None) MultipleObjectsReturned.bases = core_exceptions.lookup( 'MultipleObjectsReturned')[1] cls.locals['MultipleObjectsReturned'] = [MultipleObjectsReturned] # add objects manager if 'objects' not in cls.locals: try: Manager = MANAGER.ast_from_module_name( 'django.db.models.manager').lookup('Manager')[1][0] QuerySet = MANAGER.ast_from_module_name( 'django.db.models.query').lookup('QuerySet')[1][0] except IndexError: pass else: if isinstance(Manager.body[0], Pass): # for django >= 1.7 for func_name, func_list in QuerySet.locals.items(): if (not func_name.startswith('_') and func_name not in Manager.locals): func = func_list[0] if (isinstance(func, Function) and 'queryset_only' not in func.instance_attrs): f = Function(func_name, None) f.args = Arguments() Manager.locals[func_name] = [f] cls.locals['objects'] = [Instance(Manager)] # add id field if 'id' not in cls.locals: try: AutoField = MANAGER.ast_from_module_name( 'django.db.models.fields').lookup('AutoField')[1][0] except IndexError: pass else: cls.locals['id'] = [Instance(AutoField)]
def _check_interfaces(self, node): """check that the given class node really implements declared interfaces """ e0221_hack = [False] def iface_handler(obj): """filter interface objects, it should be classes""" if not isinstance(obj, astroid.Class): e0221_hack[0] = True self.add_message('interface-is-not-class', node=node, args=(obj.as_string(), )) return False return True ignore_iface_methods = self.config.ignore_iface_methods try: for iface in node.interfaces(handler_func=iface_handler): for imethod in iface.methods(): name = imethod.name if name.startswith('_') or name in ignore_iface_methods: # don't check method beginning with an underscore, # usually belonging to the interface implementation continue # get class method astroid try: method = node_method(node, name) except astroid.NotFoundError: self.add_message('missing-interface-method', args=(name, iface.name), node=node) continue # ignore inherited methods if method.parent.frame() is not node: continue # check signature self._check_signature(method, imethod, '%s interface' % iface.name) except astroid.InferenceError: if e0221_hack[0]: return implements = Instance(node).getattr('__implements__')[0] assignment = implements.parent assert isinstance(assignment, astroid.Assign) # assignment.expr can be a Name or a Tuple or whatever. # Use as_string() for the message # FIXME: in case of multiple interfaces, find which one could not # be resolved self.add_message('unresolved-interface', node=implements, args=(node.name, assignment.value.as_string()))
def infer_call_result(self, caller, context=None): yield Instance(DjangoModel)