def test_unpack_infer_empty_tuple(self): node = test_utils.extract_node(''' () ''') inferred = next(node.infer()) with self.assertRaises(InferenceError) as cm: unpacked = list(node_classes.unpack_infer(inferred))
def excepthandler_assigned_stmts(self, node=None, context=None, assign_path=None): for assigned in node_classes.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): assigned = objects.ExceptionInstance(assigned) yield assigned return dict(node=self, unknown=node, assign_path=assign_path, context=context)
def test_bad_import_inference(self): # Explication of bug '''When we import PickleError from nonexistent, a call to the infer method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name will catch this exception and yield and YES instead. ''' code = '''try: from pickle import PickleError except ImportError: from nonexistent import PickleError try: pass except PickleError: pass ''' astroid = abuilder.string_build(code) from_node = astroid.body[1].handlers[0].body[0] handler_type = astroid.body[1].handlers[0].type excs = list(unpack_infer(handler_type))
def test_unpack_infer_empty_tuple(self): node = builder.extract_node(''' () ''') inferred = next(node.infer()) with self.assertRaises(InferenceError) as cm: unpacked = list(node_classes.unpack_infer(inferred))
def test_bad_import_inference(self): # Explication of bug """When we import PickleError from nonexistent, a call to the infer method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name will catch this exception and yield and Uninferable instead. """ code = """ try: from pickle import PickleError except ImportError: from nonexistent import PickleError try: pass except PickleError: pass """ module = builder.parse(code) handler_type = module.body[1].handlers[0].type excs = list(node_classes.unpack_infer(handler_type)) # The number of returned object can differ on Python 2 # and Python 3. In one version, an additional item will # be returned, from the _pickle module, which is not # present in the other version. self.assertIsInstance(excs[0], nodes.ClassDef) self.assertEqual(excs[0].name, "PickleError") self.assertIs(excs[-1], util.Uninferable)
def test_bad_import_inference(self): # Explication of bug '''When we import PickleError from nonexistent, a call to the infer method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name will catch this exception and yield and Uninferable instead. ''' code = ''' try: from pickle import PickleError except ImportError: from nonexistent import PickleError try: pass except PickleError: pass ''' module = builder.parse(code) handler_type = module.body[1].handlers[0].type excs = list(node_classes.unpack_infer(handler_type)) # The number of returned object can differ on Python 2 # and Python 3. In one version, an additional item will # be returned, from the _pickle module, which is not # present in the other version. self.assertIsInstance(excs[0], nodes.ClassDef) self.assertEqual(excs[0].name, 'PickleError') self.assertIs(excs[-1], util.Uninferable)
def test_bad_import_inference(self): # Explication of bug '''When we import PickleError from nonexistent, a call to the infer method of this From node will be made by unpack_infer. inference.infer_from will try to import this module, which will fail and raise a InferenceException (by mixins.do_import_module). The infer_name will catch this exception and yield and YES instead. ''' code = ''' try: from pickle import PickleError except ImportError: from nonexistent import PickleError try: pass except PickleError: pass ''' astroid = test_utils.build_module(code) handler_type = astroid.body[1].handlers[0].type excs = list(unpack_infer(handler_type)) # The number of returned object can differ on Python 2 # and Python 3. In one version, an additional item will # be returned, from the _pickle module, which is not # present in the other version. self.assertIsInstance(excs[0], nodes.Class) self.assertEqual(excs[0].name, 'PickleError') self.assertIs(excs[-1], bases.YES)
def test_unpack_infer_empty_tuple(self): node = builder.extract_node(""" () """) inferred = next(node.infer()) with self.assertRaises(InferenceError): list(node_classes.unpack_infer(inferred))
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None): for assigned in node_classes.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): assigned = objects.ExceptionInstance(assigned) yield assigned # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. return dict(node=self, unknown=node, assign_path=asspath, context=context)
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None): for assigned in node_classes.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): assigned = bases.Instance(assigned) yield assigned # Explicit StopIteration to return error information, see comment # in raise_if_nothing_inferred. raise StopIteration(dict(node=self, unknown=node, assign_path=asspath, context=context))
def test_unpack_infer_uninferable_nodes(self): node = test_utils.extract_node(''' x = [A] * 1 f = [x, [A] * 2] f ''') inferred = next(node.infer()) unpacked = list(node_classes.unpack_infer(inferred)) self.assertEqual(len(unpacked), 3) self.assertTrue(all(elt is astroid_util.YES for elt in unpacked))
def test_unpack_infer_uninferable_nodes(self): node = builder.extract_node(''' x = [A] * 1 f = [x, [A] * 2] f ''') inferred = next(node.infer()) unpacked = list(node_classes.unpack_infer(inferred)) self.assertEqual(len(unpacked), 3) self.assertTrue(all(elt is astroid_util.Uninferable for elt in unpacked))
def test_unpack_infer_uninferable_nodes(self): node = builder.extract_node(""" x = [A] * 1 f = [x, [A] * 2] f """) inferred = next(node.infer()) unpacked = list(node_classes.unpack_infer(inferred)) self.assertEqual(len(unpacked), 3) self.assertTrue( all(elt is astroid_util.Uninferable for elt in unpacked))
def interfaces(node, herited=True, handler_func=_iface_hdlr): """Return an iterator on interfaces implemented by the given class node.""" try: implements = bases.Instance(node).getattr("__implements__")[0] except exceptions.NotFoundError: return if not herited and implements.frame() is not node: return found = set() missing = False for iface in node_classes.unpack_infer(implements): if iface is astroid.Uninferable: missing = True continue if iface not in found and handler_func(iface): found.add(iface) yield iface if missing: raise exceptions.InferenceError()
def interfaces(node, herited=True, handler_func=_iface_hdlr): """Return an iterator on interfaces implemented by the given class node.""" # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)... try: implements = bases.Instance(node).getattr('__implements__')[0] except exceptions.NotFoundError: return if not herited and implements.frame() is not node: return found = set() missing = False for iface in node_classes.unpack_infer(implements): if iface is astroid.YES: missing = True continue if iface not in found and handler_func(iface): found.add(iface) yield iface if missing: raise exceptions.InferenceError()
def interfaces(self, herited=True, handler_func=_iface_hdlr): """return an iterator on interfaces implemented by the given class node """ # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)... try: implements = Instance(self).getattr('__implements__')[0] except NotFoundError: return if not herited and not implements.frame() is self: return found = set() missing = False for iface in unpack_infer(implements): if iface is YES: missing = True continue if not iface in found and handler_func(iface): found.add(iface) yield iface if missing: raise InferenceError()
def interfaces(self, herited=True, handler_func=_iface_hdlr): """return an iterator on interfaces implemented by the given class node """ # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)... try: implements = Instance(self).getattr("__implements__")[0] except NotFoundError: return if not herited and not implements.frame() is self: return found = set() missing = False for iface in unpack_infer(implements): if iface is YES: missing = True continue if not iface in found and handler_func(iface): found.add(iface) yield iface if missing: raise InferenceError()
def excepthandler_assigned_stmts(self, node, context=None, asspath=None): for assigned in unpack_infer(self.type): if isinstance(assigned, nodes.Class): assigned = Instance(assigned) yield assigned
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None): for assigned in node_classes.unpack_infer(self.type): if isinstance(assigned, nodes.ClassDef): assigned = bases.Instance(assigned) yield assigned