def test_worksForPackageInitFile(self): try: createPackageStructure("pass","pass") self.assertEqual(getModuleUsingFQN("a.b",[pkgstructureRootDir]).filename, os.path.join(pkgstructureChilddir,"__init__.py")) finally: removePackageStructure()
def test_worksForFullPath(self): try: createPackageStructure("pass","pass") self.assertEqual(pkgstructureFile2, getModuleUsingFQN("a.b.bah",[pkgstructureRootDir]).filename) finally: removePackageStructure()
def handleModuleClassOrFunctionScope(scope,fqn,pythonpath): assert isinstance(scope,Function) or isinstance(scope,Class) or isinstance(scope,Module), "this function cant take a scope of type"+str(scope.__class__) if fqn == "self" and scopeIsAMethod(scope): return Instance(scope.getParent()) child = scope.getChild(fqn) if child: return child type = scanScopeSourceForType(scope, fqn) if type is not None: return type type = getImportedType(scope, fqn, pythonpath) # try imported types if type is not None: return type if isinstance(scope,Module) and scope.getSourceNode().filename.endswith("__init__.py"): # try searching the package for a module packagedir = os.path.dirname(scope.getSourceNode().filename) node = getModuleUsingFQN(fqn,[packagedir]+pythonpath) if node is not None: return node try: parentScope = scope.getParent() except ElementHasNoParentException: return None # scope is a module while isinstance(parentScope,Class): # don't search class scope, since this is not accessible except # through self (is this true?) parentScope = parentScope.getParent() return getTypeOf(parentScope, fqn, pythonpath)
def test_worksForFullPath(self): try: createPackageStructure("pass", "pass") self.assertEqual( pkgstructureFile2, getModuleUsingFQN("a.b.bah", [pkgstructureRootDir]).filename) finally: removePackageStructure()
def test_worksForPackageInitFile(self): try: createPackageStructure("pass", "pass") self.assertEqual( getModuleUsingFQN("a.b", [pkgstructureRootDir]).filename, os.path.join(pkgstructureChilddir, "__init__.py")) finally: removePackageStructure()
def test_doesntGotIntoRecursiveLoopWhenEvaluatingARecursiveFunction(self): src = trimLines(""" def fn(v): if v < 45: return fn(root+1) val = fn(3) """) root = createSourceNodeAt(src,"a.foo") mod = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) getTypeOf(mod,"val",[pkgstructureRootDir]) # stack overflow!
def test_handlesRecursionProblem(self): src = trimLines(""" def fn(root): node = root node = node.getPackage('something') """) root = createSourceNodeAt(src,"a.foo") m = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) fn = getTypeOf(m,"fn",[pkgstructureRootDir]) getTypeOf(fn,"node",[pkgstructureRootDir]) # stack overflow!
def test_doesntGotIntoRecursiveLoopWhenEvaluatingARecursiveFunction(self): src = trimLines(""" def fn(v): if v < 45: return fn(root+1) val = fn(3) """) root = createSourceNodeAt(src, "a.foo") mod = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) getTypeOf(mod, "val", [pkgstructureRootDir]) # stack overflow!
def test_handlesRecursionProblem(self): src = trimLines(""" def fn(root): node = root node = node.getPackage('something') """) root = createSourceNodeAt(src, "a.foo") m = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) fn = getTypeOf(m, "fn", [pkgstructureRootDir]) getTypeOf(fn, "node", [pkgstructureRootDir]) # stack overflow!
def resolveImportedModuleOrPackage(scope,fqn,pythonpath): # try searching from directory containing scope module path = os.path.dirname(scope.module.filename) node = getModuleUsingFQN(fqn,[path]+pythonpath) if node is not None: return node # try searching in same package hierarchy basedir = getPackageBaseDirectory(scope.module.filename) if fqn.split('.')[0] == os.path.split(basedir)[-1]: # base package in fqn matches base directory restOfFqn = ".".join(fqn.split('.')[1:]) node = getModuleUsingFQN(restOfFqn,[basedir]+pythonpath) if node is not None: return node # try searching the python path node = getModuleUsingFQN(fqn,pythonpath) if node is not None: return node
def test_getsTypeOfClassReferencedViaAlias(self): src = trimLines(""" from b.bah import TheClass as FooBah FooBah() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) self.assertEqual(getTypeOf(themodule,"FooBah",[pkgstructureRootDir]).name,"TheClass") self.assertEqual(getTypeOf(themodule,"FooBah",[pkgstructureRootDir]).filename, os.path.abspath(os.path.join("a","b","bah.py")))
def resolveImportedModuleOrPackage(scope, fqn, pythonpath): # try searching from directory containing scope module path = os.path.dirname(scope.module.filename) node = getModuleUsingFQN(fqn, [path] + pythonpath) if node is not None: return node # try searching in same package hierarchy basedir = getPackageBaseDirectory(scope.module.filename) if fqn.split('.')[0] == os.path.split(basedir)[-1]: # base package in fqn matches base directory restOfFqn = ".".join(fqn.split('.')[1:]) node = getModuleUsingFQN(restOfFqn, [basedir] + pythonpath) if node is not None: return node # try searching the python path node = getModuleUsingFQN(fqn, pythonpath) if node is not None: return node
def test_getsTypeOfClassReferenceFromImportedPackage(self): src = trimLines(""" import b.bah a = b.bah.TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") module = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) res = getTypeOf(module, "a", [pkgstructureRootDir]) assert isinstance(res, Instance) assert isinstance(res.getType(), Class) assert res.getType().name == "TheClass"
def test_getsTypeOfClassReferenceFromImportedPackage(self): src = trimLines(""" import b.bah a = b.bah.TheClass() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") module = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) res = getTypeOf(module,"a",[pkgstructureRootDir]) assert isinstance(res,Instance) assert isinstance(res.getType(),Class) assert res.getType().name == "TheClass"
def test_getsTypeOfSimpleClassInstanceReference(self): src = trimLines(""" from b.bah import TheClass a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") module = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) res = getTypeOf(module,"a",[pkgstructureRootDir]) assert isinstance(res,Instance) assert isinstance(res.getType(),Class) assert res.getType().name == "TheClass"
def test_doesntGetTypeDefinedInChildFunction(self): src = trimLines(""" from b.bah import TheClass a = AnotherClass() def foo(): a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) assert getTypeOf(themodule,"a",[pkgstructureRootDir]) is None
def test_getsTypeOfSimpleClassInstanceReference(self): src = trimLines(""" from b.bah import TheClass a = TheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") module = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) res = getTypeOf(module, "a", [pkgstructureRootDir]) assert isinstance(res, Instance) assert isinstance(res.getType(), Class) assert res.getType().name == "TheClass"
def test_doesntGetTypeDefinedInChildFunction(self): src = trimLines(""" from b.bah import TheClass a = AnotherClass() def foo(): a = TheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) assert getTypeOf(themodule, "a", [pkgstructureRootDir]) is None
def test_getsTypeOfClassReferencedViaAlias(self): src = trimLines(""" from b.bah import TheClass as FooBah FooBah() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(testdata.TheClass, "a.b.bah") themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) self.assertEqual( getTypeOf(themodule, "FooBah", [pkgstructureRootDir]).name, "TheClass") self.assertEqual( getTypeOf(themodule, "FooBah", [pkgstructureRootDir]).filename, os.path.abspath(os.path.join("a", "b", "bah.py")))
def test_getsTypeOfClassImportedFromPackageInitFile(self): initfile = trimLines(""" from bah import TheClass """) src = trimLines(""" from a import b b.TheClass() """) createSourceNodeAt(src,"a.foo") createSourceNodeAt(testdata.TheClass, "a.b.bah") createSourceNodeAt(initfile,"a.b.__init__") themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) self.assertEqual(getTypeOf(themodule,"b.TheClass",[pkgstructureRootDir]).name,"TheClass") self.assertEqual(getTypeOf(themodule,"b.TheClass",[pkgstructureRootDir]).filename, os.path.abspath(os.path.join("a","b","bah.py")))
def test_findsClassRefUsingFromImportStatement(self): src=trimLines(""" from a.b.bah import TheClass """) classsrc=trimLines(""" class TheClass: pass """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(classsrc, "a.b.bah") module = getModuleUsingFQN("a.foo",[pkgstructureRootDir]) filename = os.path.abspath(os.path.join("a","foo.py")) defn = findDefinitionByCoords(filename,1,21,[]) assert defn.filename == os.path.abspath(os.path.join("a","b","bah.py")) assert defn.lineno == 1 assert defn.colno == 6 assert defn.confidence == 100
def test_getsTypeOfInstanceThatIsAnAttributeOfSelf(self): src = trimLines(""" class TheClass: def theMethod(self): pass class AnotherClass: def __init__(self): self.a = TheClass() def anotherFn(self): self.a.theMethod() """) root = createSourceNodeAt(src,"a.foo") module = getModuleUsingFQN('a.foo',[pkgstructureRootDir]) theclass = getTypeOf(module,"TheClass",[pkgstructureRootDir]) fn = getTypeOf(module,"AnotherClass.anotherFn",[pkgstructureRootDir]) self.assertEqual("TheClass",getTypeOf(fn,"self.a",[pkgstructureRootDir]).getType().name)
def test_getsTypeOfClassImportedFromPackageInitFile(self): initfile = trimLines(""" from bah import TheClass """) src = trimLines(""" from a import b b.TheClass() """) createSourceNodeAt(src, "a.foo") createSourceNodeAt(testdata.TheClass, "a.b.bah") createSourceNodeAt(initfile, "a.b.__init__") themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir]) self.assertEqual( getTypeOf(themodule, "b.TheClass", [pkgstructureRootDir]).name, "TheClass") self.assertEqual( getTypeOf(themodule, "b.TheClass", [pkgstructureRootDir]).filename, os.path.abspath(os.path.join("a", "b", "bah.py")))
def handlePackageScope(package, fqn, pythonpath): child = package.getChild(fqn) if child: return child # try searching the fs node = getModuleUsingFQN(fqn,[package.path]+pythonpath) if node: return node # try the package init module initmod = package.getChild("__init__") if initmod is not None: type = getImportedType(initmod, fqn, pythonpath) if type: return type raise "Shouldn't get to here"
def handlePackageScope(package, fqn, pythonpath): child = package.getChild(fqn) if child: return child # try searching the fs node = getModuleUsingFQN(fqn, [package.path] + pythonpath) if node: return node # try the package init module initmod = package.getChild("__init__") if initmod is not None: type = getImportedType(initmod, fqn, pythonpath) if type: return type raise "Shouldn't get to here"
def test_getsTypeOfInstanceThatIsAnAttributeOfSelf(self): src = trimLines(""" class TheClass: def theMethod(self): pass class AnotherClass: def __init__(self): self.a = TheClass() def anotherFn(self): self.a.theMethod() """) root = createSourceNodeAt(src, "a.foo") module = getModuleUsingFQN('a.foo', [pkgstructureRootDir]) theclass = getTypeOf(module, "TheClass", [pkgstructureRootDir]) fn = getTypeOf(module, "AnotherClass.anotherFn", [pkgstructureRootDir]) self.assertEqual( "TheClass", getTypeOf(fn, "self.a", [pkgstructureRootDir]).getType().name)
def handleModuleClassOrFunctionScope(scope, fqn, pythonpath): assert isinstance( scope, Function) or isinstance(scope, Class) or isinstance( scope, Module), "this function cant take a scope of type" + str( scope.__class__) if fqn == "self" and scopeIsAMethod(scope): return Instance(scope.getParent()) child = scope.getChild(fqn) if child: return child type = scanScopeSourceForType(scope, fqn) if type is not None: return type type = getImportedType(scope, fqn, pythonpath) # try imported types if type is not None: return type if isinstance( scope, Module) and scope.getSourceNode().filename.endswith("__init__.py"): # try searching the package for a module packagedir = os.path.dirname(scope.getSourceNode().filename) node = getModuleUsingFQN(fqn, [packagedir] + pythonpath) if node is not None: return node try: parentScope = scope.getParent() except ElementHasNoParentException: return None # scope is a module while isinstance(parentScope, Class): # don't search class scope, since this is not accessible except # through self (is this true?) parentScope = parentScope.getParent() return getTypeOf(parentScope, fqn, pythonpath)