diff options
Diffstat (limited to 'astroid/tests/unittest_inference.py')
-rw-r--r-- | astroid/tests/unittest_inference.py | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 2d6ab502..beb85e47 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -25,6 +25,7 @@ import unittest import six from astroid import InferenceError, builder, nodes +from astroid.builder import parse from astroid.inference import infer_end as inference_infer_end from astroid.bases import YES, Instance, BoundMethod, UnboundMethod,\ path_wrapper, BUILTINS @@ -123,10 +124,10 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): a, b= b, a # Gasp ! ''' - ast = test_utils.build_module(CODE, __name__) + ast = parse(CODE, __name__) def test_infer_abstract_property_return_values(self): - module = test_utils.build_module(''' + module = parse(''' import abc class A(object): @@ -362,7 +363,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): a = f() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) a = ast['a'] a_inferred = a.inferred() self.assertEqual(a_inferred[0].value, 1) @@ -392,7 +393,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ex2 = ex raise ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) ex1 = ast['ex1'] ex1_infer = ex1.infer() ex1 = next(ex1_infer) @@ -422,7 +423,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): a = 2 d = a ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) n = ast['b'] n_infer = n.infer() inferred = next(n_infer) @@ -447,7 +448,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): s = '' s2 = '_' ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) n = ast['l'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.List) @@ -481,7 +482,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.getitem(0).value, '_') code = 's = {1}' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) n = ast['s'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Set) @@ -492,7 +493,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_unicode_type(self): code = '''u = u""''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) n = ast['u'] inferred = next(n.infer()) self.assertIsInstance(inferred, nodes.Const) @@ -507,7 +508,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): statm = staticmethod(open) clsm = classmethod('whatever') ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) statm = next(ast['A'].igetattr('statm')) self.assertTrue(statm.callable()) clsm = next(ast['A'].igetattr('clsm')) @@ -518,7 +519,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): class Warning(Warning): pass ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) w = ast['Warning'] ancestors = w.ancestors() ancestor = next(ancestors) @@ -540,7 +541,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): from astroid.modutils import load_module_from_name xxx = load_module_from_name('__pkginfo__') ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) xxx = ast['xxx'] self.assertSetEqual({n.__class__ for n in xxx.inferred()}, {nodes.Const, YES.__class__}) @@ -556,7 +557,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): kwargs['e_type'] = e_type.capitalize().encode() print(args) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) arg = test_utils.get_name_node(ast['ErudiEntitySchema']['__init__'], 'e_type') self.assertEqual([n.__class__ for n in arg.infer()], [YES.__class__]) @@ -621,7 +622,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): if ok: fct(a_line) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) self.assertEqual(len(list(ast['process_line'].infer_call_result(None))), 3) self.assertEqual(len(list(ast['tupletest'].infer())), 3) values = ['FunctionDef(first_word)', 'FunctionDef(last_word)', 'Const(NoneType)'] @@ -657,7 +658,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): something = 1.0j return something.conjugate() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) values = [i.value for i in test_utils.get_name_node(ast, 'something', -1).infer()] self.assertEqual(values, [1.0, 1.0j]) @@ -715,7 +716,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 97) def test_simple_tuple(self): - module = test_utils.build_module(""" + module = parse(""" a = (1,) b = (22,) some = a + b #@ @@ -736,7 +737,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): print ([(d,e) for e,d in ([1,2], [3,4])]) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) self.assertEqual([i.value for i in test_utils.get_name_node(ast, 'a', -1).infer()], [1, 2, 3]) self.assertEqual([i.value for i in @@ -752,7 +753,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): code = ''' print ((d,e) for e,d in ([1,2], [3,4])) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) self.assertEqual([i.value for i in test_utils.get_name_node(ast, 'd', -1).infer()], [2, 4]) self.assertEqual([i.value for i in @@ -795,7 +796,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): un = mirror(1) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast.igetattr('un')) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Const) @@ -807,7 +808,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): un = mirror(1) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast.igetattr('mirror')) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Lambda) @@ -829,7 +830,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): sub = Sub.instance() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast.igetattr('sub')) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], Instance) @@ -844,7 +845,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): from os.path import exists as e assert e(__file__) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast.igetattr('osp')) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], nodes.Module) @@ -1020,7 +1021,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): x = randint(1) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast.igetattr('x')) self.assertEqual(len(inferred), 2) value = [str(v) for v in inferred] @@ -1047,7 +1048,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def f(x): a = ()[x] ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(ast['f'].ilookup('a')) self.assertEqual(len(inferred), 1) self.assertEqual(inferred[0], YES) @@ -1066,7 +1067,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def __init__(self): self.attr = 41 """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) foo_class = ast['Foo'] bar_class = ast['Bar'] bar_self = ast['Bar']['__init__']['self'] @@ -1131,7 +1132,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): print(Browser) b = Browser() ''' - ast = test_utils.build_module(data, __name__) + ast = parse(data, __name__) browser = next(test_utils.get_name_node(ast, 'Browser').infer()) self.assertIsInstance(browser, nodes.ClassDef) bopen = list(browser.igetattr('open')) @@ -1163,7 +1164,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): my_me = SendMailController().me ''' decorators = set(['%s.property' % BUILTINS]) - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) self.assertEqual(ast['SendMailController']['smtp'].decoratornames(), decorators) propinferred = list(ast.body[2].value.infer()) @@ -1193,7 +1194,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): pactions = EnvBasedTC.pactions.im_func print (pactions) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) pactions = test_utils.get_name_node(ast, 'pactions') inferred = list(pactions.infer()) self.assertEqual(len(inferred), 1) @@ -1209,7 +1210,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): a += 2 print (a) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(test_utils.get_name_node(ast, 'a').infer()) self.assertEqual(len(inferred), 1) @@ -1226,7 +1227,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): spam = bar(None, qux) print (spam) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(test_utils.get_name_node(ast['foo'], 'spam').infer()) self.assertEqual(len(inferred), 1) self.assertIs(inferred[0], YES) @@ -1251,7 +1252,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): p = self.app print (p) ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = list(Instance(ast['DataManager']).igetattr('app')) self.assertEqual(len(inferred), 2, inferred) # None / Instance(Application) inferred = list(test_utils.get_name_node(ast['DataManager']['test'], 'p').infer()) @@ -1277,7 +1278,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): Z = test() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) inferred = next(ast['Z'].infer()) self.assertIsInstance(inferred, nodes.List) self.assertEqual(len(inferred.elts), 0) @@ -1293,7 +1294,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): n = NewTest() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) self.assertRaises(InferenceError, list, ast['NewTest'].igetattr('arg')) n = next(ast['n'].infer()) inferred = list(n.igetattr('arg')) @@ -1306,7 +1307,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): class Xxx(nonregr.Aaa, nonregr.Ccc): "doc" ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) parents = list(ast['Xxx'].ancestors()) self.assertEqual(len(parents), 3, parents) # Aaa, Ccc, object @@ -1316,7 +1317,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): A = namedtuple('A', ['a', 'b']) B = namedtuple('B', 'a b') ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) aclass = ast['A'].inferred()[0] self.assertIsInstance(aclass, nodes.ClassDef) self.assertIn('a', aclass.instance_attrs) @@ -1348,7 +1349,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): empty = A.empty() empty_list = A().empty_method() ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) int_node = ast['x'].inferred()[0] self.assertIsInstance(int_node, nodes.Const) self.assertEqual(int_node.value, 1) @@ -1369,7 +1370,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): vararg = args kwarg = kwargs ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) func = ast['test'] vararg = func.body[0].value kwarg = func.body[1].value @@ -1393,7 +1394,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): """ # Test that inferring Thread.__init__ looks up in # the nested scope. - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) callfunc = next(ast.nodes_of_class(nodes.Call)) func = callfunc.func inferred = func.inferred()[0] @@ -1409,7 +1410,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): sub = a - b mul = a * b """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] self.assertIs(sub, YES) @@ -1428,7 +1429,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): sub = a - b mul = a * b """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] self.assertIs(sub, YES) @@ -1448,7 +1449,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): sub = a - b mul = a * b """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) sub = ast['sub'].inferred()[0] mul = ast['mul'].inferred()[0] self.assertIs(sub, YES) @@ -1466,7 +1467,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): b = A() c = a * b """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) node = ast['c'] self.assertEqual(node.inferred(), [YES]) @@ -1488,7 +1489,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def do_a_thing(): pass """ - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) node = ast['do_a_thing'] self.assertEqual(node.type, 'function') @@ -1768,7 +1769,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): return "second" ''' - ast = test_utils.build_module(code, __name__) + ast = parse(code, __name__) bases = ast['Second'].bases[0] inferred = next(bases.infer()) self.assertTrue(inferred) @@ -1776,7 +1777,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.qname(), 'collections.Counter') def test_inferring_with_statement_failures(self): - module = test_utils.build_module(''' + module = parse(''' class NoEnter(object): pass class NoMethod(object): @@ -1797,7 +1798,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, module['no_elts'].infer()) def test_inferring_with_statement(self): - module = test_utils.build_module(''' + module = parse(''' class SelfContext(object): def __enter__(self): return self @@ -1848,7 +1849,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 2) def test_inferring_with_contextlib_contextmanager(self): - module = test_utils.build_module(''' + module = parse(''' import contextlib from contextlib import contextmanager @@ -1906,7 +1907,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): # indices. This is the case of contextlib.nested, where the # result is a list, which is mutated later on, so it's # undetected by astroid. - module = test_utils.build_module(''' + module = parse(''' class Manager(object): def __enter__(self): return [] @@ -1916,7 +1917,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, module['a'].infer()) def test_inferring_with_contextlib_contextmanager_failures(self): - module = test_utils.build_module(''' + module = parse(''' from contextlib import contextmanager def no_decorators_mgr(): @@ -2144,7 +2145,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_bool_value(self): # Verify the truth value of nodes. - module = test_utils.build_module(''' + module = parse(''' import collections collections_module = collections def function(): pass |