def test_class_variable_attribute_in_class_body2(self): code = 'a_var = 10\nclass C(object):\n a_var \\\n= a_var\n' scope = libutils.get_string_scope(self.project, code) name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) a_var_pyname = scope['C'].get_object()['a_var'] result = name_finder.get_pyname_at(len(code) - 12) self.assertEquals(a_var_pyname, result)
def test_function_but_not_indexed(self): code = 'def a_func(a_func):\n pass\n' scope = libutils.get_string_scope(self.project, code) a_func_pyname = scope['a_func'] name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) result = name_finder.get_pyname_at(code.index('a_func') + 3) self.assertEquals(a_func_pyname, result)
def test_get_lineno(self): scope = libutils.get_string_scope( self.project, '\ndef sample_func():\n a = 10\n') self.assertEqual(1, len(scope.get_scopes())) sample_func_scope = scope.get_scopes()[0] self.assertEqual(1, scope.get_start()) self.assertEqual(2, sample_func_scope.get_start())
def test_inner_class_attribute_in_class_body(self): code = 'class C(object):\n class CC(object):\n pass\n' scope = libutils.get_string_scope(self.project, code) name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) a_class_pyname = scope['C'].get_object()['CC'] result = name_finder.get_pyname_at(code.index('CC') + 2) self.assertEquals(a_class_pyname, result)
def test_get_inner_scope_for_dict_comprehension(self): code = "a = {i:i for i in range(10)}\n" scope = libutils.get_string_scope(self.project, code) self.assertEqual(len(scope.get_scopes()), 1) self.assertNotIn("i", scope) self.assertIn("i", scope.get_scopes()[0])
def test_simple_type_inferencing_for_in_class_assignments(self): code = "class Sample(object):\n pass\n" "class Another(object):\n an_attr = Sample()\n" scope = libutils.get_string_scope(self.project, code) sample_class = scope["Sample"].get_object() another_class = scope["Another"].get_object() an_attr = another_class["an_attr"].get_object() self.assertEquals(sample_class, an_attr.get_type())
def test_class_method_in_class_body_but_not_indexed(self): code = 'class C(object):\n def func(self, func):\n pass\n' scope = libutils.get_string_scope(self.project, code) a_func_pyname = scope.get_scopes()[0].get_scopes()[0]['func'] name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) result = name_finder.get_pyname_at(code.index(', func') + 3) self.assertEquals(a_func_pyname, result)
def test_getting_overwritten_scopes(self): scope = libutils.get_string_scope( self.project, 'def f():\n pass\ndef f():\n pass\n') self.assertEquals(2, len(scope.get_scopes())) f1_scope = scope.get_scopes()[0] f2_scope = scope.get_scopes()[1] self.assertNotEquals(f1_scope, f2_scope)
def test_get_inner_scope_for_generator(self): code = "a = (i for i in range(10))\n" scope = libutils.get_string_scope(self.project, code) self.assertEqual(len(scope.get_scopes()), 1) self.assertNotIn("i", scope) self.assertIn("i", scope.get_scopes()[0])
def test_getting_overwritten_scopes(self): scope = libutils.get_string_scope( self.project, 'def f():\n pass\ndef f():\n pass\n') self.assertEqual(2, len(scope.get_scopes())) f1_scope = scope.get_scopes()[0] f2_scope = scope.get_scopes()[1] self.assertNotEqual(f1_scope, f2_scope)
def test_simple_type_inferencing_for_chained_assignments(self): mod = 'class Sample(object):\n pass\n' \ 'copied_sample = Sample' mod_scope = libutils.get_string_scope(self.project, mod) sample_class = mod_scope['Sample'] copied_sample = mod_scope['copied_sample'] self.assertEqual(sample_class.get_object(), copied_sample.get_object())
def test_get_lineno(self): scope = libutils.get_string_scope( self.project, '\ndef sample_func():\n a = 10\n') self.assertEquals(1, len(scope.get_scopes())) sample_func_scope = scope.get_scopes()[0] self.assertEquals(1, scope.get_start()) self.assertEquals(2, sample_func_scope.get_start())
def test_simple_function_scope(self): scope = libutils.get_string_scope( self.project, 'def sample_func():\n a = 10\n') self.assertEquals(1, len(scope.get_scopes())) sample_func_scope = scope.get_scopes()[0] self.assertEquals(1, len(sample_func_scope.get_names())) self.assertEquals(0, len(sample_func_scope.get_scopes()))
def test_simple_function_scope(self): scope = libutils.get_string_scope(self.project, 'def sample_func():\n a = 10\n') self.assertEqual(1, len(scope.get_scopes())) sample_func_scope = scope.get_scopes()[0] self.assertEqual(1, len(sample_func_scope.get_names())) self.assertEqual(0, len(sample_func_scope.get_scopes()))
def test_scopes_in_function_scopes(self): scope = libutils.get_string_scope( self.project, 'def func():\n def inner():\n var = 10\n') func_scope = scope.get_scopes()[0] inner_scope = func_scope.get_scopes()[0] self.assertTrue('var' in inner_scope)
def test_dict_comprehension_scope(self): scope = libutils.get_string_scope( self.project, '{b_var: d_var for b_var, c_var in e_var}\n') self.assertEqual( list(sorted(scope.get_defined_names())), ['b_var', 'c_var'], )
def test_get_inner_scope_and_logical_lines(self): scope = libutils.get_string_scope( self.project, 'class C(object):\n' ' def f():\n s = """\n1\n2\n"""\n a = 1\n') c_scope = scope.get_scopes()[0] f_in_c = c_scope.get_scopes()[0] self.assertEqual(f_in_c, scope.get_inner_scope_for_line(7))
def test_getting_defined_names_for_modules(self): scope = libutils.get_string_scope(self.project, 'class A(object):\n pass\n') self.assertTrue('open' in scope.get_names()) self.assertTrue('A' in scope.get_names()) self.assertTrue('open' not in scope.get_defined_names()) self.assertTrue('A' in scope.get_defined_names())
def test_getting_defined_names_for_modules(self): scope = libutils.get_string_scope( self.project, 'class A(object):\n pass\n') self.assertTrue('open' in scope.get_names()) self.assertTrue('A' in scope.get_names()) self.assertTrue('open' not in scope.get_defined_names()) self.assertTrue('A' in scope.get_defined_names())
def test_get_scope_for_offset_for_comprehension(self): code = "a = [i for i in range(10)]\n" scope = libutils.get_string_scope(self.project, code) c_scope = scope.get_scopes()[0] self.assertEqual(c_scope, scope.get_inner_scope_for_offset(10)) self.assertEqual(scope, scope.get_inner_scope_for_offset(1))
def test_nested_modules(self): pkg = testutils.create_package(self.project, 'pkg') testutils.create_module(self.project, 'mod', pkg) imported_module = self.project.get_module('pkg.mod') scope = libutils.get_string_scope(self.project, 'import pkg.mod\n') mod_pyobject = scope['pkg'].get_object()['mod'] self.assertEquals((imported_module, 1), mod_pyobject.get_definition_location())
def test_none_assignments_in_classes(self): code = 'class C(object):\n' \ ' var = ""\n' \ ' def f(self):\n' \ ' self.var += "".join([])\n' scope = libutils.get_string_scope(self.project, code) c_class = scope['C'].get_object() self.assertTrue('var' in c_class)
def test_function_returned_object_static_type_inference2(self): src = 'class Sample(object):\n pass\n' \ 'def a_func():\n return Sample()\n' \ 'a_var = a_func()\n' scope = libutils.get_string_scope(self.project, src) sample_class = scope['Sample'].get_object() a_var = scope['a_var'].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_simple_type_inferencing_for_chained_assignments(self): mod = 'class Sample(object):\n pass\n' \ 'copied_sample = Sample' mod_scope = libutils.get_string_scope(self.project, mod) sample_class = mod_scope['Sample'] copied_sample = mod_scope['copied_sample'] self.assertEquals(sample_class.get_object(), copied_sample.get_object())
def test_relative_imports_for_string_scopes(self): pkg = testutils.create_package(self.project, 'pkg') mod1 = testutils.create_module(self.project, 'mod1', pkg) mod2 = testutils.create_module(self.project, 'mod2', pkg) mod2.write('import mod1\n') mod1_object = self.pycore.resource_to_pyobject(mod1) mod2_scope = libutils.get_string_scope(self.project, mod2.read(), mod2) self.assertEquals(mod1_object, mod2_scope['mod1'].get_object())
def test_for_variables_in_scopes(self): code = dedent("""\ for a_var in range(10): pass """) scope = libutils.get_string_scope(self.project, code) self.assertTrue("a_var" in scope)
def test_simple_type_inferencing_classes_defined_in_holding_scope(self): code = 'class Sample(object):\n pass\n' \ 'def a_func():\n a_var = Sample()\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() a_var = scope['a_func'].get_object().\ get_scope()['a_var'].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_dict_comprehension_scope(self): code = "{b_var: d_var for b_var, c_var in e_var}\n" scope = libutils.get_string_scope(self.project, code) self.assertEqual( list(sorted(scope.get_scopes()[0].get_defined_names())), ["b_var", "c_var"], )
def test_get_inner_scope_and_logical_lines(self): scope = libutils.get_string_scope( self.project, 'class C(object):\n' ' def f():\n s = """\n1\n2\n"""\n a = 1\n') c_scope = scope.get_scopes()[0] f_in_c = c_scope.get_scopes()[0] self.assertEquals(f_in_c, scope.get_inner_scope_for_line(7))
def test_attributed_object_inference(self): src = 'class Sample(object):\n' \ ' def __init__(self):\n self.a_var = None\n' \ ' def set(self):\n self.a_var = Sample()\n' scope = libutils.get_string_scope(self.project, src) sample_class = scope['Sample'].get_object() a_var = sample_class['a_var'].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_func_returned_obj_using_call_spec_func_static_type_infer(self): src = 'class Sample(object):\n' \ ' def __call__(self):\n return Sample\n' \ 'sample = Sample()\na_var = sample()' scope = libutils.get_string_scope(self.project, src) sample_class = scope['Sample'] a_var = scope['a_var'] self.assertEquals(sample_class.get_object(), a_var.get_object())
def test_get_scope_for_offset_for_in_nested_comprehension(self): code = "[i for i in [j for j in k]]\n" scope = libutils.get_string_scope(self.project, code) c_scope = scope.get_scopes()[0] self.assertEqual(c_scope, scope.get_inner_scope_for_offset(5)) inner_scope = c_scope.get_scopes()[0] self.assertEqual(inner_scope, scope.get_inner_scope_for_offset(15))
def test_inline_assignment(self): code = """values = (a_var := 2,)""" scope = libutils.get_string_scope(self.project, code) self.assertEqual( list(sorted(scope.get_defined_names())), ["a_var", "values"], )
def test_get_inner_scope_for_staticmethods(self): scope = libutils.get_string_scope( self.project, 'class C(object):\n' ' @staticmethod\n' ' def a_func(self):\n pass\n') c_scope = scope.get_scopes()[0] f_in_c = c_scope.get_scopes()[0] self.assertEqual(f_in_c, scope.get_inner_scope_for_line(4))
def test_assists_inside_fors(self): code = dedent("""\ for i in range(10): a_var = i """) scope = libutils.get_string_scope(self.project, code) self.assertTrue("a_var" in scope)
def test_list_comprehension_scope_inside_assignment(self): scope = libutils.get_string_scope( self.project, 'a_var = [b_var + d_var for b_var, c_var in e_var]\n') self.assertEqual( list(sorted(scope.get_defined_names())), ['a_var', 'b_var', 'c_var'], )
def test_simple_type_inferencing_for_in_class_assignments(self): code = 'class Sample(object):\n pass\n' \ 'class Another(object):\n an_attr = Sample()\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() another_class = scope['Another'].get_object() an_attr = another_class['an_attr'].get_object() self.assertEquals(sample_class, an_attr.get_type())
def test_get_inner_scope_for_staticmethods(self): scope = libutils.get_string_scope( self.project, 'class C(object):\n' ' @staticmethod\n' ' def a_func(self):\n pass\n') c_scope = scope.get_scopes()[0] f_in_c = c_scope.get_scopes()[0] self.assertEquals(f_in_c, scope.get_inner_scope_for_line(4))
def test_location_of_imports_when_importing(self): mod = testutils.create_module(self.project, 'mod') mod.write('from samplemod import SampleClass\n') scope = libutils.get_string_scope( self.project, 'from mod import SampleClass\n') sample_class = scope['SampleClass'] samplemod = self.project.get_module('samplemod') self.assertEquals((samplemod, 1), sample_class.get_definition_location())
def test_scope_lookup(self): scope = libutils.get_string_scope( self.project, 'var1 = 10\ndef sample_func(param):\n var2 = 20\n') self.assertTrue(scope.lookup('var2') is None) self.assertEquals(get_base_type('Function'), scope.lookup('sample_func').get_object().get_type()) sample_func_scope = scope.get_scopes()[0] self.assertTrue(sample_func_scope.lookup('var1') is not None)
def test_classes_inside_function_scopes(self): scope = libutils.get_string_scope( self.project, 'def sample_func():\n' ' class SampleClass(object):\n pass\n') self.assertEqual(1, len(scope.get_scopes())) sample_func_scope = scope.get_scopes()[0] # noqa self.assertEqual( get_base_type('Type'), scope.get_scopes()[0]['SampleClass'].get_object().get_type())
def test_simple_type_inferencing_class_attributes(self): code = 'class Sample(object):\n pass\n' \ 'class Another(object):\n' \ ' def __init__(self):\n self.a_var = Sample()\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() another_class = scope['Another'].get_object() a_var = another_class['a_var'].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_following_chained_assignments_avoiding_circles(self): mod = 'class Sample(object):\n pass\n' \ 'sample_class = Sample\n' \ 'sample_class = sample_class\n' mod_scope = libutils.get_string_scope(self.project, mod) sample_class = mod_scope['Sample'] sample_class_var = mod_scope['sample_class'] self.assertEquals(sample_class.get_object(), sample_class_var.get_object())
def test_attributed_object_inference(self): src = ( "class Sample(object):\n" " def __init__(self):\n self.a_var = None\n" " def set(self):\n self.a_var = Sample()\n" ) scope = libutils.get_string_scope(self.project, src) sample_class = scope["Sample"].get_object() a_var = sample_class["a_var"].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_simple_type_inferencing_classes_in_class_methods(self): code = 'class Sample(object):\n pass\n' \ 'class Another(object):\n' \ ' def a_method():\n a_var = Sample()\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() another_class = scope['Another'].get_object() a_var = another_class['a_method'].\ get_object().get_scope()['a_var'].get_object() self.assertEquals(sample_class, a_var.get_type())
def test_self_in_methods(self): code = 'class Sample(object):\n' \ ' def func(self):\n' \ ' pass\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() func_scope = scope.get_scopes()[0].get_scopes()[0] self.assertEquals(sample_class, func_scope['self'].get_object().get_type()) self.assertTrue('func' in func_scope['self'].get_object())
def test_modules_after_from_statements(self): root_folder = self.project.root mod = testutils.create_module(self.project, 'mod', root_folder) mod.write('def a_func():\n pass\n') code = 'from mod import a_func\n' scope = libutils.get_string_scope(self.project, code) name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) mod_pyobject = self.project.get_pymodule(mod) found_pyname = name_finder.get_pyname_at(code.index('mod') + 1) self.assertEquals(mod_pyobject, found_pyname.get_object())
def test_renaming_functions_with_from_import_and_parens(self): mod1 = testutils.create_module(self.project, 'mod1') mod1.write('def afunc():\n pass\n') code = 'from mod1 import (\n afunc as func)\n' scope = libutils.get_string_scope(self.project, code) name_finder = rope.base.evaluate.ScopeNameFinder(scope.pyobject) mod_pyobject = self.project.get_pymodule(mod1) afunc = mod_pyobject['afunc'] found_pyname = name_finder.get_pyname_at(code.index('afunc') + 1) self.assertEquals(afunc.get_object(), found_pyname.get_object())
def test_self_in_methods_with_decorators(self): code = 'class Sample(object):\n' \ ' @staticmethod\n' \ ' def func(self):\n' \ ' pass\n' scope = libutils.get_string_scope(self.project, code) sample_class = scope['Sample'].get_object() func_scope = scope.get_scopes()[0].get_scopes()[0] self.assertNotEquals(sample_class, func_scope['self'].get_object().get_type())