Skip to content

Python: a parameter named like an imported function links to it when read as a value #3430

Open

Description

@inth3shadows

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.prompt is referenced by the import and called by prompt_for_value (line 14). Nothing in __init__ refers to it.
  • Actual: a references edge goes from Option.__init__ to termui.prompt at line 9, prompt_text = prompt, where prompt is 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=1 shows 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:

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);

https://github.com/colbymchenry/codegraph/blob/461a5d87a873a4932997cd9a49042ab30e45a675/src/resolution/name-matcher.ts#L15230-L15231

Python already has the check that would apply, pythonBindsLocally, but this path never calls it:

https://github.com/colbymchenry/codegraph/blob/461a5d87a873a4932997cd9a49042ab30e45a675/src/resolution/name-matcher.ts#L778

#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions