MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolvePythonModuleMember

Function resolvePythonModuleMember

src/resolution/import-resolver.ts:4148–4253  ·  view source on GitHub ↗

* Resolve a Python qualified reference whose receiver is an imported MODULE: * `certs.where()` after `from . import certs`, `mod.func()` after `import mod` * or `from pkg import mod`. The receiver names a submodule (a file), not a * symbol, so the generic symbol lookup in `resolveViaImport` can't

(
  ref: UnresolvedRef,
  imports: ImportMapping[],
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

4146 * to the other strategies untouched.
4147 */
4148function resolvePythonModuleMember(
4149 ref: UnresolvedRef,
4150 imports: ImportMapping[],
4151 context: ResolutionContext
4152): ResolvedRef | null {
4153 const dotIdx = ref.referenceName.indexOf('.');
4154 if (dotIdx <= 0) return null;
4155 const receiver = ref.referenceName.substring(0, dotIdx);
4156 // The immediate member of the module (first segment after the receiver).
4157 const member = ref.referenceName.substring(dotIdx + 1).split('.')[0];
4158 if (!member) return null;
4159
4160 // `import a.b as X` inside the calling function binds X there, ahead of any
4161 // module-level binding of X, and the file's mappings don't hold it (#2600).
4162 const local = pythonFunctionModuleAlias(receiver, ref, context);
4163 if (local) {
4164 // The module from the package roots, and its member as the module binds it,
4165 // the same lookup as for a module-level binding below.
4166 const localPath = resolveImportPath(local, ref.filePath, ref.language, context);
4167 const target = localPath && localPath !== ref.filePath
4168 ? findExportedSymbol(localPath, { isDefault: false, isNamespace: false, exportedName: member, memberName: null }, 'python', context, new Set())
4169 : null;
4170 return target ? { original: ref, targetNodeId: target.id, confidence: 0.85, resolvedBy: 'import' } : null;
4171 }
4172
4173 for (const imp of pythonMemberImports(imports, receiver, ref.referenceName)) {
4174 // `import pkg.mod` binds `pkg`, so a call spells the whole path out —
4175 // `pkg.mod.func()`. The mapping names the module by its last segment, which
4176 // never matches that receiver; match the full dotted source instead.
4177 const spelledOut = imp.isNamespace && imp.localName === imp.source.split('.').pop()
4178 && imp.source.includes('.') && ref.referenceName.startsWith(`${imp.source}.`);
4179 if (imp.localName !== receiver && !spelledOut) continue;
4180
4181 // `import mod` / `import numpy as np` bind the module at `source` itself;
4182 // `from . import certs` / `from pkg import mod` bind a SUBMODULE whose
4183 // dotted path is the source joined with the imported name.
4184 //
4185 // Join with the EXPORTED name, not the local one: under
4186 // `from pkg import mod as alias` the receiver is `alias` but the module on
4187 // disk is `pkg.mod`, and building `pkg.alias` looked for a file that does
4188 // not exist — so the aliased form dropped its `calls` edge while the plain
4189 // form (where the two names coincide) worked (#1626). For an unaliased
4190 // import the two are identical, so this changes nothing there.
4191 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
4192 let modulePath = imp.isNamespace
4193 ? imp.source
4194 : imp.source.endsWith('.')
4195 ? imp.source + moduleName
4196 : imp.source + '.' + moduleName;
4197 const rest = ref.referenceName
4198 .substring(spelledOut ? imp.source.length + 1 : dotIdx + 1)
4199 .split('.');
4200
4201 if (!imp.isNamespace && pythonPackageBinds(imp, ref.filePath, context)) continue;
4202 let resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
4203 if (!resolvedPath || resolvedPath === ref.filePath) continue;
4204
4205 // The symbol `name` as the module at `filePath` binds it: a module-level

Callers 1

resolveViaImportFunction · 0.85

Calls 6

pythonMemberImportsFunction · 0.90
resolveImportPathFunction · 0.85
findExportedSymbolFunction · 0.85
pythonPackageBindsFunction · 0.85
boundFunction · 0.70
getNodesInFileMethod · 0.65

Tested by

no test coverage detected