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

Function pythonBareImportsInScope

src/resolution/import-resolver.ts:3868–3882  ·  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

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