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

Function pythonBareImportsInScope

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

* The file's import mappings as a bare Python name reads them (#3229). The * mappings read from-imports at any depth, so `from pkg.core import reverts` * written inside one function was the whole file's, and every other call of * `reverts` linked the import, though the file's own `def reverts` is

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

Source from the content-addressed store, hash-verified

3868 * binding other than an import, and the module's own imports, change nothing.
3869 */
3870function pythonBareImportsInScope(ref: UnresolvedRef, imports: ImportMapping[], context: ResolutionContext): ImportMapping[] {
3871 const name = ref.referenceName;
3872 const binds = (m: ImportMapping): boolean => m.localName === name;
3873 if (!imports.some(binds)) return imports;
3874 const local = pythonFunctionImportKeys(name, ref.filePath, context);
3875 if (local.size === 0) return imports;
3876 const keyOf = (m: ImportMapping): string => (m.isNamespace ? `${m.source}:*` : `${m.source}:${m.exportedName}`);
3877 const binding = pythonReceiverBinding(name, ref, context);
3878 if (binding === 'module') {
3879 const moduleLevel = pythonModuleImportKeys(name, ref.filePath, context);
3880 return imports.filter((m) => !binds(m) || moduleLevel.has(keyOf(m)) || !local.has(keyOf(m)));
3881 }
3882 if (binding === 'local' || 'module' in binding) return imports;
3883 return imports.filter((m) => !binds(m) || (!m.isNamespace && m.source === binding.source && m.exportedName === binding.exportedName));
3884}
3885
3886/**
3887 * Resolve a Python qualified reference whose receiver is an imported MODULE:

Callers 1

pythonImportsInScopeFunction · 0.85

Calls 3

hasMethod · 0.80
bindsFunction · 0.70
keyOfFunction · 0.70

Tested by

no test coverage detected