Repository navigation
Description
What happens
In Python, a parameter named like a function the file imports is linked to that function when the parameter is read as a value, for example on the right of an assignment. The parameter shadows the import inside the function, so the edge is wrong.
# pkg/termui.py
def prompt(text):
return input(text)# pkg/core.py
from .termui import prompt
class Option:
def __init__(self, prompt=False):
if prompt is True:
prompt_text = "Value"
else:
prompt_text = prompt
self.prompt = prompt_text
def prompt_for_value(self):
assert self.prompt is not None
return prompt(self.prompt)pkg/__init__.py is empty.
- Expected:
termui.promptis referenced by the import and called byprompt_for_value(line 14). Nothing in__init__refers to it. - Actual: a
referencesedge goes fromOption.__init__totermui.promptat line 9,prompt_text = prompt, wherepromptis the parameter.
Repro (main at 461a5d8, which is tag v1.6.4: reproduced; also 69bbb38; Node v22.23.2, Linux)
codegraph init -i
sqlite3 .codegraph/codegraph.db "select e.kind, s.qualified_name, e.line, e.metadata from edges e join nodes t on t.id=e.target join nodes s on s.id=e.source where t.name='prompt' and t.kind='function' and e.kind<>'contains' order by e.line"
imports|pkg/core.py|1|{"confidence":0.9,"resolvedBy":"import","refName":"prompt"}
references|Option::__init__|9|{"confidence":0.9,"resolvedBy":"import","refName":"prompt","refKind":"function_ref","fnRef":true}
calls|Option::prompt_for_value|14|{"confidence":0.9,"resolvedBy":"import","refName":"prompt"}
Runs:
- v1.6.4: 3 of 3 on wasm, and 3 of 3 with the native kernel (
CODEGRAPH_KERNEL_DEBUG=1shows it loaded). - 69bbb38: 3 of 3.
The same shape in JavaScript gives no edge: a constructor parameter prompt shadowing import { prompt } from './termui.js', read as promptText = prompt.
Cause (inferred from the source; the edge's metadata matches it)
A function used as a value (function_ref) is resolved through the file's import first. The only shadowing check after that is for JavaScript and TypeScript:
codegraph/src/resolution/index.ts
Lines 2041 to 2066 in 461a5d8
| if (ref.referenceKind === 'function_ref') { | |
| // `this.<member>` values (TS/JS) resolve ONLY against the enclosing | |
| // class's own members — never a same-named symbol elsewhere. | |
| if (ref.referenceName.startsWith('this.')) { | |
| return this.gateLanguage(this.resolveThisMemberFnRef(ref), ref); | |
| } | |
| if ((ref.language === 'python' || ref.language === 'go' || ref.language === 'csharp') && ref.referenceName.includes('.')) { | |
| return this.gateLanguage(matchFunctionRef(ref, this.context), ref); | |
| } | |
| const viaImport = this.gateLanguage(resolveViaImport(ref, this.context), ref); | |
| // A local of the name in scope is the value handed on, which matchFunctionRef finds (#2949). | |
| if (viaImport && !isImportShadowedByJsBinding(viaImport, ref, this.context)) { | |
| const target = this.nodeById(viaImport.targetNodeId); | |
| if ( | |
| target && | |
| (target.kind === 'function' || | |
| target.kind === 'method' || | |
| // Python (#1478): an imported class used as a value (`return | |
| // OrgSerializerFull`) resolves through its import like any | |
| // callback — mirrors matchFunctionRef's bareClassOk. | |
| (ref.language === 'python' && target.kind === 'class')) | |
| ) { | |
| return viaImport; | |
| } | |
| } | |
| return this.gateLanguage(matchFunctionRef(ref, this.context), ref); |
Python already has the check that would apply, pythonBindsLocally, but this path never calls it:
#2219 added the rule for this case ("Python: a name the enclosing function binds is that local", parameters included). It lives in the name-matching step, matchFunctionRef, which only runs when the import step finds nothing. When the file also imports the name, the import step answers first and the rule is never asked.
A fix could apply that rule to a Python function_ref before taking the import, as #3037 did for calls.
Impact
pallets/click at 2247b35ea1c47c727d7a06e51fa280e12a863ff6: Option.__init__ takes a prompt parameter. Its read at prompt_text = prompt is linked to termui.prompt, which core.py imports. So Option.__init__ shows up as a dependent of click.prompt. That held in 3 of 3 runs, wasm and native kernel.
Related: #2219 (the rule for parameters, which the import step bypasses), #3037 (the same shadowing for calls, fixed in 1.6.4), #2949 (the JavaScript form of this check), #3046 (a parameter named like an imported module).
Activity